SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Member
    Join Date
    Jul 2002
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Looping Through Multi-Dimensional Arrays Using Foreach

    Can you loop through a multi-dimensional array using the “foreach” function, but only make it output key/value pairs that match a specific value?

  2. #2
    SitePoint Evangelist compwizard's Avatar
    Join Date
    May 2002
    Location
    United States
    Posts
    457
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    couldn't you just do something like:
    PHP Code:
    <?
    // create array
    $starwars = array("princess" => "Leia""teacher" => "Yoda""new hope" =>
    "Luke""bad guy" => "Darth""worse guy" => "The Emperor" );

    // returns true
    echo in_array("Yoda"$starwars);

    // returns false
    echo in_array("Obi-Wan"$starwars);
    ?>
    Compwizard
    "There are 10 kinds of people in this world -- those who know binary, and those who don't."

  3. #3
    SitePoint Evangelist compwizard's Avatar
    Join Date
    May 2002
    Location
    United States
    Posts
    457
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    check out this page if you have php4:
    http://www.devshed.com/Server_Side/P...nip/print_html
    Compwizard
    "There are 10 kinds of people in this world -- those who know binary, and those who don't."

  4. #4
    ********* Member website's Avatar
    Join Date
    Oct 2002
    Location
    Iceland
    Posts
    1,238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ogoka
    Can you loop through a multi-dimensional array using the “foreach” function, but only make it output key/value pairs that match a specific value?
    Following the lead of our admired advisors , here is my reply:

    Yes you can
    - website

  5. #5
    SitePoint Member
    Join Date
    Jul 2002
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Website,

    Can you give an example not already mention above?

  6. #6
    ********* Member website's Avatar
    Join Date
    Oct 2002
    Location
    Iceland
    Posts
    1,238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hmm, not quite sure what you want but here is one example if you want to search 2 dimensional array, you simply do foreach twice
    PHP Code:
    foreach ($data as $key => $val) {
      foreach (
    $val as $subkey => $subval) {
       if (
    strcmp($subkey'something') == )
         echo (
    $subkey ' => ' $subval);
      }

    and so on, if you can say more about what you need I (or others) could give you more specific examples....

    Hope that helps!
    - website

  7. #7
    SitePoint Member
    Join Date
    Jul 2002
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Website,

    Here's an example array:

    $birds = array (
    '21064' => array('number' => '21064', 'name' => 'Porcelain Cardinals', 'price' => '19.95'),
    '21065' => array('number' => '21065', 'name' => 'Porcelain Canaries', 'price' => '19.95'),
    '21066' => array('number' => '21066', 'name' => 'Porcelain Doves', 'price' => '19.95'),
    '21681' => array('number' => '21681', 'name' => 'Porcelain Cardinal On Branch', 'price' => '9.95'),
    '21683' => array('number' => '21683', 'name' => 'Porcelain White Dove', 'price' => '17.95'),
    '22798' => array('number' => '22798', 'name' => 'Porcelain Baby Cardinals', 'price' => '14.95'),
    '22801' => array('number' => '22801', 'name' => 'Porcelain Cardinals On Branch', 'price' => '37.95'),
    '22802' => array('number' => '22802', 'name' => 'Porcelain Bluejays On Branch', 'price' => '37.95'),
    '22803' => array('number' => '22803', 'name' => 'Porcelain Doves On Branch', 'price' => '37.95'),
    '24795' => array('number' => '24795', 'name' => 'Porcelain Eagle With 2 Flags', 'price' => '29.95')
    );

    I'm trying to get the foreach loop function to show only all items with a price of $37.95.

    Thanks.

  8. #8
    Sidewalking anode's Avatar
    Join Date
    Mar 2001
    Location
    Philadelphia, US
    Posts
    2,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $keep = array(); 
    foreach(
    $birds as $bird) {
        if (
    $bird['price'] == '37.95') {
          
    $keep[] = $bird;
       }

    Last edited by anode; May 10, 2003 at 14:18.
    TuitionFree — a free library for the self-taught
    Anode Says...Blogging For Your Pleasure

  9. #9
    SitePoint Member
    Join Date
    Jul 2002
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for all your help... I figured it out!


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
  •