0

我有下面的数组:

[1]=>
  array(2) {
    [0]=>
        object(stdClass)#23 (7) {
          ["AddressesTableID"]=> string(1) "8"
          ["AccreditNo"]=> string(13) "5129876de28ff"
          ["Type"]=> string(4) "home"
          ["Street"]=> string(34) "Wallace, Village, Under the bridge"
          ["Municipality"]=> string(8) "Tortuous"
          ["Province"]=> string(8) "Sardonic"
          ["ContactNo"]=> string(8) "92012010"
        }
    [1]=>
        object(stdClass)#24 (7) {
          ["AddressesTableID"]=> string(1) "9"
          ["AccreditNo"]=> string(13) "5129876de28ff"
          ["Type"]=> string(6) "office"
          ["Street"]=> string(25) "Rasputin Query, Palpitate"
          ["Municipality"]=> string(7) "Opulent"
          ["Province"]=> string(6) "Number"
          ["ContactNo"]=> string(8) "29101011"
    }
  }

你们能帮我把它变成:

所有的值都作为一个数组分组到相似的键中?

["AccreditNo"]=> array(2) { "5129876de28ff", "GKIJUGUIKGI" }
["Type"]=> array(2) { "home", "home" }
["Street"]=> ...
["Municipality"]=> ... 
["Province"]=> ...
["ContactNo"]=> ...
4

2 回答 2

2

尝试:

$input  = array( /* your input data*/ );
$output = array();

foreach ( $input as $data ) {
  foreach ( $data as $key => $value ) {
    if ( !isset($output[$key]) ) {
      $output[$key] = array();
    }
    $output[$key][] = $value;
  }
}
于 2013-02-25T14:17:24.963 回答
2

您可以使用 array_merge_recursive() 函数,如下所示:

$arr = array_merge_recursive($arr1, $arr2)

有关更多信息,请参阅此处:http ://www.php.net/manual/en/function.array-merge-recursive.php

于 2013-02-25T14:27:17.630 回答