blue-genie Posted August 14, 2009 Share Posted August 14, 2009 flash is sending some variables to php. $gameID = $_REQUEST['gameID'];//gets this as 1, 2 etc $arr = $_GET['arr'];//gets this as i1=Symbol=Octopus222,Odds=2,Prize=puss&i2=Symbol=Lobster222,Odds=2,Prize=puss i need to convert this to an array. i tried $data = array($arr); didn't work. if i echo $data i get Array Link to comment https://forums.phpfreaks.com/topic/170298-how-do-i-convert-a-string-into-an-array/ Share on other sites More sharing options...
Daniel0 Posted August 14, 2009 Share Posted August 14, 2009 Split it up by comma. Iterate through these and split each up by the equals sign. Link to comment https://forums.phpfreaks.com/topic/170298-how-do-i-convert-a-string-into-an-array/#findComment-898347 Share on other sites More sharing options...
MatthewJ Posted August 14, 2009 Share Posted August 14, 2009 Like Daniel0 said <?php $arr = "i1=Symbol=Octopus222,Odds=2,Prize=puss&i2=Symbol=Lobster222,Odds=2,Prize=puss"; $data = explode(",", $arr); foreach($data as $k => $v) { $data[$k] = explode("=", $v); } echo "<pre>"; print_r($data); echo "</pre>"; ?> Link to comment https://forums.phpfreaks.com/topic/170298-how-do-i-convert-a-string-into-an-array/#findComment-898352 Share on other sites More sharing options...
blue-genie Posted August 14, 2009 Author Share Posted August 14, 2009 Matthew can you help me out here please. I'm going around in circles i need to loop through the array and do an insert. if there's an error i do an echo '<?xml version="1.0"?>'; echo '<dataxml>'; die("There's little problem: ".mysql_error()); $MyError = "An error occured while saving data. Please try again later." + mysql_error(); echo "<sqlError>".$MyError."</sqlError>"; echo '</dataxml>'; once all the inserts are done i need to echo another bit of xml to say it's done. currently the insert never happens and i'm not sure where to put the success message since it's in a loop and i end up with mailformed xml. Link to comment https://forums.phpfreaks.com/topic/170298-how-do-i-convert-a-string-into-an-array/#findComment-898376 Share on other sites More sharing options...
blue-genie Posted August 14, 2009 Author Share Posted August 14, 2009 okay am i even remotely close (This gives me an <b>Warning</b>: Invalid argument supplied for foreach() in <b>C:\xampp\htdocs\... error) when testing in browser <?php session_start(); include 'config.php'; include 'opendb.php'; $gameID = $_REQUEST['gameID']; $arr = $_REQUEST['arr']; $data = explode(",", $arr); foreach($data as $k => $v) { $data[$k] = explode("=", $v); foreach($raw[$key] as $value){ $info = explode('=', $value); $data[$key][$info['0']] = $info['1']; } } echo '<?xml version="1.0"?>'; echo '<dataxml>'; if ( is_array( $data ) ){ $n = count($data); for ( $i = 1; $i < $n; $i++ ){ $key = 'i'.$i; // Read the various keys, you should know what to expect! // if( !empty($data[$key]['Symbol'])){ $insert = mysql_query('INSERT INTO gameprizes(name, prize, odds, gameID) VALUES("'.$data[$key]['Symbol'].'","'.$data[$key]['Odds'].'", "'.$data[$key]['Prize'].'", "'.$gameID.'")'); } } } if (!$insert) { die("There's little problem: ".mysql_error()); $MyError = "An error occured while saving data. Please try again later." + mysql_error(); echo "<sqlError>".$MyError."</sqlError>"; } else { echo '</dataxml>'; } ?> Link to comment https://forums.phpfreaks.com/topic/170298-how-do-i-convert-a-string-into-an-array/#findComment-898403 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.