Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old July 2nd, 2003, 05:24 PM
Registered User
 
Join Date: Jul 2003
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default reading data from multidimensional array

i am trying to read info from a multidimensional array but can't seem to get anything from it. Here is a snippet of the code i'm using:

$pictures = array (
    array ("dodder", "All from the Dodder"),
    array ("drawings", "Drawings of Wildlife"),
    array ("garbage", "Stuff and more stuff")
);

echo "$pictures[2][1]";


Now this should (i thought) echo out "Stuff and more stuff" but instead it the browser says 'Array[1]' Any idea what i'm doing wrong? Thanks.

Reply With Quote
  #2 (permalink)  
Old July 2nd, 2003, 06:44 PM
Friend of Wrox
Points: 2,570, Level: 21
Points: 2,570, Level: 21 Points: 2,570, Level: 21 Points: 2,570, Level: 21
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: San Diego, CA, USA
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Okay, you're doing a few things wrong.

First of all, you don't HAVE any numerical indexes defined in your inner arrays. You only have the string indexes "dodder", "drawings", and "garbage".

2nd of all, the fact that you're trying to access index 1 in an array that only contains one item implies that you believe PHP arrays are 1-indexed. They're not. The first automatically assigned numerical index in an array is 0.

The $pictures[2][1] should most likely be $pictures[2]['garbage'].

Lastly, you've enclosed $pictures[2][1] in a string. PHP performs variable substitution within double-quoted strings up to a point; it doesn't handle nested arrays.

What's happening is that PHP sees "$pictures[2]" and tries to substitute THAT value into the string, then prints out the "[1]" after it. Since $pictures[2] is an array, PHP just substitutes the string "Array" for it's value.

Since you're not echoing anything else, just drop the quotes:

echo $pictures[2]['garbage'];

If you need the quotes, use curly-brace syntax for nested arrays:

echo "The item is {$pictures[2]['garbage']}.";

For more info on string parsing, read the manual at:
    http://www.php.net/types.string#lang....syntax.double


You should also make heavy use of the print_r() and/or var_dump() functions. They display the contents of your variables, including all key/value pairs in an array.

For example:

Code:
// using your $pictures from above:

echo "<pre>\n";
print_r($pictures);
echo "</pre>\n";
?>
would result in something like the following output:

Code:
Array:
(
   [0]  =>  Array:
      (
          [dodder]  =>  "All from the Dodder"
      )
   [1]  =>  Array:
      (
          [drawings]  =>  "Drawings of Wildlife"
      )
   [2]  =>  Array:
      (
          [garbage]  =>  "Stuff and more stuff"
      )
)

Hope this helps!



Take care,

Nik
http://www.bigaction.org/
Reply With Quote
  #3 (permalink)  
Old July 3rd, 2003, 04:40 AM
Registered User
 
Join Date: Jul 2003
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Nik,

thanks for the reply. the code i gave was just a small part of a huge array, so referring to the elements by name wasn't practical. i ended up solving the problem by simply putting braces around the the string. so, instead of:

echo "$pictures[2][1]";

I used:

echo "{$pictures[2][1]}";

And it displayed the page just as i needed. The first half of the array was a .jpg name and the second half was a caption. it ended up mkaing a brilliant photo album. Thanks for your help though - very appreciated.:D
Reply With Quote
  #4 (permalink)  
Old July 3rd, 2003, 12:36 PM
Friend of Wrox
Points: 2,570, Level: 21
Points: 2,570, Level: 21 Points: 2,570, Level: 21 Points: 2,570, Level: 21
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: San Diego, CA, USA
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Again, if you aren't echoing anything else out within your double-quoted string, there's really no reason to enquote your variable. When PHP sees the double quoted string, it goes into string-parsing mode. When it sees a { followed by a $, it drops back into variable parsing mode until it sees the }.

The whole thing could simply be rewritten as:

  echo $pictures[2][1];

