Array Clarification

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
swetha
Forum Commoner
Posts: 88
Joined: Wed Sep 10, 2008 11:00 pm

Array Clarification

Post by swetha »

Code: Select all

 
$gdesc[$i] = explode("--",$all[$i]); 
$sdesc[$i] = explode("-",$gdesc[$i][0]);
 

Consider this line $gdesc[$i] = explode("--",$all[$i]).In the second line,is it correct to give
gdesc[$i][0],since gdesc[$i] is one dimensional array and gdesc[$i][0] is 2 dimensional array.
User avatar
novice4eva
Forum Contributor
Posts: 327
Joined: Thu Mar 29, 2007 3:48 am
Location: Nepal

Re: Array Clarification

Post by novice4eva »

it is perfectly CORRECT
swetha
Forum Commoner
Posts: 88
Joined: Wed Sep 10, 2008 11:00 pm

Re: Array Clarification

Post by swetha »

how is that?
$gdesc[$i] = explode("--",$all[$i]);
$sdesc[$i] = explode("-",$gdesc[$i][0]);

$gdesc[$i] is one dimensional array but
$gdesc[$i][0] is 2 dimensional array.Then how come this works?thanks for help
User avatar
Ziq
Forum Contributor
Posts: 194
Joined: Mon Aug 25, 2008 12:43 am
Location: Russia, Voronezh

Re: Array Clarification

Post by Ziq »

After this line execute

Code: Select all

 
$gdesc[$i] = explode("--",$all[$i]);
 
$gdesc[$i] is 2 dimensional array. Because explode() function return array value and this one assign to 1 dimensional array $gdesc[$i].

Same happens if you try this

Code: Select all

 
// 1 dimensional array
$array[0] = 1;
$array[0] = array('2 dimensional');
//  Now in $array is 2 dimensional array
echo $array[0][0]; //  print: 2 dimensional
 
Read about explode() and Arrays.
User avatar
novice4eva
Forum Contributor
Posts: 327
Joined: Thu Mar 29, 2007 3:48 am
Location: Nepal

Re: Array Clarification

Post by novice4eva »

if you do

Code: Select all

 
echo '<pre>';
print_r($gdesc);
echo '</pre>';
 
it will give you the hierarchical order, it always helps before drowning when things go really deeper in array
Post Reply