我将这个json保存在一个变量中,我试图将其转换为字符串?
$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_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';
我们有没有办法修改implode(",",$json_object)
函数来实现这个目标?
另一个问题: 知道我们怎么可能把这个
{"26":"Child - 1500.00","28":"Foreigner - 4000.00","27":"Resident - 3000.00"}
到
之类的列表26 : Child - 1500.00
27: Resident - 3000.00
28: Foreigner - 4000.00
答案 0 :(得分:3)
$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));
答案 1 :(得分:2)
试试这个:
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"
输出:
socket.onAny({ data in
print("ANy event =====> \(data)" )
})
socket.on("user", callback: {
data,ack in
print("call back called \(data) \(ack)")
})
答案 2 :(得分:2)
{
"_id" : ObjectId("592ea5d8026c1e263c3d99d9"),
"projectName" : "Yosemite",
"events" : {
"eventName" : "Great",
"eventDate" : ISODate("2018-05-30T00:00:00.000+05:30")
}
},
{
"_id" : ObjectId("5954d98f7be20e32842aea16"),
"projectName" : "Alfa Beta",
"events" : {
"eventName" : "Final Review",
"eventDate" : ISODate("2017-07-31T00:00:00.000+05:30")
}
},
{
"_id" : ObjectId("5954dc0023aac8f9f28ec8d2"),
"projectName" : "Hackthon",
"events" : {
"eventName" : "2nd Review",
"eventDate" : ISODate("2017-07-23T00:00:00.000+05:30")
},
{
"eventName" : "Final Review",
"eventDate" : ISODate("2017-07-31T00:00:00.000+05:30")
}
}
Out put is
$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);
答案 3 :(得分:2)
另一种可能的解决方案(如果您的JSON输入的大小以MB为单位测量,但对于几KB的输入足够好,则不是最佳解决方案):
$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'])."\n");
echo('Values: '.implode(',', $output['vals'])."\n");
答案 4 :(得分:0)
首先将json数据转换为php数组,然后将所有键和值存储在不同的数组中,然后将其内爆
$ sudo usermod -aG www-data personaluser
$ sudo chown -R personaluser:www-data /var/www
$ sudo chmod -R 775 /var/www
然后输出:
<?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;
答案 5 :(得分:-1)
你可以这样做,
$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);
并回复您的$resultStringKeys
和$resultStringValues
,您将获得输出。