And PHP won't incur the tiny additional processing overhead as the original version. It's not a big deal, I know, but the simpler version is (imho) much easier to read.

Either way, I'm glad it's working!


Take care,

Nik
http://www.bigaction.org/
Reply With Quote
  #5 (permalink)  
Old April 1st, 2007, 06:42 PM
Registered User
 
Join Date: Apr 2007
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I am having a similar problem of extracting values from an array that was parsed from an XML file...perhaps someone can point me in the right direction.

I have a parser put together that outputs the array below. What I am having problems with is getting the values into php friendy variables so I can display the content. I am also including a "work in progress" piece of code where I am trying to loop through the array to get at the values...but I am stuck on the right format to out put the content of the array!

Array output of XML feed:

Code:
 
Array
(
    [SPECIALS] => Array
        (
            [SPECIAL] => Array
                (
                    [0] => Array
                        (
                            [ID] => 2114
                            [TITLE] => COZUMEL - Allegro Cozumel ~ July 1 - August 31, 2007
                            [THUMBNAIL] => http://www.mysite.com/upload/file_16183.jpg
                            [SALESEND] => Apr 03, 2007
                            [ADVNIGHTS] => 5
                            [LOCALEID] => 322
                            [LOCALENAME] => Cozumel
                            [ADVERTISEDPRICE] => 949.00
                            [TRAVELDATESINFO] => July 1 - August 31, 2007
                            [ADDITIONALINFO] => All Inclusive ~ Kids Stay & Eat Free
                        )

                    [1] => Array
                        (
                            [ID] => 2113
                            [TITLE] => COZUMEL - Allegro Cozumel ~ May 1 - June 30 & September 1 - October 31, 2007
                            [THUMBNAIL] => http://www.mysite.com/upload/file_16183.jpg
                            [SALESEND] => Apr 03, 2007
                            [ADVNIGHTS] => 5
                            [LOCALEID] => 322
                            [LOCALENAME] => Cozumel
                            [ADVERTISEDPRICE] => 879.00
                            [TRAVELDATESINFO] => May 1 - Jun 30 & Sep 1 - Oct 31, 2007
                            [ADDITIONALINFO] => All Inclusive ~ Kids Stay & Eat Free
                        )

                )

        )

)

Get the file contents of the array: This is where I am having a problem?

Code:
 

// Get the file contents 
$myar=getXmlData2($contents); 
for($i=0;$i<=count($myar[SPECIALS][SPECIAL]);$i++) { 
// Here, we want to read the TITLE, SUMMARY, and THUMBNAIL etc. of the XML feed. 

$id = $myar[ID][$i]; 
$title = $myar[TITLE][$i]; 
$thumbnaillink = $myar[THUMBNAIL][$i]; 
$saleends = $myar[SALESEND][$i]; 
$advnights = $myar[ADVNIGHTS][$i]; 
$localeid = $myar[LOCALEID][$i]; 
$localename = $myar[LOCALENAME][$i]; 
$adverpprice = $myar[ADVERTISEDPRICE][$i]; 
$pricedoubleroom = $myar[TRAVELDATESINFO][$i]; 
$salesend = $myar[ADDITIONALINFO][$i]; 
$summary = $myar[SUMMARY][$i]; 

//echo " <b>$id</b> $title $summary <br><br>"; 
} 
?>
Any suggestions would be appreciated!

Thanks,

Neal



Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
multidimensional array help for a newbie grahama PHP Databases 1 May 13th, 2008 09:41 AM
Multidimensional Array macsham Beginning PHP 2 February 24th, 2005 03:30 PM
Multidimensional array in ch9 willburke BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 0 November 2nd, 2004 04:44 PM
Efficient implementation of multidimensional array Utwig C# 2 October 26th, 2004 04:34 AM
Multidimensional Array help grahama Beginning PHP 0 June 12th, 2004 09:01 PM



All times are GMT -4. The time now is 11:24 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.