0

I'm trying to parse an array that looks like this:

array(1) {
  ["StrategischeDoelstellingenPerDepartement"] => array(412) {
    [0] => array(5) {
      ["CodeDepartement"] => string(8) "DEPBRAND"
      ["NummerHoofdstrategischeDoelstelling"] => string(1) "1"
      ["Nummer"] => string(2) "27"
      ["Titel"] => string(22) "DSD 01 - HULPVERLENING"
      ["IdBudgetronde"] => string(1) "2"
    }
    [1] => array(5) {
      ["CodeDepartement"] => string(8) "DEPBRAND"
      ["NummerHoofdstrategischeDoelstelling"] => string(1) "2"
      ["Nummer"] => string(2) "28"
      ["Titel"] => string(24) "DSD 02 - Dienstverlening"
      ["IdBudgetronde"] => string(1) "2"
    }
    [2] => array(5) {
      ["CodeDepartement"] => string(8) "DEPBRAND"
      ["NummerHoofdstrategischeDoelstelling"] => string(1) "2"
      ["Nummer"] => string(2) "29"
      ["Titel"] => string(16) "DSD 03 - KLANTEN"
      ["IdBudgetronde"] => string(1) "2"
    }
    ...

(The array goes on but it's too big to post it here in its entirety)

I can do a foreach loop on the array like this:

foreach($my_arr->StrategischeDoelstellingenPerDepartement as $row){
    echo "i found one <br>";
}

However, I want to do the same thing on other arrays and I want to make the function generic. The first key (StrategischeDoelstellingenPerDepartement in this case) can sometimes change, which is why I'd like to do it generically. I've already tried the following:

foreach($my_arr[0] as $row){
    echo "i found one <br>";
}

But then I get the following notice, and no data:

Notice: Undefined offset: 0 in C:\Users\Thomas\Documents\GitHub\Backstage\application\controllers\AdminController.php on line 29

This is probably a silly question, but I'm new to PHP and this seemed like the right way to do it. Obviously, it isn't. Can anyone help me out, please?

4 Answers 4

2

Use reset to grab the first element of $my_arr without knowing the key name:

$a = reset($my_arr);
foreach($a as $row){
    echo "i found one <br>";
}
Sign up to request clarification or add additional context in comments.

Comments

0

Shift the sub-array off the main array and loop over it:

$sub = array_shift($my_arr);
foreach ($sub as $row) {
    echo $row['Titel'], "<br>";
}

Comments

0

You are trying to do is object, not array $my_arr->StrategischeDoelstellingenPerDepartement. You could use isset() to check the index existence:

if(isset($my_arr['StrategischeDoelstellingenPerDepartement'])){
    foreach($my_arr['StrategischeDoelstellingenPerDepartement'] as $row){
        echo "i found one <br>";
    }
}

Or, you could use array_values() to ignore the array keys and to make it an index array:

$my_new_arr = array_values($my_arr);
foreach($my_new_arr as $row){
    echo "i found one <br>";
}   

Comments

0

Use current ref : https://www.php.net/manual/en/function.current.php

$a = current($my_arr);
foreach($a as $row){
    echo "i found one <br>";
}

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.