Foreach array in array

Hello,
How to write foreach for this array?

Array
(
[0] => Array
(
[urun] => canaklar
[id] => 1
[adet] => 4
)

[1] => Array
(
[urun] => mdulnbler
[id] => 4
[adet] => 1
)

[2] => Array
(
[urun] => lnbler
[id] => 1
[adet] => 3
)

[3] => Array
(
[urun] => multiswitchler
[id] => 192
[adet] => 1
)

[4] => Array
(
[urun] => multiswitchler
[id] => 186
[adet] => 1
)

[5] => Array
(
[urun] => multiswitchler
[id] => 182
[adet] => 1
)

[6] => Array
(
[urun] => multiswitch_amplifier
[id] => 7
[adet] => 2
)

[7] => Array
(
[urun] => kablolar
[id] => 1
[adet] => 2420
)

[8] => Array
(
[urun] => fkonnektorler
[id] => 4
[adet] => 230
)

)

that’s not an array, thats a print_r dump.

Yes, print_r dump

foreach ($_POST['siparis'] as $key => $value){

          echo $value."<br />";

      }

out
Array ( [urun] => canaklar [id] => 1 [adet] => 4 )
Array ( [urun] => lnbler [id] => 4 [adet] => 1 )
Array ( [urun] => lnbler [id] => 1 [adet] => 3 )
Array ( [urun] => multiswitchler [id] => 192 [adet] => 1 )
Array ( [urun] => multiswitchler [id] => 186 [adet] => 1 )
Array ( [urun] => multiswitchler [id] => 182 [adet] => 1 )
Array ( [urun] => multiswitch_amplifier [id] => 7 [adet] => 2 )
Array ( [urun] => kablolar [id] => 1 [adet] => 2420 )
Array ( [urun] => fkonnektorler [id] => 4 [adet] => 230 )

foreach ($_POST['siparis'] as $key => $value){

        foreach ($value as $siparis){

          echo $siparis."<br />";

       }

      }

Warning: Invalid argument supplied for foreach() in

I think I can’t create an array

foreach($urunler as $urun)

{
$array = array('urun'=>$urun['urun'], 'id'=>$urun['id'], 'adet'=>$urun['adet']);
?>
<input type="checkbox" name="siparis[]" value="<?php echo $array; ?>">
<?php
}

There are multiple checkboxes
I want to post the arrays in value

PHP and HTML do not have a proper, exchangeable string representation for Arrays, better use JSON

https://www.php.net/manual/en/function.json-encode.php
https://www.php.net/manual/en/function.json-decode.php

beware: for associative arrays, that will give you an object, or you use the second parameter in json_decode

I made some trials but I couldn’t

This syntax is correct?

$array = array('urun'=>$urun['urun'], 'id'=>$urun['id'], 'adet'=>$urun['adet']);

<input type="checkbox" name="siparis[]" value="<?php echo json_encode($array); ?>">

HTML
<input type=“checkbox” name=“siparis[]” value="{“urun”:“canaklar”,“id”:“1”,“adet”:“4”}">
<input type=“checkbox” name=“siparis[]” value="{“urun”:“lnbler”,“id”:“4”,“adet”:“1”}">
<input type=“checkbox” name=“siparis[]” value="{“urun”:“lnbler”,“id”:“1”,“adet”:3}">
<input type=“checkbox” name=“siparis[]” value="{“urun”:“multiswitchler”,“id”:“192”,“adet”:“1”}">


var_dump(json_decode($_POST['siparis'], true));
Warning: json_decode() expects parameter 1 to be string, array given in

If you have quotes within quotes, you must escape the inner ones, or here i would prefer to use single-quotes for the HTML attribute as the JSON string contains double-quotes. Also with siparis[] you tell it to be an array so you have to loop over $_POST['siparis'] with foreach or something

https://www.php.net/manual/en/control-structures.foreach.php

I solved the problem thank you
Single quotes instead of double quotes
<input type="checkbox" name="siparis[]" value='<?php echo json_encode($dizi); ?>'>

json_decode second foreach should be used

foreach ($_POST['siparis'] as $value){

    foreach (json_decode($value) as $siparisler => $siparis){

      echo $siparisler." : ".$siparis."<br />";

    }

  }
Sponsor our Newsletter | Privacy Policy | Terms of Service