There is an array of $ arQuestion ["ANSWERS"] inside of which there is a PERSENT, how can I replace it with the values ​​from the array $ persent?

foreach($arQuestion["ANSWERS"] as $k=>$v){ if($v["PERCENT"])array_replace ($arQuestion["ANSWERS"][$k]["PERCENT"], $persent[$k]); } 

I print the pre-array $ arQuestion ["ANSWERS"]

 Array ( [40] => Array ( [CHANNEL_ID] => 1 [PERCENT] => 33 [BAR_PERCENT] => 100 ) [38] => Array ( [CHANNEL_ID] => 1 [PERCENT] => 17 [BAR_PERCENT] => 50 ) [39] => Array ( [CHANNEL_ID] => 1 [PERCENT] => 17 [BAR_PERCENT] => 50 ) ) Array ( [42] => Array ( [CHANNEL_ID] => 1 [PERCENT] => 33 [BAR_PERCENT] => 100 ) [43] => Array ( [CHANNEL_ID] => 1 [PERCENT] => 17 [BAR_PERCENT] => 50 ) [41] => Array ( [CHANNEL_ID] => 1 [PERCENT] => 17 [BAR_PERCENT] => 50 ) ) 
  • one
    foreach($arQuestion["ANSWERS"] as $k => &$v) { if($v["PERCENT"]) $v["PERCENT"] = $persent[$k]; } foreach($arQuestion["ANSWERS"] as $k => &$v) { if($v["PERCENT"]) $v["PERCENT"] = $persent[$k]; } - splash58
  • how to fix the code? - ChromeChrome
  • What does $ persent look like? - splash58
  • Array ([0] => 50 [1] => 25 [2] => 25) Array ([0] => 40 [1] => 40 [2] => 20) - ChromeChrome
  • one
    foreach($arQuestion["ANSWERS"] as $k => &$v) { if($v["PERCENT"]) $v["PERCENT"] = array_shift($persent); } foreach($arQuestion["ANSWERS"] as $k => &$v) { if($v["PERCENT"]) $v["PERCENT"] = array_shift($persent); } - splash58

1 answer 1

If the indices of the arrays are the same, then:

 foreach($arQuestion["ANSWERS"] as $k=>$v){ if (isset($v['PERSENT'])) $arQuestion["ANSWERS"][$k]["PERSENT"] = $persent[$k]; } 
  • no brackets. error in the code. - Naumov
  • @Naumov code has no syntax errors. Not sure about something, don't give valuable comments. - iosp
  • the error is it is not syntactic. - Naumov
  • $ - skipped notice, no parentheses needed. - iosp
  • Argue unnecessary brackets, link off sources why they are not needed? - Naumov