1

How can i itarate this array

array(2) {
  [0]=>
  array(1) {
    [0]=>
    array(14) {
      [0]=>
      string(17) "ratosk8@censored"
      [1]=>
      string(23) "alokkumar.censored"
      [2]=>
      string(24) "uuleticialima1@censored"
      [3]=>
      string(23) "camera.clicks@censored"
      [4]=>
      string(24) "billthailand@censored"
      [5]=>
      string(17) "v.golev@censored"
      [6]=>
      string(22) "flipe.lost@censored"
      [7]=>
      string(25) "notherdirtybird@censored"
      [8]=>
      string(21) "booktiphani@censored"
      [9]=>
      string(32) "opinion?jazzantoledo@censored"
      [10]=>
      string(25) "skateforemerica@censored"
      [11]=>
      string(28) "blockdymezmagazine@censored"
      [12]=>
      string(17) "and6451@censored"
      [13]=>
      string(22) "flipe.lost@censored"
    }
  }
  [1]=>
  array(1) {
    [0]=>
    array(14) {
      [0]=>
      string(17) "ratosk8@censored"
      [1]=>
      string(23) "alokkumar.jsr@censored"
      [2]=>
      string(24) "[email protected]"
      [3]=>
      string(23) "[email protected]"
      [4]=>
      string(24) "[email protected]"
      [5]=>
      string(17) "[email protected]"
      [6]=>
      string(22) "[email protected]"
      [7]=>
      string(25) "[email protected]"
      [8]=>
      string(21) "[email protected]"
      [9]=>
      string(32) "[email protected]"
      [10]=>
      string(25) "[email protected]"
      [11]=>
      string(28) "[email protected]"
      [12]=>
      string(17) "[email protected]"
      [13]=>
      string(22) "[email protected]"
    }
  }
}
3
  • 4
    I'd be really mad if one of those was my e-mail... Commented Nov 7, 2009 at 20:22
  • 1
    Which helps until someone looks at the edit history. Commented Nov 7, 2009 at 20:29
  • @Chris: right – I’ve flagged this for moderator attention. Commented Nov 7, 2009 at 21:01

5 Answers 5

7

You could use a foreach inside another foreach:

foreach ($array as $item) {
    foreach ($item[0] as $email) {
        // …
    }
}

Note that I used $item[0] instead of just $item.

You could also use a function to flatten that multidimensional array and then ireate it with a single foreach:

function array_flatten($array) {
    if (!is_array($array)) {
        return false;
    }
    $result = array();
    $stack = $array;
    $len = count($stack);
    while ($len) {
        $val = array_shift($stack);
        --$len;
        if (is_array($val)) {
            foreach ($val as $key => $val) {
                if (is_array($val)) {
                    array_unshift($stack, $val);
                    ++$len;
                } else {
                    $result[] = $val;
                }
            }
        } else {
            $result[] = $val;
        }
    }
    return $result;
}
Sign up to request clarification or add additional context in comments.

2 Comments

@danieltalsky: Note that I used $item[0].
noted. sorry about that. it won't let me change my vote... you might want to edit and make this clearer to the submitter
5

I would create a function and pass it into array_walk_recursive

function spam_someone($value, $key)
{
    $email=$value;
    send_evil_spam($email);
 }

 array_walk_recursive($people_to_spam, 'spam_someone');

Alternatively, you could use a RecursiveIteratorIterator to iterate sequentially over a RecursiveArrayIterator. They're poorly documented, but I believe the code would look like this:

//iterate over the array using recursion as in les' answer (doesn't gain much)
$array_iter=new RecursiveArrayIterator($people_to_spam);

//Ahh, here we go, this will let us iterate over the leaves in a sequential manner
$iter_iter=new RecursiveIteratorIterator($array_iter);
foreach($iter_iter as $email)
{
    send_evil_spam($email)
}

I find these two solutions to be the cleanest and most readable. If the array is only ever going to be 3 levels deep though, I might just hard code that. If I didn't know about either of these, I would just write my own recursive function to do it (as in les' answer).

Comments

1

Maybe a bit more flexible solution is Recursion:

<?php

$a = array();
$a[] = array( array( "a", "b") );
$a[] = array( array( array("c", "d"), array("e", "f"), array("g", "h")));


print_arr($a);

function print_arr($obj) {
    foreach($obj as $k => $v) {
        if(is_array($v)) {
            print_arr($v);
        }else{
            echo $v ."<br />";
        }
    }
}

Useless fact: my first recursive function that doesn't have a stop condition but a go condition.

Comments

0

Well, what you have here is a two-element array, where each element contains a one-element array, that contains the arrays you want to iterate over.

// This gets us the two seperate arrays
foreach ($root_array as $2nd_level_array) {

  // But now, we still have one more one-element array in each...
  // we could iterate, but since there's only one element, 
  // we can just grab that element using array_pop()
  $email_address_array = array_pop($2nd_level_array)

  // now we can iterate over each of the two lists
  foreach ($email_address_array as $email_address) {

    // process email address

  }

}

Comments

0

The question is not too specific about how to iterate the array, and since it's a multidimensional array there are several ways to do it.

Below i outline the two ways i see would make the most sense to me.

First off i travel through the array listing items sequential.

In my second attempt i travel through the array grouping the second level items according to key.

$a = array(
    0 => array(
    0 => array(
        0 => "test0-0-0",
        1 => "test0-0-1"
    )
    ),
    1 => array(
    0 => array(
        0 => "test1-0-0",
        1 => "test1-0-1"
    )
    )
);

echo "Method 1\n";
foreach ($a as $array) {
    foreach ($array[0] as $item) {
    echo "$item\n";
    }
}
echo "Method 2\n";
$a0 = $a[0][0];
$a1 = $a[1][0];
if (count($a0) == count($a1)) {
    for ($i=0;$i<count($a0);++$i) {
    echo $a0[$i]."\n";
    echo $a1[$i]."\n";
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.