如何在PHP中组合或合并数组

时间:2015-03-09 06:01:23

标签: php arrays

这可能吗?如何在php中组合或合并数组。为了回应目的而单独获取数据。这有可能吗?如何在php中组合或合并数组。为了回应目的而单独获取数据。

Array(

[0] => stdClass Object
    (
        [id] => 1
        [icd] => J96.0
        [rank] => 1
        [description] => Acute respiratory failure
    )

[1] => stdClass Object
    (
        [id] => 1
        [icd] => J44.1
        [rank] => 2
        [description] => Chronic obstructive pulmonary disease with (acute) exacerbation
    )

[2] => stdClass Object
    (
        [id] => 1
        [icd] => J18.9
        [rank] => 3
        [description] => Pneumonia, unspecified organism
    )

到此

Array(
 [id]  => 1
 [icd] => (
           [0] => J96.0
           [1] => J44.1
           [2] => J18.9
          )
 [description] => (
                   [0] => Acute respiratory failure
                   [1] => Chronic obstructive pulmonary disease with (acute) exacerbation
                   [2] => Pneumonia, unspecified organism
                  )

)

3 个答案:

答案 0 :(得分:0)

使用此

array_merge_recursive

关注链接

Link With Example

答案 1 :(得分:0)

这是我的答案。如果这对您有用,请告诉我。

$output = array();

$arrayAB = array_merge($arrayA, $arrayB);
foreach ( $arrayAB as $value ) {
    $keys = array("id", "icd", "rank", "description"); 
    foreach ($keys as $key) {
        $currKey = $value[$key];
        if ( !isset($output[$currKey]) ) {
            $output[$currKey] = array();
        }
        $output[$currKey] = array_merge($output[$currKey], $value);
    }
}

var_dump($output);

答案 2 :(得分:0)

$new_array = array();
foreach($your_array as $row){
         $new_array['id']        = $row->id;
         $new_array['icd'][] = $row->icd;
         $new_array['description'][] = $row->description; 
}
print_r($new_array);