getting values from multi-dimensional arrays

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
serap
Forum Commoner
Posts: 31
Joined: Thu Jan 15, 2004 5:10 pm

getting values from multi-dimensional arrays

Post 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,
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post 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).
serap
Forum Commoner
Posts: 31
Joined: Thu Jan 15, 2004 5:10 pm

Post 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?
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Try $country_new=unserialize($_POST['country']);
Post Reply