0

I'm struggling to get the specific item from the following array. I'd like to get the 2nd item from the 3rd, 10th, and 17th number array. If I've more item in the array then it will continuously find the 2nd item in a pattern.

However, I'm unable to get the 2nd item from using the above pattern and I am not getting any logic to get this.

What should I do in this case? Have you got any logic? How would you skip all the arrays and just return specific items?

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
        )

)
8
  • So your expected output would be: ["Script 1: Login", "Script 2: Enter...", "Script 3: Change user details"]? Commented Apr 4, 2016 at 0:32
  • Yes, you're right. Commented Apr 4, 2016 at 0:33
  • Try to do a simple foreach loop, check if your key is one of the allowed subArrays (Look at in_array() for this). And if it is, just get the second element into an array. If you get stuck, post the attempt here. Commented Apr 4, 2016 at 0:34
  • Any example would be appreciated. Commented Apr 4, 2016 at 0:44
  • Look up in_array() in the manual and try something like this (pseudo code): for each element{if($key == $whitelist){get element 2}} Commented Apr 4, 2016 at 0:46

2 Answers 2

2

SET $array as your multi dimensional array.

Loop through the 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";
    }
}
Sign up to request clarification or add additional context in comments.

8 Comments

The array_key_exists() function will check if the key exist on the array. The key 2 exist in most of the array. It will not work as how I wanted.
The array could have more items in it. We can't specifically just say [3, 10, 17].
@MdMazedulIslamKhan That is where you define which one you want
that's why we have 2 conditions on the if clause for all the items after the key '17' or whatever condition you want to have. feel free to modify it as needed.
the logic that was implemented will just get the [2] values of the keys specified in the list array. If key is not in the list, then it will check if greater than 17, and will just check if array key [2] exists. It will just pull out all [2] values after key 17. If a diff scenario is required, then just change the list array and the if condition.. it's difficult to create a logic that fit's all if your specifications is pretty random.
|
1

If you just want the 2 index (it won't be the "second" as second could be any number) from the sub-arrays then:

$twos = array_column($array, 2);

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.