From what I understand you want to change all the values ofjsonusing the values of the array. So I created a solution that goes through all the elements of json and checks whether the 'type' attribute is contained in the index of array. If it is, change the values of json using the values and indices of array:
Commented code
$array = [
'Premium' => ['valor' => '350', 'quantidade' => '50', 'status' => '1'],
'Arena' => ['valor' => '250', 'quantidade' => '10', 'status' => '1']
];
$json = '{"3":{"tipo":"Premium","valor":"100","quantidade":"200","vendas":"0","status":"1"},"1":{"tipo":"Arena","valor":"50","quantidade":"200","vendas":"0","status":"1"}}';
$json = json_decode($json, true); // tranforma o json em array para a comparação
$keys = array_keys($array); // pega as chaves do array
// loop em cada elemento
foreach($json as $key => $val){
// se houver o indice no array que for igual ao tipo, iguala os valore do array
if(in_array($val['tipo'], $keys)){
$json[$key]['valor'] = $array[$val['tipo']]['valor']; // altera o valor
$json[$key]['quantidade'] = $array[$val['tipo']]['quantidade']; // altera a quantidade
$json[$key]['status'] = $array[$val['tipo']]['status']; // altera o status
}
}
print_r(json_encode($json));
When I change the value in the array it is not modified in json!
– João pedro
@Joãopedro sorry, it’s sleep! = ) It’s there!
– Andrei Coelho
Thank you very much!
– João pedro