Jump to content

How do i convert a string into an array


blue-genie

Recommended Posts

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

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>";
?>

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.

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>';

}

?>

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.