Search code examples
phparrays

How to access the second level values of a 2d array?


I don't know what to call this, but I exploded the each of the values of a pre-existing array to create another array with each of the exploded values on a new array identifier ([0],[1],etc). But for some reason it created something like this and its getting hard to handle the values.

Array (
    [0] => Array (
        [0] => [email protected]
        [1] => password1
    )
    [1] => Array (
        [0] => [email protected]
        [1] => password2
    )

How do I make each of those values have their own identifier?


Solution

  • It's a multidimensional array, don't let it scare you off, they're better to deal with than an array like this:

    Array (
        [0] => [email protected]
        [1] => password1
        [2] => [email protected]
        [3] => password2
        )
    

    What you do instead of accessing those values directly (or within your loop) is create another loop around it:

    foreach($array as $current) {
        foreach($current as $subarray) {
            list($email, $password) = $subarray;
            echo $email . ': ' . $password;
        }
    }