-1

Could this be possible? How to combine or merge arrays in php. to fetch data individualy for echoing purpose. Could this be possible? How to combine or merge arrays in 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
                  )

)
Martin J.
  • 5,028
  • 4
  • 24
  • 41
  • 2
    at least show some effort and attempt – Kevin Mar 09 '15 at 06:06
  • [This SO article](http://stackoverflow.com/questions/9112920/array-merge-on-key-of-two-associative-arrays-in-php) has the answer you are looking for. Please read it and then answer your own question. – Tim Biegeleisen Mar 09 '15 at 06:06
  • This question doesn't really offer enough clarity. There are some techniques (in the answers below) that will work on this sample input but not work if there are multiple `id`s. Please provide a better, more complex [mcve]. – mickmackusa Aug 21 '22 at 04:02

3 Answers3

0

USe This

array_merge_recursive

Follow Link

Link With Example

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);
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
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);
Raj
  • 150
  • 1
  • 10