从多维数组中获取特定项目

Get specific item from a multidimensional array

我正在努力从以下数组中获取特定项目。我想从第 3 个、第 10 个和第 17 个数字数组中获取第 2 个项目。如果我在数组中有更多项目,那么它会不断地找到模式中的第二个项目。

但是,我无法使用上述模式获得第二项,而且我没有任何逻辑来获得它。

遇到这种情况怎么办?你有什么逻辑吗?您将如何跳过所有数组并仅跳过 return 个特定项目?

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
        )

)

SET $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";
    }
}

如果您只想要子数组中的 2 索引(它不会是 "second",因为第二个可以是任何数字)然后:

$twos = array_column($array, 2);

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