Array within an array

Thread Solved

Join Date: Jan 2009
Posts: 22
Reputation: EvilOrange is an unknown quantity at this point 
Solved Threads: 0
EvilOrange EvilOrange is offline Offline
Newbie Poster

Array within an array

 
0
  #1
Jul 10th, 2009
i have to access an array within an array using php, and i have very little experience working with php.

can anyone point me towards a good tutorial or show me an example of how to go about it?

eg:

[0] contians [a],[b],[c]

many thanks
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 1,402
Reputation: ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light 
Solved Threads: 225
Sponsor
ShawnCplus's Avatar
ShawnCplus ShawnCplus is offline Offline
Code Monkey

Re: Array within an array

 
0
  #2
Jul 10th, 2009
  1. $somearray = array(
  2. array('a' => 'Hello'),
  3. array('b' => 'World')
  4. );
  5.  
  6. echo $somearray[0]['a'] . ' ' . $somearray[1]['b'];
  7. // Hello World
GCS d- s+ a-->? C++(++++) UL+++ P+>+++ L+++ E--- W+++
N+ o K w++(---) O? !M- V PS+>++ PE+ Y+ PGP !t- 5? X- R tv+
b+>++ DI+ D G++>+++ e+ h+>++ r y+
PMs asking for help will not be answered, post on the forums. That's what they're there for.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 165
Reputation: Fest3er is an unknown quantity at this point 
Solved Threads: 18
Fest3er Fest3er is offline Offline
Junior Poster

Re: Array within an array

 
0
  #3
Jul 10th, 2009
  1. <?php
  2. // Set array parts
  3. $a = array("1" => "one", "2" => "two");
  4. $b = array("a" => "aye", "b" => "bee");
  5. $c = array("a scalar", $a, $b);
  6.  
  7. // Extract a sub-element from an array in the array
  8. $d = $c[1][2];
  9.  
  10. // Extract a sub-array from the array
  11. $e = $c[2];
  12.  
  13. // Display the various demonstration bits
  14. print "<pre>\n\$c[2] = ";
  15. print_r ($c[2]);
  16. print "</pre>\n\n";
  17. print "\$c[2]['b'] = {$c[2]['b']}<br>\n\n";
  18. print "<pre>\n\$c[1] = ";
  19. print_r ($c[1]);
  20. print "</pre>\n\n";
  21. print "\$d = $d<br>\n\n";
  22. print "<pre>\n\$e = ";
  23. print_r ($e);
  24. print "</pre>\n\n";
  25. print "\$c[0] = $c[0]<br>\n";
  26. ?>

Watch the syntax when printing. PHP's parser is a little dicey in that situation; arrays often need to be braced when printing directly.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 22
Reputation: EvilOrange is an unknown quantity at this point 
Solved Threads: 0
EvilOrange EvilOrange is offline Offline
Newbie Poster

Re: Array within an array

 
0
  #4
Jul 10th, 2009
thanks for the advice

so using the first example:

if [0] had [a][b][c]

all i'd need to do to access an array and assign the contents to vars would be (using first example):

  1. $avar = $somearray[0]['a'];
  2. $bvar = $somearray[1]['b'];
  3.  
  4. echo $avar;
  5. echo $bvar;
  6.  
  7. // Hello
  8. // World
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 1,402
Reputation: ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light 
Solved Threads: 225
Sponsor
ShawnCplus's Avatar
ShawnCplus ShawnCplus is offline Offline
Code Monkey

Re: Array within an array

 
0
  #5
Jul 10th, 2009
Originally Posted by EvilOrange View Post
thanks for the advice

so using the first example:

if [0] had [a][b][c]

all i'd need to do to access an array and assign the contents to vars would be (using first example):

  1. $avar = $somearray[0]['a'];
  2. $bvar = $somearray[1]['b'];
  3.  
  4. echo $avar;
  5. echo $bvar;
  6.  
  7. // Hello
  8. // World
Precisely. If you want to get really deep into how arrays work take a look at the language documentation. http://php.net/arrays
GCS d- s+ a-->? C++(++++) UL+++ P+>+++ L+++ E--- W+++
N+ o K w++(---) O? !M- V PS+>++ PE+ Y+ PGP !t- 5? X- R tv+
b+>++ DI+ D G++>+++ e+ h+>++ r y+
PMs asking for help will not be answered, post on the forums. That's what they're there for.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC