0

I create an array which ends up with quite a bit of data. An example of this array is like so.

array:9 [▼
  0 => array:9 [▼
    "ID" => "1232806"
    "Date" => "21/04/2016"
    "Name" => "Test"
    "Owner" => "Someone"
    "Value" => "2160.00"
    "Status/Stage" => "70%"
    0 => array:2 [▼
      "Structure" => ""
      "Prospect" => "No"
    ]
    1 => array:2 [▼
      0 => array:8 [▼
        "Quote ID" => "Q0020"
        "Name" => "Test"
        "Amount" => "2160"
      ]
      1 => array:2 [▼
        0 => array:1 [▼
          "Type" => "New"
        ]
        1 => array:1 [▼
          "Month" => "June 16"
        ]
      ]
    ]
  ]
]

I am now trying to get the data out of this array. I can get the top level items out without issue

foreach ($array as $data) {
    echo $data["ID"];
    echo $data["Date"];
    echo $data["Name"];
    ...
}

I am struggling with the inner data however. I am trying something like

foreach ($array as $data) {
    echo $data["ID"];
    echo $data["Date"];
    echo $data["Name"];
    foreach ($data as $innerData) {
        echo innerData["Structure"];
        echo innerData["Prospect"];
    }
}

If I do this however, it complains about an Illegal String Offset. How can I get access to all the different data within this array?

Thanks

Created a question showing how I collect the data as I could be doing this wrong https://stackoverflow.com/questions/36837239/processing-xml-data-into-array

2
  • What is below of Status/Stage? Commented Apr 25, 2016 at 8:59
  • the missing $ before $innerData post typo or code bug? Commented Apr 25, 2016 at 9:28

3 Answers 3

1

Try as per below flow:

foreach ($array as $k=>$data) {
        echo $data["ID"];
        echo $data["Date"];
        echo $data["Name"];
    if(is_numeric($k) && is_array($data))
    {

        foreach ($data as $innerData) {
        echo innerData["Structure"];
        echo innerData["Prospect"];
        }
    }
}

}

Sign up to request clarification or add additional context in comments.

Comments

1

From your example the array entry after $data["name"] is not an array but a string

"Owner" => "Someone"

You can circumvent this kind of problem by testing your array value to determine whether it's an array and check if your array key exists

if (is_array($innerData)) {
    foreach ($data as $innerData) {
        if (isset($innerData['structure'])) {
            echo $innerData["Structure"];
        }
        // This also works
        if (array_key_exists('Prospect', $innerdData)) {
            echo $innerData["Prospect"];
        }
    }
}

Comments

1
You need to assign inner array in another new index like:
I have created innnerdata index. 

array:9 [▼
  0 => array:9 [▼
    "ID" => "1232806"
    "Date" => "21/04/2016"
    "Name" => "Test"
    "Owner" => "Someone"
    "Value" => "2160.00"
    "Status/Stage" => "70%"
    "innnerdata" => array:2 [
    0 => array:2 [
      "Structure" => ""
      "Prospect" => "No"
    ]
    1 => array:2 [
      0 => array:8 [
        "Quote ID" => "Q0020"
        "Name" => "Test"
        "Amount" => "2160"
      ]
      1 => array:2 [
        0 => array:1 [
          "Type" => "New"
        ]
        1 => array:1 [
          "Month" => "June 16"
        ]
      ]
    ]
   ]

  ]
]

so now you can access it like:

foreach ($array as $data) {
    echo $data["ID"];
    echo $data["Date"];
    echo $data["Name"];
    foreach ($data['innerdata'] as $innerData) {
        echo innerData["Structure"];
        echo innerData["Prospect"];
    }
}

otherwise you 'll have to access it like $data[0]["Structure"], $data[1]["Structure"] etc..

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.