user3163495 user3163495 - 1 year ago 69
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
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 Source

You can use array_column for that:

$FoodArr = array_column($MasterArr, 'food');
$TasteArr = array_column($MasterArr, 'taste');
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download