我有像这样的json格式
{"January":1,"February":2,"March":3,"April":0,"May":0,"June":0,"July":0,"August":0,"September":0,"October":0,"November":0,"December":0}
我希望有两个阵列可以像这样输出
arry1 = [January,February,March ...]
arry2 = [1,2,3...]
如何在不通过子字符串函数的情况下执行此操作,因为这将是一个更长的方法,php具有此内置功能
答案 0 :(得分:1)
您不需要循环来将键与值分开。 php具有隔离键和值的功能......
代码:(Demo)
$json = '{"January":1,"February":2,"March":3,"April":0,"May":0,"June":0,"July":0,"August":0,"September":0,"October":0,"November":0,"December":0}';
$decoded = json_decode($json, true); // decode as an array
$arry1 = array_keys($decoded); // isolate the keys
$arry2 = array_values($decoded); // isolate (and re-index) the values
echo "\$arry1: ";
var_export($arry1);
echo "\n---\n\$arry2: ";
var_export($arry2);
输出:
$arry1: array (
0 => 'January',
1 => 'February',
2 => 'March',
3 => 'April',
4 => 'May',
5 => 'June',
6 => 'July',
7 => 'August',
8 => 'September',
9 => 'October',
10 => 'November',
11 => 'December',
)
---
$arry2: array (
0 => 1,
1 => 2,
2 => 3,
3 => 0,
4 => 0,
5 => 0,
6 => 0,
7 => 0,
8 => 0,
9 => 0,
10 => 0,
11 => 0,
)
答案 1 :(得分:0)
这样的事情会对你有用:
<?php
// Put the json object into a variable
$jsonObject = '{"January":1,"February":2,"March":3,"April":0,"May":0,"June":0,"July":0,"August":0,"September":0,"October":0,"November":0,"December":0}';
// Decode the json object
$jsonDecodeValues= json_decode($jsonObject);
// Create an array for months and day numbers
$monthNameArray = array();
$monthNumberArray = array();
// Loop through the decoded values and add them to the month/number variables
foreach ($jsonDecodeValues as $jsonDecodeKey => $jsonDecodeValue) {
$monthNameArray[] = $jsonDecodeKey;
$monthNumberArray[] = $jsonDecodeValue ;
}