'php handle curly array with foreach
i am trying to handle curly {} array with php and foreach. i have this array:
$_POST = json_decode(file_get_contents("php://input"),true);
...
...
...
"son_options": {"2": "3", "7": "5", "10": "9", "6": "22"}
i am trying to do this:
foreach($_POST["variants"] as $data):
$itemOptionsArr["itemid"]=$itemid;
$itemOptionsArr["optionid"]=$data["optionid"];
$itemOptionsArr["valueid"]=$data["optionvalueid"];
$itemOptionsArr["price"]=$data["i_vprice"];
$itemOptionsArr["pic"]=$data["i_photo"];
$itemOptionsArr["quantity"]=$data["i_quantity"];
if($data["son_options"]){
foreach($data["son_options"] as $data2):
$itemOptionsArr["optionid"]= [0]2 or [1]7.....
$itemOptionsArr["valueid"]= [0]3 or [1]5....
$itemoptionsid=setItemOptions($itemOptionsArr);
endforeach;
}
$itemoptionsid=setItemOptions($itemOptionsArr);
endforeach;
i am trying to get the key and the value as parameters for example: "2":"3"
$par1 = 2; $par2=3;
any help please?
Solution 1:[1]
ok i found the solution...it's easy...
foreach($data["son_options"] as $key=>$val):
$itemOptionsArr["optionid"]= $key;
$itemOptionsArr["valueid"]= $val;
$itemoptionsid=setItemOptions($itemOptionsArr);
endforeach;
thank you all.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Mikha Matta |
