如何在PHP中动态插入JSON数组

时间:2017-04-07 07:41:50

标签: php json

我有Json文件,其中包含三个数组我有相同的键品牌,参考所有三个数组我怎么能动态地将这些数据插入到PHP中。如果我增加JSON数组,那么数据将动态打印。

JSON

[{
    "Brand": "29\/25 PROSPRING",
    "Reference": "BC0290C",
},

{
    "Brand": "Alpha",
    "Reference": "BC23WEQ",
}

{
    "Brand": "30/40 Resin",
    "Reference": "BC23MJU",
}]

PHP

<?php

error_reporting(0);

$json_file = file_get_contents('jsonfile.json');

$someArray = json_decode($json_file, true);


?> 

HTML

<div class="page4">

<a href="product.php">29\/25 PROSPRING</a> 

</div> 

1 个答案:

答案 0 :(得分:1)

<?php
    $json = file_get_contents("jsonfile.json");

    $jsonIterator = new RecursiveIteratorIterator(
                    new RecursiveArrayIterator(json_decode($json, TRUE)),
                    RecursiveIteratorIterator::SELF_FIRST);
    $arr = [];

    foreach($jsonIterator as $key => $val) {
        if(is_array($val)) {
            $arr[$key]=$val;
        }
    }

    echo '<pre>';
    print_r($arr);
    echo '</pre>';
?>

输出: enter image description here