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
$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?


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

That’s not going to work; looks like it will repeat data for the same user several times before moving to the next. Remember, you’re iterating through a multidimensional array here

Hmm, so what could be the best possible way of achieving this?

Thanks in advance

PS: Edit: Please check this code: http://codepad.org/eX7Wxl44

This should work



   foreach ($data as $userID => $val) {

	echo 'User ID is: ' . $userID, "<br>";

            foreach ($val as $type => $key) {

            	echo "Mobile Type is: " . $type, "<br>";

                foreach ($key as $number) {                    
                    echo "Mobile number is: " .  $number, "<br><br>";
                }
            }
        } 


Thanks arout77 for your answer, but whats wrong with my approach? Did u see the link I posted? It also works

It works, but it will give a completely different output than the above. The code I posted above should put out something like:


User ID is: 10
Mobile Type is: work_phone
Mobile number is: #########
Mobile number is: #########
Mobile Type is: home_phone
Mobile number is: #########
Mobile number is: #########

User ID is: 20
Mobile Type is: work_phone
Mobile number is: #########
Mobile number is: #########
Mobile Type is: home_phone
Mobile number is: #########
Mobile number is: #########

I assume that is the result you were going after? If your current method is the way you wanted it, then I’m really not sure what you are asking