tags:

views:

95

answers:

3

How can i filter this array values with [category] => 1

[0] => Array
    (
        [link] => index
        [image] => spot
        [category] => 0
    )

[1] => Array
    (
        [link] => test
        [image] => spotless
        [category] => 0
    )

[2] => Array
    (
        [link] => differentcat
        [image] => spotly
        [category] => 1
    )

thanks.

+2  A: 

You can use array_filter that checks for the category value in a callback. http://php.net/manual/en/function.array-filter.php

Trevor
+5  A: 

Use array_filter.

You want something like this (presuming you want to keep entries with category 1):

function categoryone($var)
{
    return (is_array($var) && $var['category'] == 1);
}

print_r(array_filter($your_array, "categoryone"));
Dominic Rodger
same error as above : {doc_root}/index.php(17): array_filter(Array, 'categoryone')[internal function]: categoryone(Array){doc_root}/index.php(14)Undefined index: category
O.
sorry ok. its working thanks
O.
+2  A: 

Define a filter function like this:

function filter_function($var) {
    return is_array($var) && $var['category'] == 1;
}

… and then use array_filter() to apply this function to your array:

$filtered_array = array_filter($my_array, 'filter_function');

Edit: changed the filtering function to keep matching values instead of discarding them.

igor
Thanks igor, its working
O.