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!

1 Answers1

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().

AbraCadaver
  • 78,200
  • 7
  • 66
  • 87