php - Convert Associative Array -> Grouping by Value -
i have associative array looks this:
i want convert it. should grouped "product_service_category_name" , should this:
$productservices = [ '' => [ 1 => 'diverses service #1', 2 => 'diverses service #2' ], 'beratung' => [ 5 => 'ernährungsberatung' ], 'massagen' => [ 3 => 'heilmassage', 4 => 'shiatsu' ] ];
please take care fact, value maybe empty.
could me this?
thank you!
function group($array) { $group = array(); $key = 'product_service_category_name'; foreach (array_values($array) $values) { if ( empty($values[$key])) { $ptr = 'empty'; } else { $ptr = $values[$key]; } $new_key = $values['product_service_id']; $group[$ptr][$new_key] = $values['product_service_name']; } return $group; } $grouped = group($ungrouped);
Comments
Post a Comment