php - Convert Associative Array -> Grouping by Value -


i have associative array looks this:

enter image description here

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); 

test here


Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -