简体   繁体   English

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

[英]How to combine or merge array in PHP

Could this be possible? 这可能吗? How to combine or merge arrays in php. 如何在php中合并或合并数组。 to fetch data individualy for echoing purpose. 为回显目的单独获取数据。 Could this be possible? 这可能吗? How to combine or merge arrays in php. 如何在php中合并或合并数组。 to fetch data individualy for echoing purpose. 为回显目的单独获取数据。

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
    )

)

To this 对此

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
                  )

)

USe This 用这个

array_merge_recursive

Follow Link 追踪连结

Link With Example 链接示例

Here is my answer. 这是我的答案。 Let me know if this works for you. 让我知道这是否适合您。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM