Accessing data within an array

时间:2016-04-25 08:57:33

标签: php arrays

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

3 个答案:

答案 0 :(得分: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..

答案 1 :(得分: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"];
        }
    }
}

}

答案 2 :(得分: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"];
        }
    }
}