George
George

Reputation: 2493

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?

Upvotes: 0

Views: 51

Answers (2)

user2672373
user2672373

Reputation:

Are you asking about foreach? If so you can try

foreach($arrOut as $arrIn) {
    foreach($inArr as $key => $val)
        echo $key . ': ' . $val . PHP_EOL;
}

Upvotes: 1

scrowler
scrowler

Reputation: 24406

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;
    }
}

Upvotes: 2

Related Questions