0

My case is like this :

$array1 = array(
                array(
                        'HotelNo'   => '1',
                        'HCode'     => 'IDJOG_00108',
                        'Name'      => 'Dafam Merapi Merbabu',
                        'RmGrade'   => 'Deluxe',
                        'TotalRate' => '1035'
                ),
                array(
                        'HotelNo'   => '4',
                        'HCode'     => 'IDJOG_00110',
                        'Name'      => 'Desa Puri',
                        'RmGrade'   => 'Standard',
                        'TotalRate' => '427'
                )

            );

    $array2 = array(
                array(
                        'HotelCode' => 'IDJOG_00108',
                        'HotelName' => 'Dafam Merapi Merbabu',
                        'Phone'     => '62 0274 4332772',
                        'Address'   => 'jl. Seturan Raya Yogyakarta'
                ),
                array(
                        'HotelCode' => 'IDJOG_00110',
                        'HotelName' => 'Desa Puri',
                        'Phone'     => '0274 - 371225',
                        'Address'   => 'Jl. Gedung Kuning No. 118'
                ),
                array(
                        'HotelCode' => 'IDJOG_00111',
                        'HotelName' => 'Pyrenees-Yog',
                        'Phone'     => '+62 274 543299',
                        'Address'   => 'Jl.Sosrowijayan No.1'
                )

            );

I want to combine $array1 and $array2

I want output like this :


IDJOG_00108

Dafam Merapi Merbabu

Deluxe

1035

jl. Seturan Raya Yogyakarta

62 0274 4332772


IDJOG_00110

Desa Puri

Standard

427

Jl. Gedung Kuning No. 118

0274 - 371225

I could only do like this :

foreach($array1 as $key=>$value){
        echo $value['HCode'].'<br>';
        echo $value['Name'].'<br>';
        echo $value['RmGrade'].'<br>';
        echo $value['TotalRate'].'<br>';
        echo '<br><br>';
    }   

What is the right way to combine the two arrays?

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
samuel toh
  • 6,836
  • 21
  • 71
  • 108
  • `$merged = array_merge($array1, $array2, $array3)` and so on... – Nechemya Kanelsky Feb 09 '16 at 22:50
  • you are on the right track, if you only need to print it, just use another `foreach` loop and an `if` to search the corresponding key into the sub `foreach`, – Kevin Feb 09 '16 at 22:54
  • 2
    the simplest way use `array_merge()` in a loop, but every iteration, you will need to find corresponding item from the second array, and after the loop, you need to add rest of the second array to the result. but it's not so beauty :(. I advise restructure your data. It will be hard and will eats much more time and memory when you reach 10000 items, for example. – Wizard Feb 09 '16 at 22:58
  • For my case, it can not use the array merge. I tried to answer from budiantoip and it worked – samuel toh Feb 09 '16 at 23:20
  • It doesn't matter, why we need to make bad application architecture solution? I still recommend to restructure your data presentation. Again, it will be hard to support this application. – Wizard Feb 09 '16 at 23:25

2 Answers2

1

Try this one :

