SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Guru
    Join Date
    Jul 2005
    Posts
    609
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problem with Function Reading Array

    I've setup an array of catering items to read at future times (small list) in a small website. However, I'm having trouble referring to the array from within a function. Here is the jist of it:

    PHP Code:
    <?php
     session_start
    (); 
     include (
    'build_menu_array.php'); // Code that builds $catering_menu array

     
    function getprice($prodid)
     {
      return 
    $catering_menu['mexiwrap']['price_normal'];
     }

     echo 
    'Function ' .getprice($prodid) .'<br>';  // This returns nothing
     
    echo 'Manual ' .$catering_menu['mexiwrap']['price_normal'] .'<br>';  //This returns the correct price.

    ?>
    I have staticly put the item name into the function for testing. I know the array is built and working because it will output manually, and I can also print_r it and it looks fine, but the function returns zip.

    What am I missing?

  2. #2
    SitePoint Addict
    Join Date
    Aug 2004
    Location
    Chicago
    Posts
    296
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can either pass $catering_menu as a parameter to the function, or you can put "global $catering_menu;" at the top of your function.

    Your choice
    Why's (Poignant) Guide to Ruby
    learn ruby with foxes, wizards, and chunky bacon

  3. #3
    SitePoint Member
    Join Date
    Dec 2005
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I concur. I had the same problem awhile back and until I actually sat down and read the php documentation, I would try to do stuff like this too and wonder why it wasn't working. cheers.

  4. #4
    SitePoint Guru
    Join Date
    Jul 2005
    Posts
    609
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Worked like a charm, thanks


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •