Home > database >  foreach using array multidimensional
foreach using array multidimensional

Time:06-27

I have a problem that I want to make a multidimensional array by the list array below:

    $barang=array(
        [
            'id_barang' => 01,
            'nama_barang' => "laptop",
            'merk' => 'lenovo'
        ],
        [
            'id_barang' => 02,
            'nama_barang' => "RAM",
            'merk' => 'lenovo'
        ],
        [
            'id_barang' => 03,
            'nama_barang' => "Keyborad",
            'merk' => 'lenovo'
        ],
    );
    $harga=array(
        [
            'id_barang' => 01,
            'harga' => 2000000,
        ],
        [
            'id_barang' => 02,
            'harga' => 100000,
        ]
        );

I have tried to make a multidimensional array. but my code is still wrong. the code I have tried like below:

    $result=array();
    foreach ($barang as $value) {
        foreach ($harga as $k => $val) {
            $result[$value['id_barang'] = $val["id_barang"]] = [
                'harga' => $val["harga"]
            ];
        }
    }
    echo "<pre>";
    echo print_r($result);

the output of code like below, but it is still wrong code

 Array
 (
   [1] => Array
    (
        [harga] => 2000000
    )
   [2] => Array
    (
        [harga] => 100000
    )
 )

what I want the output of multidimensional array like below :

 Array
  (
  [0] => Array
  (
    [id_barang] => 01
    [nama_barang] => laptop
    [merk] => lenovo
    [dataharga] => Array
            (
                [0] => Array
                    (
                        [id_barang] => 01
                        [harga] => 2000000
                    )
            )
  )
  [1] => Array
  (
    [id_barang] => 02
    [nama_barang] => RAM
    [merk] => lenovo
    [dataharga] => Array
            (
                [1] => Array
                    (
                        [id_barang] => 02
                        [harga] => 100000
                    )
            )
  )
  [2] => Array
  (
    [id_barang] => 03
    [nama_barang] => Keyboard
    [merk] => lenovo
    [dataharga] => Array
            (
                [2] => Array
                    (
                        [id_barang] => ""
                        [harga] => ""
                    )
            )
  )
 )

Thanks in advance and sorry for occasional english mistakes

CodePudding user response:

Try the following code and see whether it works:

$barang=array(
    [
        'id_barang' => 01,
        'nama_barang' => "laptop",
        'merk' => 'lenovo'
    ],
    [
        'id_barang' => 02,
        'nama_barang' => "RAM",
        'merk' => 'lenovo'
    ],
    [
        'id_barang' => 03,
        'nama_barang' => "Keyborad",
        'merk' => 'lenovo'
    ],
);
$harga=array(
    [
        'id_barang' => 01,
        'harga' => 2000000,
    ],
    [
        'id_barang' => 02,
        'harga' => 100000,
    ]
    );


foreach ($barang as &$value) {
    $index =array_search($value['id_barang'], array_column($harga, 'id_barang'));

    if($index > -1) {
      $value['dataharga'] = array($harga[$index]); // Do you really need additional array() ?
    } else {
      $value['dataharga'] = array(['id_barang' => "", 'harga' => ""]); // Do you really need additional array() ?
    }
}

echo "<pre>";
echo print_r($barang);
  • Related