www.webdeveloper.com
Results 1 to 4 of 4

Thread: Best way to iterate an array

  1. #1
    Join Date
    Sep 2006
    Posts
    655

    Question Best way to iterate an array

    Hi

    Imagine we have an array like the following,

    The values, 10, 20 are the userids. I want to send text messages to my users. What is the best way to iterate through this array to get the following:

    1. userid
    2. phone type (work_phone, home_phone)
    3. mobile number




    PHP Code:
    $data = array(
        
    '10' => array(
            
    'work_phone' => array(
                
    '9988776655''7788665544'
            
    ),
            
    'home_phone' => array(
                
    '94455667733''6655447733'
            
    )
        ),
        
    '20' => array(
            
    'home_phone' => array(
                
    '3333333333''4444444444''5555555555'
            
    )
        ),
    ); 
    I am doing it in the following way, is this the best approach?

    PHP Code:
            foreach ($data as $userID => $val) {
                foreach (
    $val as $type => $key) {
                    foreach (
    $key as $number) {
                        echo 
    'User ID is: ' $userID"\n";
                        echo 
    "Mobile Type is: " $type"\n";
                        echo 
    "Mobile number is: " .  $number"\n\n\n";
                    }
                }
            } 

  2. #2
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    883
    PHP Code:
    foreach ($data as $userid => $val)
    {
          echo 
    "User $userid has: \n";
          foreach (
    $val as $type=>$nums)
          {
              echo 
    "$type is \n";
              foreach (
    $nums as $num)
                  echo 
    "$num ";
          }


  3. #3
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,633
    My interpretation:
    PHP Code:
    <?php

    $data 
    = array(
        
    '10' => array(
            
    'work_phone' => array(
                
    '9988776655''7788665544'
            
    ),
            
    'home_phone' => array(
                
    '94455667733''6655447733'
            
    )
        ),
        
    '20' => array(
            
    'home_phone' => array(
                
    '3333333333''4444444444''5555555555'
            
    )
        ),
    );
    echo 
    "<ul>\n";
    foreach(
    $data as $userId => $numbers) {
        echo 
    "<li>User # $userId\n<ul>\n";
        foreach(
    $numbers as $type => $values) {
            echo 
    "<li>".ucwords(str_replace('_'' '$type)).":\n<ul>";
            foreach(
    $values as $nbr) {
                echo 
    "<li>$nbr</li>\n";
            }
            echo 
    "</ul>\n</li>\n";
        }
        echo 
    "</li>\n</ul>\n";
    }
    echo 
    "</ul>\n";
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  4. #4
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,506
    IMHO if you have a list of people then you should be using a database to pull your data from and then have the PHP send out the emails or SMS.

    In all honesty, this smells like homework phishing.
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles