数组内的PHP foreach数组

时间:2018-09-20 02:00:06

标签: php arrays

所以我目前正在使用XenForo从数组内部的数组中读取值,但是当使用此代码时,我似乎在最后一个foreach上遇到了问题。 ErrorException: Invalid argument supplied for foreach()

数组

{
  "data": {
    "type": "server",
    "id": "121",
    "attributes": {
      "name": "1",
      "ip": "127.0.0.1",
      "port": 4000,
    },
  },
}

这是我的foreach(正在获取上述数组的“属性”部分)。

foreach($json_array as $key => $arrays){
    foreach($arrays as $array){
        foreach($array as $key => $value){
            $data[$key] = $value;
        }
    }
}

有人知道是否有更好的方法来获取以下值?

$value['name']
$value['ip']
$value['port']

3 个答案:

答案 0 :(得分:1)

尝试这种方式:

with
inputs(gamedate,playername,pointsscored) as (
select to_date('20180912','yyyymmdd'), 'George',52 from dual union all
select to_date('20180907','yyyymmdd'), 'George',47 from dual union all
select to_date('20180829','yyyymmdd'), 'George',9 from dual union all
select to_date('20180823','yyyymmdd'), 'George',55 from dual union all
select to_date('20180818','yyyymmdd'), 'George',49 from dual union all
select to_date('20180811','yyyymmdd'), 'George',58 from dual union all
select to_date('20180805','yyyymmdd'), 'George',31 from dual union all
select to_date('20180730','yyyymmdd'), 'George',40 from dual union all
select to_date('20180720','yyyymmdd'), 'George',44 from dual union all
select to_date('20180712','yyyymmdd'), 'George',45 from dual union all
select to_date('20180707','yyyymmdd'), 'George',29 from dual union all
select to_date('20180701','yyyymmdd'), 'George',-5 from dual union all
select to_date('20180626','yyyymmdd'), 'George',46 from dual union all
select to_date('20180620','yyyymmdd'), 'George',22 from dual union all
select to_date('20180614','yyyymmdd'), 'George',49 from dual union all
select to_date('20180609','yyyymmdd'), 'George',40 from dual union all
select to_date('20180602','yyyymmdd'), 'George',40 from dual
)

答案 1 :(得分:1)

如果要循环播放:

foreach($json_array['data']['attributes'] as $key => $value){
  print_r($value);
}

答案 2 :(得分:-3)

非常简单。要获取给定数组的属性,您可以尝试上面的代码。 无需循环数组,只需使用$ array [key]方法即可获取目标值。

$data = $json_array['data']['attributes']