duanci1939 2015-05-23 17:10
浏览 42
已采纳

PHP - 从多维数组中获取数据

Easy question ... but here it goes:

I have an array and I need to use the [0] index which represents the id of each user. But I can't seem to get it in a foreach statement.

$emails = $aTools->getUsers();

Below is a print_r of the above array '$emails'.

Array
(
[0] => Array
    (
        [0] => 72
        [1] => 
        [2] => email@yahoo.com
    )

[1] => Array
    (
        [0] => 62
        [1] => 
        [2] => email@gmail.com
    )

[2] => Array
    (
        [0] => 64
        [1] => 
        [2] => email@gmail.com
    )

[3] => Array
    (
        [0] => 66
        [1] => 
        [2] => email@yahoo.com
    )
)

This is not working

$i = 0;
foreach($emails as $email)
{
    echo $email[$i][0];
    echo "<br/>";
    $i++;
}
  • 写回答

1条回答 默认 最新

  • douhui8454 2015-05-23 17:16
    关注

    Each time u're looping u'll get in $email on the entry table :

    Array
        (
            [0] => 72
            [1] => 
            [2] => email@yahoo.com
        )
    

    Then

    Array
    (
        [0] => 62
        [1] => 
        [2] => email@gmail.com
    )
    

    ETC..

    So echo $email[$i][0] can't return the id u want.

    $email[0] CAN.

    SOLUTION :

     foreach($emails as $email)
            echo $email[0];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?