I have 2 json data from nosql. first match if the search word match in $array1, get the item number then put item number into $array2, get the price of custom search require. But my code cause Invalid argument supplied for foreach() in
foreach($json2[$num] as $data2)
$str = 'paper';
$array1 = '[{"a":"1","b":"book"},{"a":"2","b":"paper"}]';
$array2 = '[{"1":["17.00","22.00"]},{"2",["4.50","6.00"]}]';
$json1 = json_decode($array1);
$json2 = json_decode($array2,true);
foreach($json1 as $data1){
if(preg_match('#'.$data1->b.'#',$str,$match)){
$num = $data1->a; // $num = 2
}
}
foreach($json2[$num] as $data2){
foreach($data2 as $newdata){
echo $newdata.'<br />'; // 4.50, 6.00
}
}
First off your JSON for
$array2is not valid. It should be:Your JSON is actually an array of objects (arrays).
var_dump($json2);shows this.You’re gonna need to loop over it like this: