Multi-dimentional Array php condition loop and variable assignment -


need figuring out. have json array going php file after submit clicked via ajax. once decoded on php side, looks this:

array

[0] => array     (         [id] => 409         [changetype] => change_seat_to         [name] => john doe         [seat] =>          [setto] => 4-2         [previousseatvalue] =>          [previousseatnewvalue] => y     )  [1] => array     (         [id] => 278         [changetype] => change_seat_to         [name] => john test         [seat] => 4-1         [setto] => 4-3         [previousseatvalue] => y         [previousseatnewvalue] => y     )  [2] => array     (         [id] => 305         [changetype] => removeseat         [name] => john blue         [seat] => 3-6         [setto] => 0     )  [3] => array     (         [id] => 314         [changetype] => change_seat_to         [name] => john red         [seat] => 3-4         [setto] => 3-6         [previousseatvalue] => y         [previousseatnewvalue] => y     ) 

main goal:

what im trying loop through arrays , if changetype matches string("change_seat_to" or "removeseat"), execute sql statement. im having issues assigning variables different key values in matching arrays during loop. here have far:

$obj = json_decode($_post['mydata'], true);   foreach ($obj $innerarray){     foreach($innerarray $key => $value){          $$key = $value;         if($value === "change_seat_to"){             echo $id;             echo $setto;           }         if($value === "removeseat"){              echo $id;             echo $setto;           }      }  } 

now set testing purposes(i have echo's displaying in console log after successful ajax). setup, able echo out id of matching elements if try other one, variable undefined. can explain why happening , offer suggestion main goal? in advance.

you need 1 foreach(), check modified code

<?php $obj = json_decode($_post['mydata'], true);  foreach ($obj $innerarray){     if($innerarray['changetype'] == 'change_seat_to') {         // stuff         $id = $innerarray['id'];         // rest of values in same way     } else if($innerarray['changetype'] == 'removeseat') {         // stuff         $id = $innerarray['id'];         // rest of values in same way     } } 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -