Split an array into auxiliary arrays

I would like to split the array:

$o = json_decode('[{"id":"1","color":"green"},{"id":"2","color":"green"},{"id":"3","color":"yellow"},{"id":"4","color":"green"}]'); 

based on the color attribute of each element and filling in the corresponding submatrices

 $a = array("green", "yellow", "blue"); function isGreen($var){ return($var->color == "green"); } $greens = array_filter($o, "isGreen"); $yellows = array_filter($o, "isYellow"); // and all possible categories in $a.. 

my $a has a length> 20 and can grow larger, so I need a general way instead of writing functions manually

There seems to be an array_split function to generate all filtered arrays
or I need some kind of lambda function, maybe

+4
source share
3 answers

You can do something like:

 $o = json_decode('[{"id":"1","color":"green"},{"id":"2","color":"green"},{"id":"3","color":"yellow"},{"id":"4","color":"green"}]'); $greens = array_filter($o, function($item) { if ($item->color == 'green') { return true; } return false; }); 

Or if you want to create something really common, you can do something like the following:

 function filterArray($array, $type, $value) { $result = array(); foreach($array as $item) { if ($item->{$type} == $value) { $result[] = $item; } } return $result; } $o = json_decode('[{"id":"1","color":"green"},{"id":"2","color":"green"},{"id":"3","color":"yellow"},{"id":"4","color":"green"}]'); $greens = filterArray($o, 'color', 'green'); $yellows = filterArray($o, 'color', 'yellow'); 

In my second example, you can simply pass an array and tell the function what to filter (for example, color or some other property of the future) based on what the value is.

Please note that I did not make any mistakes, checking if the properties really exist

+6
source

I would not go the way of creating a ton of functions manually or dynamically.

Here my idea and design can be changed, so the filters can be grouped:

 <?php class ItemsFilter { protected $items = array(); public function __construct($items) { $this->items = $items; } public function byColor($color) { $items = array(); foreach ($this->items as $item) { // I don't like this: I would prefer each item was an object and had getColor() if (empty($item->color) || $item->color != $color) continue; $items[] = $item; } return $items; } } $items = json_decode('[{"id":"1","color":"green"},{"id":"2","color":"green"},{"id":"3","color":"yellow"},{"id":"4","color":"green"}]'); $filter = new ItemsFilter($items); $greens = $filter->byColor('green'); echo '<pre>'; print_r($greens); echo '</pre>'; 
+1
source

If you need more arguments, you can use this function:

 function splitArray($array, $params) { $result = array(); foreach ($array as $item) { $status = true; foreach ($params as $key => $value) { if ($item[$key] != $value) { $status = false; continue; } } if ($status == true) { $result[] = $item; } } return $result; } $greensAndID1 = splitArray($o, array('color' => 'green', 'id' => 1)); 
0
source

All Articles