multidimensinal array: group and output php -


hi have array coming in:

$myregiondata =     array (      [0] => stdclass object          (              [howmuch] => 1              [country] => id              [state] => jawa barat              )      [1] => stdclass object          (              [howmuch] => 1              [country] => ro              [state] => bucuresti           )       [2] => stdclass object          (              [howmuch] => 2              [country] =>              [state] => california           )       [3] => stdclass object          (              [howmuch] => 1              [country] =>              [state] => texas             )       )  

im trying group array outuput such

id  jawa barat (1)  ro  bucuresti (1)  california (2)  texas (1) 

i have tried key value associations, loops etc.. , can seem combine states in display.

any advice appreciated

i'd reorganise country first make things easier:

// hold re-indexed array $indexed = array();  // store each state's object under country identifier foreach($myregiondata $object) {     if(!isset($indexed[$object->country])) {         $indexed[$object->country] = array();     }      $indexed[$object->country][] = $object; }  // output data foreach($indexed $country => $states) {     echo $country, "\n";     foreach($states $state) {         printf(" %s (%u)\n", $state->state, $state->howmuch);     } } 

Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -