Преобразование моего кода JSON в массив PHP - PullRequest
0 голосов
/ 19 мая 2018

Привет, у меня есть код JSON для размещения видео на моем сайте. Я хочу преобразовать его в формат массива php, чтобы использовать его в функции php foreach.

Это код JSON

{
  "results": [
    {
      "TCA_ANALYZING": [
        "https://storage.googleapis.com/gypsoid/GOPRO/GoPRO_1/100GOPRO/10120_f1.mp4", 
        "https://storage.googleapis.com/gypsoid/GOPRO/GoPRO_1/100GOPRO/GH010001.MP4", 
        "https://storage.googleapis.com/gypsoid/GOPRO/GoPRO_1/100GOPRO/GH010001.THM", 
        "https://storage.googleapis.com/gypsoid/GOPRO/GoPRO_1/100GOPRO/GH010002.MP4", 
        "https://storage.googleapis.com/gypsoid/GOPRO/GoPRO_1/100GOPRO/GH010002.THM", 
        "https://storage.googleapis.com/gypsoid/GOPRO/GoPRO_1/100GOPRO/GH010014.MP4", 
        "https://storage.googleapis.com/gypsoid/GOPRO/GoPRO_1/100GOPRO/GH010014.THM", 
        "https://storage.googleapis.com/gypsoid/GOPRO/GoPRO_1/100GOPRO/GH010111.MP4", 
        "https://storage.googleapis.com/gypsoid/GOPRO/GoPRO_1/100GOPRO/GH010111.THM", 
        "https://storage.googleapis.com/gypsoid/GOPRO/GoPRO_1/100GOPRO/GH010112.MP4", 
        "https://storage.googleapis.com/gypsoid/GOPRO/GoPRO_1/100GOPRO/GH010112.THM", 
        "https://storage.googleapis.com/gypsoid/GOPRO/GoPRO_1/100GOPRO/GH010113.MP4", 
        "https://storage.googleapis.com/gypsoid/GOPRO/GoPRO_1/100GOPRO/GH010113.THM", 
        "https://storage.googleapis.com/gypsoid/GOPRO/GoPRO_1/100GOPRO/GH010114.MP4", 
        "https://storage.googleapis.com/gypsoid/GOPRO/GoPRO_1/100GOPRO/GH010114.THM", 
        "https://storage.googleapis.com/gypsoid/GOPRO/GoPRO_1/100GOPRO/GH010115.MP4", 
        "https://storage.googleapis.com/gypsoid/GOPRO/GoPRO_1/100GOPRO/GH010115.THM", 
        "https://storage.googleapis.com/gypsoid/GOPRO/GoPRO_1/100GOPRO/GH010116.MP4", 
        "https://storage.googleapis.com/gypsoid/GOPRO/GoPRO_1/100GOPRO/GH010116.THM", 
        "https://storage.googleapis.com/gypsoid/GOPRO/GoPRO_1/100GOPRO/GH010117.MP4", 
        "https://storage.googleapis.com/gypsoid/GOPRO/GoPRO_1/100GOPRO/GH010117.THM", 
        "https://storage.googleapis.com/gypsoid/GOPRO/GoPRO_1/100GOPRO/GH010118.MP4", 
        "https://storage.googleapis.com/gypsoid/GOPRO/GoPRO_1/100GOPRO/GH010118.THM", 
        "https://storage.googleapis.com/gypsoid/GOPRO/GoPRO_1/100GOPRO/GH010119.MP4", 
        "https://storage.googleapis.com/gypsoid/GOPRO/GoPRO_1/100GOPRO/GH010119.THM", 
        "https://storage.googleapis.com/gypsoid/GOPRO/GoPRO_1/100GOPRO/GH010120.MP4", 
        "https://storage.googleapis.com/gypsoid/GOPRO/GoPRO_1/100GOPRO/GH010120.THM", 
        "https://storage.googleapis.com/gypsoid/GOPRO/GoPRO_1/100GOPRO/GH010121.MP4", 
        "https://storage.googleapis.com/gypsoid/GOPRO/GoPRO_1/100GOPRO/GH010121.THM", 
        "https://storage.googleapis.com/gypsoid/GOPRO/GoPRO_1/100GOPRO/GH010122.MP4", 
        "https://storage.googleapis.com/gypsoid/GOPRO/GoPRO_1/100GOPRO/GH010122.THM", 
        "https://storage.googleapis.com/gypsoid/GOPRO/GoPRO_1/100GOPRO/GH010123.THM", 
        "https://storage.googleapis.com/gypsoid/GOPRO/GoPRO_1/100GOPRO/GH010124.MP4", 
        "https://storage.googleapis.com/gypsoid/GOPRO/GoPRO_1/100GOPRO/GH010124.THM", 
        "https://storage.googleapis.com/gypsoid/GOPRO/GoPRO_1/100GOPRO/GH020123.THM", 
        "https://storage.googleapis.com/gypsoid/GOPRO/GoPRO_1/100GOPRO/GHAA0005.MP4", 
        "https://storage.googleapis.com/gypsoid/GOPRO/GoPRO_1/100GOPRO/GHAA0005.THM", 
        "https://storage.googleapis.com/gypsoid/GOPRO/GoPRO_1/100GOPRO/GHAB0006.MP4", 
        "https://storage.googleapis.com/gypsoid/GOPRO/GoPRO_1/100GOPRO/GHAB0006.THM", 
        "https://storage.googleapis.com/gypsoid/GOPRO/GoPRO_1/100GOPRO/GHAC0007.MP4", 
        "https://storage.googleapis.com/gypsoid/GOPRO/GoPRO_1/100GOPRO/GHAC0007.THM", 
        "https://storage.googleapis.com/gypsoid/GOPRO/GoPRO_1/100GOPRO/GHAD0008.MP4", 
        "https://storage.googleapis.com/gypsoid/GOPRO/GoPRO_1/100GOPRO/GHAD0008.THM", 
        "https://storage.googleapis.com/gypsoid/GOPRO/GoPRO_1/100GOPRO/GHAE0009.MP4", 
        "https://storage.googleapis.com/gypsoid/GOPRO/GoPRO_1/100GOPRO/GHAE0009.THM", 
        "https://storage.googleapis.com/gypsoid/GOPRO/GoPRO_1/100GOPRO/GHAF0010.MP4", 
        "https://storage.googleapis.com/gypsoid/GOPRO/GoPRO_1/100GOPRO/GHAF0010.THM", 
        "https://storage.googleapis.com/gypsoid/GOPRO/GoPRO_1/100GOPRO/GHAG0011.MP4", 
        "https://storage.googleapis.com/gypsoid/GOPRO/GoPRO_1/100GOPRO/GHAG0011.THM", 
        "https://storage.googleapis.com/gypsoid/GOPRO/GoPRO_1/100GOPRO/GHAH0012.MP4", 
        "https://storage.googleapis.com/gypsoid/GOPRO/GoPRO_1/100GOPRO/GHAH0012.THM", 
        "https://storage.googleapis.com/gypsoid/GOPRO/GoPRO_1/100GOPRO/GHAI0013.MP4", 
        "https://storage.googleapis.com/gypsoid/GOPRO/GoPRO_1/100GOPRO/GHAI0013.THM", 
        "https://storage.googleapis.com/gypsoid/GOPRO/GoPRO_1/100GOPRO/GL010001.LRV", 
        "https://storage.googleapis.com/gypsoid/GOPRO/GoPRO_1/100GOPRO/GL010002.LRV", 
        "https://storage.googleapis.com/gypsoid/GOPRO/GoPRO_1/100GOPRO/GL010014.LRV", 
        "https://storage.googleapis.com/gypsoid/GOPRO/GoPRO_1/100GOPRO/GL010111.LRV", 
        "https://storage.googleapis.com/gypsoid/GOPRO/GoPRO_1/100GOPRO/GL010112.LRV", 
        "https://storage.googleapis.com/gypsoid/GOPRO/GoPRO_1/100GOPRO/GL010113.LRV", 
        "https://storage.googleapis.com/gypsoid/GOPRO/GoPRO_1/100GOPRO/GL010114.LRV", 
        "https://storage.googleapis.com/gypsoid/GOPRO/GoPRO_1/100GOPRO/GL010115.LRV", 
        "https://storage.googleapis.com/gypsoid/GOPRO/GoPRO_1/100GOPRO/GL010116.LRV", 
        "https://storage.googleapis.com/gypsoid/GOPRO/GoPRO_1/100GOPRO/GL010117.LRV", 
        "https://storage.googleapis.com/gypsoid/GOPRO/GoPRO_1/100GOPRO/GL010118.LRV", 
        "https://storage.googleapis.com/gypsoid/GOPRO/GoPRO_1/100GOPRO/GL010119.LRV", 
        "https://storage.googleapis.com/gypsoid/GOPRO/GoPRO_1/100GOPRO/GL010120.LRV", 
        "https://storage.googleapis.com/gypsoid/GOPRO/GoPRO_1/100GOPRO/GL010121.LRV", 
        "https://storage.googleapis.com/gypsoid/GOPRO/GoPRO_1/100GOPRO/GL010122.LRV", 
        "https://storage.googleapis.com/gypsoid/GOPRO/GoPRO_1/100GOPRO/GL010123.LRV", 
        "https://storage.googleapis.com/gypsoid/GOPRO/GoPRO_1/100GOPRO/GL010124.LRV", 
        "https://storage.googleapis.com/gypsoid/GOPRO/GoPRO_1/100GOPRO/GL020123.LRV", 
        "https://storage.googleapis.com/gypsoid/GOPRO/GoPRO_1/100GOPRO/GLAA0005.LRV", 
        "https://storage.googleapis.com/gypsoid/GOPRO/GoPRO_1/100GOPRO/GLAB0006.LRV", 
        "https://storage.googleapis.com/gypsoid/GOPRO/GoPRO_1/100GOPRO/GLAC0007.LRV", 
        "https://storage.googleapis.com/gypsoid/GOPRO/GoPRO_1/100GOPRO/GLAD0008.LRV", 
        "https://storage.googleapis.com/gypsoid/GOPRO/GoPRO_1/100GOPRO/GLAE0009.LRV", 
        "https://storage.googleapis.com/gypsoid/GOPRO/GoPRO_1/100GOPRO/GLAF0010.LRV", 
        "https://storage.googleapis.com/gypsoid/GOPRO/GoPRO_1/100GOPRO/GLAG0011.LRV", 
        "https://storage.googleapis.com/gypsoid/GOPRO/GoPRO_1/100GOPRO/GLAH0012.LRV", 
        "https://storage.googleapis.com/gypsoid/GOPRO/GoPRO_1/100GOPRO/GLAI0013.LRV", 
        "https://storage.googleapis.com/gypsoid/GOPRO/GoPRO_1/100GOPRO/GX010003.MP4", 
        "https://storage.googleapis.com/gypsoid/GOPRO/GoPRO_1/100GOPRO/GX010003.THM", 
        "https://storage.googleapis.com/gypsoid/GOPRO/GoPRO_1/100GOPRO/GX010004.MP4", 
        "https://storage.googleapis.com/gypsoid/GOPRO/GoPRO_1/100GOPRO/GX010004.THM"
      ]
    }, 
    {
      "TCA_ANALYZED": [
        "https://storage.googleapis.com/gypsoid/GOPRO/GoPRO_1/highlights/highlights/10116_f1.mp4", 
        "https://storage.googleapis.com/gypsoid/GOPRO/GoPRO_1/highlights/highlights/10118_f1.mp4", 
        "https://storage.googleapis.com/gypsoid/GOPRO/GoPRO_1/highlights/highlights/10119_f1.mp4", 
        "https://storage.googleapis.com/gypsoid/GOPRO/GoPRO_1/highlights/highlights/10122_f1.mp4"
      ]
    }, 
    {
      "OTHER_ANALYZED": [
        "https://storage.googleapis.com/gypsoid/comparison.mp4", 
        "https://storage.googleapis.com/gypsoid/detection", 
        "https://storage.googleapis.com/gypsoid/front.mp4", 
        "https://storage.googleapis.com/gypsoid/heat.mp4", 
        "https://storage.googleapis.com/gypsoid/impact.mp4", 
        "https://storage.googleapis.com/gypsoid/speed.mp4", 
        "https://storage.googleapis.com/gypsoid/thspeed.mp4", 
        "https://storage.googleapis.com/gypsoid/trajectory.mp4", 
        "https://storage.googleapis.com/gypsoid/tspeed.mp4", 
        "https://storage.googleapis.com/gypsoid/zoom.mp4"
      ]
    }, 
    {
      "OTHER_ANALYZING": []
    }
  ]
}

Я использовал json_decode(), но он не дал мне ожидаемого результата.

Я хочу преобразовать этот код Json в формат, который я могу использовать как:

$row['TCA_ANALYZING'];
$row['TCA_ANALYZED'];
$row['OTHER_ANALYZING'];
$row['OTHER_ANALYZED'];

1 Ответ

0 голосов
/ 19 мая 2018

Вы можете использовать array_reduce для форматирования массива в нужный вывод

$json = '';                                  //<-- Your json string
$arr = json_decode($json, true);             //Convert the json into associative array 
$row = array_reduce($arr['results'], function($c, $v){
    $c[ key($v) ] = current($v);
    return $c;
}, array());

print_r( $row );

Это приведет к:

Array
(
    [TCA_ANALYZING] => Array
        (
            ....
        )

    [TCA_ANALYZED] => Array
        (
            ....
        )

    [OTHER_ANALYZED] => Array
        (
            ....
        )

    [OTHER_ANALYZING] => Array
        (
        )

)

Doc: array_reduce ()

...