我有一个字符串,我想将值更改为重新排列,例如我有这个:
{
"code_2":"CODGD",
"description_2":"First product",
"price_2":"45.0",
"quantity_2":"1",
"quantityant_2":"1",
"subtotal_2":"45.0",
"code_3":"CODRT",
"description_3":"Second product",
"price_3":"12.68",
"quantity_3":"1",
"quantityant_3":"1",
"subtotal_3":"12.68",
"code_7":"CODPO",
"description_7":"Third product",
"price_7":"434.0",
"quantity_7":"1",
"quantityant_7":"1",
"subtotal_7":"434.0"
}
它像2,3和7,但我需要它们是1,2,3 每个对象都是6个组; code_1,description_1,price_1,quantity_1,quantityant_1,subtotal_1。 我需要的是被_所包围的对象并且“被重新编号,例如像这样:
{
"code_1":"CODGD",
"description_1":"First product",
"price_1":"45.0",
"quantity_1":"1",
"quantityant_1":"1",
"subtotal_1":"45.0",
"code_2":"CODRT",
"description_2":"Second product",
"price_2":"12.68",
"quantity_2":"1",
"quantityant_2":"1",
"subtotal_2":"12.68",
"code_3":"CODPO",
"description_3":"Third product",
"price_3":"434.0",
"quantity_3":"1",
"quantityant_3":"1",
"subtotal_3":"434.0"
}
我一直在考虑使用str_replace但是我有点失落。
希望你能帮助我。
由于
额外细节 我实际上使用以下形式获取数组中的信息:
Array([code_2] => CODGD[description_2] => First product[price_2] => 45.0[quantity_2] => 1[quantityant_2] => 1[subtotal_2] => 45.0[code_3] => CODRT[description_3] => Second product[price_3] => 45.0[quantity_3] => 1[quantityant_3] => 1[subtotal_3] => 45.0[code_7] => CODPO[description_7] => Third product[price_7] => 23.43[quantity_7] => 1[quantityant_7] => 1[subtotal_7] => 23.43)
答案 0 :(得分:2)
你可以这样做: -
<?php
$string = '{"code_2":"CODGD", "description_2":"First product", "price_2":"45.0", "quantity_2":"1", "quantityant_2":"1", "subtotal_2":"45.0", "code_3":"CODRT", "description_3":"Second product", "price_3":"12.68", "quantity_3":"1", "quantityant_3":"1", "subtotal_3":"12.68", "code_7":"CODPO", "description_7":"Third product", "price_7":"434.0", "quantity_7":"1", "quantityant_7":"1", "subtotal_7":"434.0"}';
echo "<pre/>";print_r($array = json_decode($string,true));
$final_array = array();
$i = 1;
for($j = 0;$j<3;$j++){
foreach ($array as $key=>$val){
$final_array[explode('_',$key)[0].'_'.$i] = $val;
}
$i++;
}
echo "<pre/>";print_r($final_array);
输出: - https://eval.in/725237
一个更好的解决方案(在我第一次尝试之后): -
<?php
$string = '{"code_2":"CODGD", "description_2":"First product", "price_2":"45.0", "quantity_2":"1", "quantityant_2":"1", "subtotal_2":"45.0", "code_3":"CODRT", "description_3":"Second product", "price_3":"12.68", "quantity_3":"1", "quantityant_3":"1", "subtotal_3":"12.68", "code_7":"CODPO", "description_7":"Third product", "price_7":"434.0", "quantity_7":"1", "quantityant_7":"1", "subtotal_7":"434.0"}';
echo "<pre/>";print_r($array = json_decode($string,true));
$final_array = array();
$i = 1;
$j = 1;
foreach ($array as $key=>$val){
if($j ==7){
$i +=1;
$j = 1;
}
$final_array[explode('_',$key)[0].'_'.$i] = $val;
$j++;
}
echo "<pre/>";print_r($final_array);
输出: - https://eval.in/725252
答案 1 :(得分:1)
您的字符串是JSON格式的,因此可以解码和处理它而不需要繁琐的str_replace
玩杂耍。
$json = <<<JSON
{"code_2":"CODGD", "description_2":"First product", "price_2":"45.0", "quantity_2":"1", "quantityant_2":"1", "subtotal_2":"45.0", "code_3":"CODRT", "description_3":"Second product", "price_3":"12.68", "quantity_3":"1", "quantityant_3":"1", "subtotal_3":"12.68", "code_7":"CODPO", "description_7":"Third product", "price_7":"434.0", "quantity_7":"1", "quantityant_7":"1", "subtotal_7":"434.0"}
JSON;
$data = json_decode($json, true);
$items = array_chunk($data, 6, true);
$items = array_combine(range(1, count($items)), $items);
$out = array();
foreach ($items as $idx => $item) {
foreach ($item as $k => $v) {
$k = preg_replace('/^(.+)_\d+$/', '$1_' . $idx, $k);
$out[$k] = $v;
}
}
$json = json_encode($out);
echo $json;