从多维数组中获取特定项

时间:2016-04-04 00:27:54

标签: php arrays multidimensional-array logic

我很难从以下数组中获取特定项目。我想从第3,第10和第17个数字数组中获得第2项。如果我在数组中有更多项目,那么它将继续在模式中找到第二项。

然而,我无法使用上述模式获得第二项,我没有得到任何逻辑来获得这个。

在这种情况下我该怎么办?你有什么逻辑吗?您如何跳过所有数组并返回特定项?

Array
(
    [1] => Array
        (
            [2] => TAB1
        )

    [2] => Array
        (
            [1] => Blah
            [2] => SCRIPT DISCRIPTION: <User is trying to accomplish some goal>
            [3] => Step #
            [4] => STEPS
            [5] => EXPECTED RESULT
            [6] => PASS/FAIL
            [7] => Comments
            [8] => QA
            [9] => Date
        )

    [3] => Array
        (
            [1] => Jim
            [2] => Script 1: Login
            [4] => Preconditions:  (Use of hyperlink to Preconditions/Setup tab)
            [6] => N/A
            [8] => Beth
            [9] => 7/1/2015
        )

    [4] => Array
        (
            [3] => 1
            [4] => Log on to the Health view.
            [6] => Pass
        )

    [5] => Array
        (
            [3] => 2
            [4] => Click the Visit tab.
            [6] => Pass
        )

    [6] => Array
        (
            [3] => 3
            [4] => Click the Daily Log side-tab.
            [5] => The Visit Log defaults to the current date. A list of any visits you have already entered for current date appears.
            [6] => Fail
        )

    [7] => Array
        (
            [3] => 4
            [4] => Type a student's name into the Name/ID field. For example, Aldred, Allison.
            [5] => Verify the name you entered exists in the database (you can select it from the list).
            [6] => Pass
        )

    [8] => Array
        (
            [3] => 5
            [4] => Click Add button.
            [5] => The New Health Log page appears and the Name field is prepopulated with the student's name
            [6] => Fail
        )

    [9] => Array
        (
            [2] => skip
        )

    [10] => Array
        (
            [1] => Ted
            [2] => Script 2: Enter a Visit for a Student on the Visit/ Daily Log page
            [4] => Preconditions:  (Use of hyperlink to Preconditions/Setup tab)
            [8] => Tom
            [9] => 7/2/2015
        )

    [11] => Array
        (
            [3] => 1
            [4] => Log on to the Health view.
            [6] => Blocked
        )

    [12] => Array
        (
            [3] => 2
            [4] => Click the Visit tab.
        )

    [13] => Array
        (
            [3] => 3
            [4] => Click the Daily Log side-tab.
            [5] => The Visit Log defaults to the current date. A list of any visits you have already entered for current date appears.
        )

    [14] => Array
        (
            [3] => 4
            [4] => Type a student's name into the Name/ID field. For example, Aldred, Allison.
            [5] => Verify the name you entered exists in the database (you can select it from the list).
        )

    [15] => Array
        (
            [3] => 5
            [4] => Click Add button.
            [5] => The New Health Log page appears and the Name field is prepopulated with the student's name
        )

    [16] => Array
        (
            [2] => skip
        )

    [17] => Array
        (
            [1] => James
            [2] => Script 3: Change user details
            [4] => Preconditions:  (Use of hyperlink to Preconditions/Setup tab)
            [8] => Jen
            [9] => 7/3/2015
        )

    [18] => Array
        (
            [3] => 1
            [4] => Log on to the Health view.
            [6] => Skip
        )

    [19] => Array
        (
            [3] => 2
            [4] => Click the Visit tab.
            [6] => Skip
        )

    [20] => Array
        (
            [3] => 3
            [4] => Click the Daily Log side-tab.
            [5] => The Visit Log defaults to the current date. A list of any visits you have already entered for current date appears.
        )

    [21] => Array
        (
            [3] => 4
            [4] => Type a student's name into the Name/ID field. For example, Aldred, Allison.
            [5] => Verify the name you entered exists in the database (you can select it from the list).
            [6] => Pass
        )

    [22] => Array
        (
            [3] => 5
            [4] => Click Add button.
            [5] => The New Health Log page appears and the Name field is prepopulated with the student's name
            [6] => Skip
        )

)

2 个答案:

答案 0 :(得分:2)

$array设为您的多维数组。

循环遍历数组

$list = array( '3', '10', '17');
foreach($array as $key => $a) {
    if ( in_array($key, $list) || ($key > 17 && array_key_exists('2', $a)) ) {
        // You can get the value in here
        echo "The 'specific' element in the array";
    }
}

答案 1 :(得分:1)

如果您只想从子阵列获得2个索引(它不会是&#34;第二个&#34;第二个可能是任何数字):

$twos = array_column($array, 2);

foreach($twos as $value) {
    echo $value;
}