943,568 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 2032
  • PHP RSS
Jul 10th, 2009
0

Array within an array

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
EvilOrange is offline Offline
30 posts
since Jan 2009
Jul 10th, 2009
0

Re: Array within an array

php Syntax (Toggle Plain Text)
  1. $somearray = array(
  2. array('a' => 'Hello'),
  3. array('b' => 'World')
  4. );
  5.  
  6. echo $somearray[0]['a'] . ' ' . $somearray[1]['b'];
  7. // Hello World
Sponsor
Reputation Points: 520
Solved Threads: 268
Code Monkey
ShawnCplus is offline Offline
1,564 posts
since Apr 2005
Jul 10th, 2009
0

Re: Array within an array

php Syntax (Toggle Plain Text)
  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.
Reputation Points: 51
Solved Threads: 35
Posting Whiz in Training
Fest3er is offline Offline
238 posts
since Aug 2007
Jul 10th, 2009
0

Re: Array within an array

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):

PHP Syntax (Toggle Plain Text)
  1. $avar = $somearray[0]['a'];
  2. $bvar = $somearray[1]['b'];
  3.  
  4. echo $avar;
  5. echo $bvar;
  6.  
  7. // Hello
  8. // World
Reputation Points: 10
Solved Threads: 0
Light Poster
EvilOrange is offline Offline
30 posts
since Jan 2009
Jul 10th, 2009
0

Re: Array within an array

Click to Expand / Collapse  Quote originally posted by EvilOrange ...
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):

PHP Syntax (Toggle Plain Text)
  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
Sponsor
Reputation Points: 520
Solved Threads: 268
Code Monkey
ShawnCplus is offline Offline
1,564 posts
since Apr 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Standard Error Showing
Next Thread in PHP Forum Timeline: BCC Email Action





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC