1

I have this array from an API:

array(2) {
  ["messages"]=>
  array(1) {
    [0]=>
    string(7) "Success"
  }
  ["data"]=>
  array(1) {
    ["INST4841_NNN59"]=>
    array(3) {
      ["English"]=>
      array(1) {
        [4380258]=>
        array(3) {
          [0]=> 
          array(12) {
            ["idinstance"]=>
            string(8) "11059013"
            ["idassessment"]=>
            string(2) "53"
            ["assessmenttitle"]=>
            string(17) "English Screening"
            ["idlevel"]=>
            string(1) "6"
            ["level"]=>
            string(7) "Level 2"
            ["start"]=>
            string(19) "10:27:21 30-05-2017"
            ["finish"]=>
            string(19) "10:33:10 30-05-2017"
            ["timetaken"]=>
            string(3) "335"
            ["idsubject"]=>
            string(2) "10"
            ["subjecttitle"]=>
            string(7) "English"
            ["possible"]=>
            int(15)
            ["actual"]=>
            int(15)
          }
          [1]=>
          array(12) {
            ["idinstance"]=>
            string(8) "11059105"
            ["idassessment"]=>
            string(2) "49"
            ["assessmenttitle"]=>
            string(26) "English Initial Assessment"
            ["idlevel"]=>
            string(1) "4"
            ["level"]=>
            string(13) "Entry Level 3"
            ["start"]=>
            string(19) "10:33:13 30-05-2017"
            ["finish"]=>
            string(19) "10:35:03 30-05-2017"
            ["timetaken"]=>
            string(3) "421"
            ["idsubject"]=>
            string(2) "10"
            ["subjecttitle"]=>
            string(7) "English"
            ["possible"]=>
            int(22)
            ["actual"]=>
            float(8.2)
          }
          [2]=>
          array(12) {
            ["idinstance"]=>
            string(8) "11059147"
            ["idassessment"]=>
            string(2) "57"
            ["assessmenttitle"]=>
            string(18) "English Diagnostic"
            ["idlevel"]=>
            string(1) "3"
            ["level"]=>
            string(13) "Entry Level 2"
            ["start"]=>
            string(19) "10:35:09 30-05-2017"
            ["finish"]=>
            string(19) "10:35:44 30-05-2017"
            ["timetaken"]=>
            string(2) "20"
            ["idsubject"]=>
            string(2) "10"
            ["subjecttitle"]=>
            string(7) "English"
            ["possible"]=>
            int(67)
            ["actual"]=>
            int(5)
          }
        }
      }
      ["ICT"]=>
      array(1) {
        [4380260]=>
        array(2) {
          [0]=>
          array(12) {
            ["idinstance"]=>
            string(8) "11059399"
            ["idassessment"]=>
            string(4) "1185"
            ["assessmenttitle"]=>
            string(22) "ICT Initial Assessment"
            ["idlevel"]=>
            string(1) "5"
            ["level"]=>
            string(7) "Level 1"
            ["start"]=>
            string(19) "10:52:05 30-05-2017"
            ["finish"]=>
            string(19) "10:58:04 30-05-2017"
            ["timetaken"]=>
            string(3) "339"
            ["idsubject"]=>
            string(2) "12"
            ["subjecttitle"]=>
            string(3) "ICT"
            ["possible"]=>
            int(79)
            ["actual"]=>
            float(66.7)
          }
          [1]=>
          array(12) {
            ["idinstance"]=>
            string(8) "11059509"
            ["idassessment"]=>
            string(4) "1189"
            ["assessmenttitle"]=>
            string(14) "ICT Diagnostic"
            ["idlevel"]=>
            string(1) "6"
            ["level"]=>
            string(7) "Level 2"
            ["start"]=>
            string(19) "10:58:07 30-05-2017"
            ["finish"]=>
            string(19) "10:58:29 30-05-2017"
            ["timetaken"]=>
            string(2) "14"
            ["idsubject"]=>
            string(2) "12"
            ["subjecttitle"]=>
            string(3) "ICT"
            ["possible"]=>
            int(19)
            ["actual"]=>
            int(1)
          }
        }
      }
      ["Maths"]=>
      array(1) {
        [4380259]=>
        array(3) {
          [0]=>
          array(11) {
            ["idinstance"]=>
            string(8) "11059522"
            ["idassessment"]=>
            string(2) "52"
            ["assessmenttitle"]=>
            string(15) "Maths Screening"
            ["idlevel"]=>
            string(1) "2"
            ["level"]=>
            string(13) "Entry Level 1"
            ["start"]=>
            string(19) "10:58:45 30-05-2017"
            ["finish"]=>
            string(19) "10:58:50 30-05-2017"
            ["idsubject"]=>
            string(2) "11"
            ["subjecttitle"]=>
            string(5) "Maths"
            ["possible"]=>
            int(4)
            ["actual"]=>
            int(0)
          }
          [1]=>
          array(12) {
            ["idinstance"]=>
            string(8) "11059523"
            ["idassessment"]=>
            string(2) "48"
            ["assessmenttitle"]=>
            string(24) "Maths Initial Assessment"
            ["idlevel"]=>
            string(1) "1"
            ["level"]=>
            string(9) "Pre Entry"
            ["start"]=>
            string(19) "10:58:52 30-05-2017"
            ["finish"]=>
            string(19) "10:59:06 30-05-2017"
            ["timetaken"]=>
            string(1) "6"
            ["idsubject"]=>
            string(2) "11"
            ["subjecttitle"]=>
            string(5) "Maths"
            ["possible"]=>
            int(0)
            ["actual"]=>
            int(0)
          }
          [2]=>
          array(12) {
            ["idinstance"]=>
            string(8) "11059530"
            ["idassessment"]=>
            string(2) "54"
            ["assessmenttitle"]=>
            string(16) "Maths Diagnostic"
            ["idlevel"]=>
            string(1) "2"
            ["level"]=>
            string(13) "Entry Level 1"
            ["start"]=>
            string(19) "10:59:08 30-05-2017"
            ["finish"]=>
            string(19) "11:01:12 30-05-2017"
            ["timetaken"]=>
            string(2) "92"
            ["idsubject"]=>
            string(2) "11"
            ["subjecttitle"]=>
            string(5) "Maths"
            ["possible"]=>
            int(63)
            ["actual"]=>
            float(15.2)
          }
        }
      }
    }
  }
}

