user3163495 user3163495 - 4 months ago 7
PHP Question

Without using a loop, how can I "chop" an array of associative arrays into separate arrays, grouped by keys?

Say I have the following array of associative arrays:

$MasterArr = array(
array("food" => "apple", "taste" => "sweet"),
array("food" => "lemon", "taste" => "sour"),
array("food" => "steak", "taste" => "meaty")
);


Without using a
foreach
loop, is there a way that can I "chop" it into 2 different arrays whose values come from the same keys, so it looks like this:

$FoodArr = array("apple","lemon","steak");
$TasteArr = array("sweet","sour","meaty");

Answer

You can use array_column for that:

$FoodArr = array_column($MasterArr, 'food');
$TasteArr = array_column($MasterArr, 'taste');
Comments