0
{ 
    "ServiceCurrentlyPlaying": {
        "fn": "Slideshow-41958.mp4",
        "apps": {
            "ServiceCurrentlyPlaying": {
                "state": "stopped"
            }
        }
    }
}

我将如何从数组中破坏任何名为 ServiceCurrentlyPlaying 的东西?(来自json_decode(file, TRUE))对于任何知道它的人来说,这可能是一个简单的问题,但是我一直在尝试做一些不涉及手动将每个数组硬编码到另一个空数组中的事情(就像foreach ($outer as $inner)我正在做的很多一样但是由于嵌套数量不同而出现问题)

注意:我必须处理大约 41958 个文件,它们都有不同的嵌套级别、不同的数量和结构,所以..

我想要的结果是:

{
    "fn": "Slideshow-41958.mp4",
    "apps": {
    "state": "stopped"
    }
}

谢谢,非常感谢。

4

2 回答 2

0

采用

 $array=json_decode($jsondata);
    $i=0;
    foreach($array as $key=>$arr)
    {
     $out[$i]['fn']=$arr['fn'];
    $out[$i]['apps]=$arr['apps']['ServiceCurrentlyPlaying']

    $i++;
    }
于 2012-10-25T04:47:52.977 回答
0

也许不是很优化,但这就是想法。

$data = json_decode('{ "ServiceCurrentlyPlaying": { "fn": "Slideshow-41958.mp4", "apps": { "ServiceCurrentlyPlaying": { "state": "stopped" } } } }', true);
$modifiedData = breakArray($data);

function breakArray($arr) {
  if(is_array($arr) && sizeof($arr)>0) {
    $buffer = array();

    foreach($arr as $key=>$value) {
      if($key==="ServiceCurrentlyPlaying") {
        if(is_array($value)) $buffer = array_merge($buffer, breakArray($value));
      } else {
        $buffer[$key] = (is_array($value) ? breakArray($value) : $value);
      }
    }

    return $buffer;
  } else {
    return $arr;
  }
}
于 2012-10-25T05:20:48.740 回答