I can get the level of English Initial Assessment using:

$english = $json['data']['INST4841_NNN59']['English'][4380258][1]['level'];

However I need to run it in a loop and [4380258] and [1] vary. How can I get the level without these values?

Thanks.

1

2 Answers 2

0

Try this:

foreach ($json['data']['INST4841_NNN59']['English'] as $key => $value){
    foreach ($value as $k => $v) {
        if (isset($v['assessmenttitle']) 
            &&
            $v['assessmenttitle'] == 'English Initial Assessment'
            &&
            isset($v['level'])
        ) {
            echo $v['level'];
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, I didn't downvote. This outputs all three levels, ie "Level 2Entry Level 3Entry Level 2" the API may return one, two or three of these. I need specifically the English Initial Assessment one?
Thanks very much works perfectly, I have tried to upvote but I don't have enough reputation. I will as soon as I do.
0

You could use a recursive method:

function loop_array($array = [], $return = []) {
    foreach ($array as $key => $value) {
        if (is_array($value)) {
            $return = loop_array($array[$key], $return);
        }
        elseif ($key === 'level') {
                $return[] = $value;
            }
        }
    }

    return $return;
}

$information = [
    'subinformation' => [
        'another_level' => [
            'level' => 'new level'
        ]
    ],
    'level' => 1,
    'another_sublevel_thing' => [
        'new_sublevel' => [
            'level' => 'first'
        ]
    ]
];

$levels = loop_array($information);

This will generate the following array:

array(3) {
    [0]=>
    string(9) "new level"
    [1]=>
    int(1)
    [2]=>
    string(5) "first"
}

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.