如何将json对象内爆到php中的字符串? [关闭]
I have this json held in a variable that I am trying to convert to a string?
$json_object = '[{"125":"1"},{"126":"2"},{"127":"3"},{"128":"4"},{"129":"5"},{"130":"6"},{"131":"7"},{"132":"8"},{"133":"9"},{"134":"10"},{"135":"11"}]';
What I want as my end result is something that looks like this:
$json_object1 = '1,2,3,4,5,6,7,8,9,10,11';
$json_object2 = '125,126,127,128,129,130,131,132,133,134,135';
Is there a way we can modify the implode(",",$json_object)
function to achieve this?
Another question: Any idea how we might put this
{"26":"Child - 1500.00","28":"Foreigner - 4000.00","27":"Resident - 3000.00"}
To a list like
26 : Child - 1500.00
27: Resident - 3000.00
28: Foreigner - 4000.00
我将这个json保存在一个我想要转换为字符串的变量中吗? p>
我想要的最终结果是 看起来像这样: p>
有没有办法可以修改 另一个问题:
不知道我们如何设置这个 p>
到列表 p>
$ json_object ='[{“125”:“1”},{“126”:“2”},{“127”:“3”},{“128”:“4 “},{” 129 “:” 5 “},{” 130 “:” 6 “},{” 131 “:” 7 “},{” 132 “:” 8 “},{” 133 “:” 9 “},{”134“:”10“},{”135“:”11“}]';
code> pre>
$ json_object1 ='1,2,3,4,5,6,7,8,9,10,11';
$ json_object2 ='125,126,127,128,129,130,131,132,133,134,135';
code> pre>
implode(“,”,$ json_object) code>函数来实现 这个? p>
{“26”:“Child - 1500.00”,“ 28“:”外国人 - 4000.00“,”27“:”居民 - 3000.00“}
code> pre>
26:儿童 - 1500.00
27:居民 - 3000.00
28:外国人 - 4000.00
code> pre>
div>
$json_object = '[{"125":"1"},{"126":"2"},{"127":"3"},{"128":"4"},{"129":"5"},{"130":"6"},{"131":"7"},{"132":"8"},{"133":"9"},{"134":"10"},{"135":"11"}]';
$json = json_decode($json_object);
echo implode(", ", array_map(function($obj) { foreach ($obj as $p => $v) { return $p;} }, $json));
echo "<br>";
echo implode(", ", array_map(function($obj) { foreach ($obj as $p => $v) { return $v;} }, $json));
first convert json data to php array then store all key and value in different array then implode it
<?php
$json_object = '[{"125":"1"},{"126":"2"},{"127":"3"},{"128":"4"},{"129":"5"},{"130":"6"},{"131":"7"},{"132":"8"},{"133":"9"},{"134":"10"},{"135":"11"}]';
$array_data = json_decode($json_object, true);
foreach($array_data as $data) {
foreach($data as $key => $value) {
$array_key[] = $key;
$array_value[] = $value;
}
}
$final_key = implode(",", $array_key);
$final_value = implode(",", $array_value);
echo $final_key;
echo "<br>";
echo $final_value;
then output is :
125,126,127,128,129,130,131,132,133,134,135
1,2,3,4,5,6,7,8,9,10,11
You can achieve like this,
$json_object = '[{"125":"1"},{"126":"2"},{"127":"3"},{"128":"4"},{"129":"5"},{"130":"6"},{"131":"7"},{"132":"8"},{"133":"9"},{"134":"10"},{"135":"11"}]';
$key_arr = array();
$val_arr = array();
$json_arr = json_decode($json_object);
foreach($json_arr as $val)
{
foreach($val as $key => $value)
{
$key_arr[] = $key;
$val_arr[] = $value;
}
}
$resultStringKeys = implode(",", $key_arr);
$resultStringValues = implode(",", $val_arr);
And echo your $resultStringKeys
and $resultStringValues
and you will get your output.
Try this:
<?php
$jsonString = '[{"125":"1"},{"126":"2"},{"127":"3"},{"128":"4"},{"129":"5"},{"130":"6"},{"131":"7"},{"132":"8"},{"133":"9"},{"134":"10"},{"135":"11"}]';
$decoded = json_decode($jsonString, true);
$keys = [];
$values = [];
foreach($decoded as $item) {
foreach($item as $key => $value) {
$keys[] = $key;
$values[] = $value;
}
}
$resultStringKeys = implode(",", $keys);
$resultStringValues = implode(",", $values);
var_dump($resultStringKeys, $resultStringValues);
The output:
string(43) "125,126,127,128,129,130,131,132,133,134,135"
string(23) "1,2,3,4,5,6,7,8,9,10,11"
$json_object = '[{"125":"1"},{"126":"2"},{"127":"3"},{"128":"4"},{"129":"5"},{"130":"6"},{"131":"7"},{"132":"8"},{"133":"9"},{"134":"10"},{"135":"11"}]';
$arr1 = []; $arr2=[];
$jsonobj = json_decode($json_object);
foreach ($jsonobj as $val){
$Arrval = (array) $val;
foreach ($Arrval as $k=>$v){
$arr1[]=$k;
$arr2[]=$v;
}
}
$json_object1 = implode(",",$arr1);
$json_object2 = implode(",",$arr2);
Out put is
125,126,127,128,129,130,131,132,133,134,135
1,2,3,4,5,6,7,8,9,10,11
Another possible solution (not the best if the size of your JSON input is measured in MBs but good enough for several KBs of input):
$input = '[{"125":"1"},{"126":"2"},{"127":"3"},{"128":"4"},{"129":"5"},{"130":"6"},{"131":"7"},{"132":"8"},{"133":"9"},{"134":"10"},{"135":"11"}]';
// Decode the JSON into arrays; TRUE as the second argument requires arrays, not objects
$data = json_decode($input, TRUE);
// Run through the list, extract the data into a new list
$output = array_reduce(
$data,
function(array $carry, array $item) {
// Put the keys and values of $item into the corresponding lists on $carry
$carry['keys'] = array_merge($carry['keys'], array_keys($item));
$carry['vals'] = array_merge($carry['vals'], array_values($item));
return $carry;
},
// Start with empty lists of keys and values
array('keys' => array(), 'vals' => array())
);
// That's all; $output['keys'] contains the keys, $output['values'] contains the values.
echo('Keys: '. implode(',', $output['keys'])."
");
echo('Values: '.implode(',', $output['vals'])."
");