0

There are a lot of Threads about filtering, but i'm google all day long and can't get this to work.

I have an array, who is created thru a mysqli query.

$cat_cross =
    array(2)
      { [0]=> array(3)
          { ["cat_cross_id"]=> string(2) "24" 
            ["cat_cross_items_id"]=> string(1) "4" 
            ["cat_cross_user_id"]=> string(2) "58" 
          } 
        [1]=> array(3)
          { ["cat_cross_id"]=> string(2) "25" 
            ["cat_cross_items_id"]=> string(1) "6" 
            ["cat_cross_user_id"]=> string(2) "58" 
          }
      }

Now i want to get a query only with the values of "cat_cross_items_id" because i need this later to compare it in a if-clause with an other var.

I tried this one:

$allowed = array("cat_cross_items_id");
var_dump(array_intersect_key($cat_cross, array_flip($allowed)));

I think this works on the first level of array, but how i can filter the second on?

Desire Result:

$new_arr = array(4,6)

Thank you very much!

0

1 Answer 1

1

PHP >= 5.5.0:

$new_arr = array_column($cat_cross, "cat_cross_items_id");

PHP >= 5.3.0:

$new_arr = array_map(function($v) { return $v["cat_cross_items_id"]; }, $cat_cross);

Earlier version will need a loop or call an outside function in array_map().

Sign up to request clarification or add additional context in comments.

3 Comments

As it would have been yesterday: stackoverflow.com/a/28307212 :)
Yes, seems like deja vu, but that wasn't yesterdays question you linked.
I'm really sorry to give you both a deja vu, but i could find the thread or could match it to my code :( But now. thank you very much!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.