php json conversion
代言
代言 2017-06-13 09:22:12
0
1
826
    {
        "a1": {
            "price": {
                "high": 4.68,
                "low": 3.75,
                "code": 1
            }
        },
        "a2": {
            "price": {
                "high": 2.77,
                "low": 2.29,
                "code": 1
            }
        }
    }

    现有上面的json,怎么把它转换一下变成下面的格式:
{

    "china": {
        "cny": {
            "a1": {
                "high": 111,
                "low": 66
            },
            "a2": {
                "high": 44,
                "low": 22
            }
        }
    }

}


    

My idea is to extract the original json first, because the original json may have a different format, and then uniformly convert it into the following format, which foreach cannot do.

代言
代言

reply all(1)
伊谢尔伦
$jsonStr = <<<CODE
    {
        "a1": {
            "price": {
                "high": 4.68,
                "low": 3.75,
                "code": 1
            }
        },
        "a2": {
            "price": {
                "high": 2.77,
                "low": 2.29,
                "code": 1
            }
        }
    }
CODE;

$nativeArray = json_decode($jsonStr, true);
$data = array();
foreach ($nativeArray as $key => $item) {
    unset($item['price']['code']);
    $data['china']['cny'][$key] = $item['price'];
}
echo json_encode($data);
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template