<?php
    $array1 = array(
                array(
                        'HotelNo'   => '1',
                        'HCode'     => 'IDJOG_00108',
                        'Name'      => 'Dafam Merapi Merbabu',
                        'RmGrade'   => 'Deluxe',
                        'TotalRate' => '1035'
                ),
                array(
                        'HotelNo'   => '4',
                        'HCode'     => 'IDJOG_00110',
                        'Name'      => 'Desa Puri',
                        'RmGrade'   => 'Standard',
                        'TotalRate' => '427'
                )

            );

    $array2 = array(
                array(
                        'HotelCode' => 'IDJOG_00108',
                        'HotelName' => 'Dafam Merapi Merbabu',
                        'Phone'     => '62 0274 4332772',
                        'Address'   => 'jl. Seturan Raya Yogyakarta'
                ),
                array(
                        'HotelCode' => 'IDJOG_00110',
                        'HotelName' => 'Desa Puri',
                        'Phone'     => '0274 - 371225',
                        'Address'   => 'Jl. Gedung Kuning No. 118'
                ),
                array(
                        'HotelCode' => 'IDJOG_00111',
                        'HotelName' => 'Pyrenees-Yog',
                        'Phone'     => '+62 274 543299',
                        'Address'   => 'Jl.Sosrowijayan No.1'
                )

            );

    foreach ($array1 as $room){
        foreach ($array2 as $address){
            if ($room['HCode'] == $address['HotelCode']){
                echo $room['HCode'] . "<BR>";
                echo $address['HotelName'] . "<BR>";
                echo $room['RmGrade'] . "<BR>";
                echo $room['TotalRate'] . "<BR>";
                echo $address['Address'] . "<BR>";
                echo $address['Phone'] . "<BR>";
                echo "<HR>";
            }
        }
    }
?>

Explaination:
$array1 and $array2 is connected by 'HCode' and 'HotelCode' key, so you need to iterate through $array1 first, then $array2 and check if $array1['HCode'] == $array2['HotelCode'], and finally print the contents.

Budianto IP
  • 1,118
  • 1
  • 13
  • 27
0

Something like this, but it's bad solution:

function combine($array1, $array2) {
    $result = array();
    foreach ($array1 as $key1=>$val1) {
      foreach ($array2 as $key2=>$val2) {
        if (strcmp($val1['HCode'], $val2['HotelCode']) == 0) {
          // we're here coz we've found additional info in array2

          // remove unnecessary items
          unset($val2['HotelCode'], $val2['HotelName']);

          $result[] = array_merge($val1, $val2);

          // remove item from array2
          unset($array2[$key2]);

          // assuming we have only one corresponding item in array2
          break;
        }
      }
    }
    // if we don't need the rest of array2
    // just change return to
    // return $result;
    return array_merge($result, $array2);
}

// get combined array    
$final = combine($array1, $array2);

// do whatever we want
var_dump($final);

result:

array(3) {
  [0]=>
  array(7) {
    ["HotelNo"]=>
    string(1) "1"
    ["HCode"]=>
    string(11) "IDJOG_00108"
    ["Name"]=>
    string(20) "Dafam Merapi Merbabu"
    ["RmGrade"]=>
    string(6) "Deluxe"
    ["TotalRate"]=>
    string(4) "1035"
    ["Phone"]=>
    string(15) "62 0274 4332772"
    ["Address"]=>
    string(27) "jl. Seturan Raya Yogyakarta"
  }
  [1]=>
  array(7) {
    ["HotelNo"]=>
    string(1) "4"
    ["HCode"]=>
    string(11) "IDJOG_00110"
    ["Name"]=>
    string(9) "Desa Puri"
    ["RmGrade"]=>
    string(8) "Standard"
    ["TotalRate"]=>
    string(3) "427"
    ["Phone"]=>
    string(13) "0274 - 371225"
    ["Address"]=>
    string(25) "Jl. Gedung Kuning No. 118"
  }
  [2]=>
  array(4) {
    ["HotelCode"]=>
    string(11) "IDJOG_00111"
    ["HotelName"]=>
    string(12) "Pyrenees-Yog"
    ["Phone"]=>
    string(14) "+62 274 543299"
    ["Address"]=>
    string(20) "Jl.Sosrowijayan No.1"
  }
}
Wizard
  • 862
  • 6
  • 9
  • The last array does not have the corresponding data in $array1. It's like you're displaying hotel and room data, but will only be displayed if both exists. Watch carefully the output please :) – Budianto IP Feb 09 '16 at 23:21
  • Np, but it can be fixed by changing one line. I think every programmer definitely need to use his own mind too. This solution more flexible instead of accepted one. :) But it's still bad :( – Wizard Feb 09 '16 at 23:27