PHP Combine Multidimensional Array -


i trying merge 2 arrays containing multidimensional array , create third array in format below"

here first multidimensional array:

array (     [usa1] => array     (         [0] => newyork         [1] => mass     )  [usa2] => array     (         [0] => newyork     )  [usa3] => array     (         [0] => newyork     )  ) 

this second multidimensional array:

array (     [usa1] => array         (             [newyork] => array                 (                 [0] => array                     (                         [0] => town1                         [1] => town2                     )             )          [mass] => array             (                 [0] => array                     (                         [0] => town3                         [1] => town4                     )             )     )  [usa2] => array     (         [newyork] => array             (                 [0] => array                     (                         [0] => town1                         [1] => town2                     )             )     ) 

now want make 3rd array merging based on there common key. if keys matching need assign 1 of values array in round robin fashion:

for e.g. if value "newyork" under usa1 key, have assign "town1" value array. key present under usa2 have assign "town2" (round robin fashion). if there more "newyork" values present , if have more values "town9" have assign value, if not present have assign "town1" value back. if key present 1 time "mass" need remove second value "town4" in case.

    array     (         [usa1] => array             (                 [0] => newyork => town1                 [1] => mass => town3             )      [usa2] => array         (             [0] => newyork => town2         )      [usa3] => array         (             [0] => newyork =>town1         )  ) 

note: array output except 3rd 1 print_r command

greatly appreciate help. thank you.

i'm not clear you're asking for. it's not merger because in final array [usa3] should empty there's no matching key in array2. , you're asking values assigned round-robin, not how expect array merge behave.

what i'm assuming you're trying evenly distribute towns across cities. based on assumption used values usa1 in array two, produce third array provided. i've updated code example merge second array.

$map = array();  foreach ($array2 $usa => $cities)  {      $map = $cities + $map;  }  $towns = array();  foreach($array1 $usa => $cityarray) {     while(!empty($cityarray))      {         $city = array_shift( $cityarray );         if(empty($towns[$city]))             $towns[$city] = $map[$city][0];          $array3[ $usa ][] = array( $city => array_shift( $towns[$city] ) );     } } 

this works if have different values in $array2['us1'] , $array2['us2'] should merge 2 arrays before sending code above. updated code merges second array properly.


Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -