Page 1 of 1

getting values from multi-dimensional arrays

Posted: Thu Apr 08, 2004 7:35 am
by serap
In xx.php I create a muti-dimensional array as follows:

$result = mysql_query('SELECT * FROM country');
$country = array();
while($row = mysql_fetch_assoc($result)) {
$country[$row['country_id']] = array('country'=>$row['country'],
'language'=>$row['language']);
}

and post this array with
<input type=hidden name=country value="<?php echo print_r($country); ?>" >

to the next page with post method. When I do the following :
print_r($country); - I get:

Array ( [1] => Array ( [country] => UK [language] => English ) [2] => Array ( [country] => Iran [language] => Persian ) [3] => Array ( [country] => Spain [language] => Spanish ) [4] => Array ( [country] => Germany [language] => German ) ) 1

However I have two questions:
1 - can not somehow get each ingredient, think Im lost in the syntax somehow?? F. ex how do i get all the specific values f.ex for [1] language? an so on...
2 - Is it the 'usual' way to post an array as
<input type=hidden name=country value="<?php echo print_r($country); ?>" >

thanks,

Posted: Thu Apr 08, 2004 7:40 am
by markl999
If you really want to post an array like that then you should use serialize and on the 'receiving' page use unserialize to get it back again (instead of using print_r which is just a raw dump).

Posted: Thu Apr 08, 2004 8:46 am
by serap
I do

- from posting page:

<input type=hidden name=country value="<?php echo serialize($country); ?>" >

- from receiving page:

$country_new=unserialize($country);

but print_r($country_new) is not returning anything now?

Posted: Thu Apr 08, 2004 10:19 am
by markl999
Try $country_new=unserialize($_POST['country']);