2016-04-04 19 views
0

Ich kämpfe um das bestimmte Element aus dem folgenden Array zu bekommen. Ich möchte den 2. Gegenstand vom 3., 10. und 17. Nummernfeld erhalten. Wenn ich mehr Objekte in dem Array habe, wird es kontinuierlich das zweite Objekt in einem Muster finden.Erhalte ein bestimmtes Objekt aus einem multidimensionalen Array

Ich bin jedoch nicht in der Lage, das 2. Element von der Verwendung des obigen Musters zu bekommen, und ich bekomme keine Logik, um dies zu bekommen.

Was soll ich in diesem Fall tun? Hast du irgendeine Logik? Wie würden Sie alle Arrays überspringen und nur bestimmte Elemente zurückgeben?

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 
     ) 

) 
+0

Ihre erwartete Ausgabe wäre also: '[" Script 1: Login "," Script 2: Enter ... "," Script 3: Benutzerdetails ändern "]'? – Rizier123

+0

Ja, du hast Recht. –

+0

Versuchen Sie, eine einfache foreach-Schleife zu machen, prüfen Sie, ob Ihr Schlüssel einer der erlaubten SubArrays ist (schauen Sie sich 'in_array()' an). Und wenn es so ist, dann hole das zweite Element in ein Array. Wenn du nicht weiterkommst, poste den Versuch hier. – Rizier123

Antwort

2

SET $array als Ihr multidimensionales Array.

Schleife durch das 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

Die Funktion 'array_key_exists()' prüft, ob der Schlüssel im Array vorhanden ist. Der Schlüssel 2 existiert im größten Teil des Arrays. Es wird nicht so funktionieren, wie ich es wollte. –

+0

Überprüfen Sie meine aktualisierte Antwort. –

+0

Das Array könnte mehr Elemente enthalten. Wir können nicht einfach nur sagen "[3, 10, 17]". –

1

Wenn Sie nur den 2 Index wollen (es wird nicht die „zweite“ sein als zweite beliebige Zahl sein könnte) von den Unteranordnungen dann:

$twos = array_column($array, 2); 

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