dongzhan1383 2017-01-27 06:42
浏览 95
已采纳

字符串更改和枚举值PHP

i have a string, and I want to change the values to be in rearranged, for example i have this:

{
"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"
}

It goes like 2,3 and 7 but i need them to be 1,2,3 Every object are in group of 6; code_1, description_1, price_1, quantity_1, quantityant_1, subtotal_1. What I need is that the objects surronded by _ and " be renumerated, for example like this:

{
"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"
}

I been thinking in using str_replace but im a little lost.

Hope you can help me.

Thanks

Extra details I actually get the info in an array with this form:

 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)
  • 写回答

2条回答 默认 最新

  • duanbanzhi4419 2017-01-27 06:49
    关注

    you can do it like below:-

    <?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);
    

    Output:- https://eval.in/725237

    A much better solution (After looking my first attempt itself):-

    <?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);
    

    Output:- https://eval.in/725252

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 怎么让数码管亮的同时让led执行流水灯代码
  • ¥20 SAP HANA SQL Script 。如何判断字段值包含某个字符串
  • ¥85 cmd批处理参数如果含有双引号,该如何传入?
  • ¥15 fx2n系列plc的自控成型机模拟
  • ¥15 时间序列LSTM模型归回预测代码问题
  • ¥50 使用CUDA如何高效的做并行化处理,是否可以多个分段同时进行匹配计算处理?目前数据传输速度有些慢,如何提高速度,使用gdrcopy是否可行?请给出具体意见。
  • ¥15 基于STM32,电机驱动模块为L298N,四路运放电磁传感器,三轮智能小车电磁组电磁循迹(两个电机,一个万向轮),如何通过环岛的原理及完整代码
  • ¥20 机器学习或深度学习问题?困扰了我一个世纪,晚来天欲雪,能饮一杯无?
  • ¥15 c语言数据结构高铁订票系统
  • ¥15 关于wkernell.PDB加载的问题,如何解决?(语言-c#|开发工具-vscode)