dscbxou1900343 2015-02-25 03:34
浏览 29
已采纳

Json_encode数组多维

I have a multidimensional array which is a combination of string and int value like this

Array ( [0] => Array ( [0] => 2,2 ) [1] => Array ( [0] => 2,59 ) )

I need to make this array become like this

[['2',   2],['2',       59]]

I using json_encode but it return

[["2,2"],["2,59"]]

I'm using this array for chart

I read from http://php.net/manual/en/function.json-encode.php but no one from json_ecode list can return like that

can someone help me? thx

can you help me once again,

    $data = new stdClass(); 
    $data->name = 'SD';
    $data->data = $totalSD;
    $data2 = new stdClass(); 
    $data2->name = 'SMP';
    $data2->data = $totalSMP;
    $list = array();
    array_push($list, $data);
    array_push($list, $data2);

I Have array like this

$a =[{"name":"SD","data":[0,0,0,0,0,0,0,0,0,0,0,0,0,17,2]},{"name":"SMP","data":[0,‌​0,0,0,0,0,0,0,0,0,0,0,0,0,0]}]; 

I need to change the array to become like this

$a =[{name:'SD',data:[0,0,0,0,0,0,0,0,0,0,0,0,0,17,2]},{name:'SMP',data:[0,0,0,0,0,‌​0,0,0,0,0,0,0,0,0,0]}];
  • 写回答

1条回答 默认 最新

  • dongzhang5787 2015-02-25 03:47
    关注

    It is do-able, using foreach() loops and exploding the string on the , delimiter.

    The below code uses the exact array you supplied above (based on the structure):

    foreach($array as $key => &$item) {
        foreach($item as $k => $data) {
            $item = explode(',', $data[0]);
        }
    }
    

    Which returns:

    [["2","2"],["2","59"]]
    

    Example


    If you need the first value as a string and the second as an int, the following should suffice:

    foreach($a as $key => &$item) {
        foreach($item as $k => $data) {
            list($string, $int) = explode(',', $data[0]);
            $item = array(strval($string), (int) $int);
        }
    }
    

    It harnesses the use of strval() to cast the first value as a string and we cast the second value as an int via the (int) loose-typing in PHP.

    Which returns:

    [["2", 2],["2", 59]]
    

    Example


    A Quick Note

    I've used by-reference in the foreach to modify the original array. So basically, you'll fetch your data, run the foreach loops to modify it as you need and then you'll encode it. This is a pseudo example of how that process would go:

    ARRAY = Get.ARRAY
    
    FOREACH ( ARRAY AS KEY => &VAL) {
        FOREACH (VAL AS KEY => VALUE) {
            LIST(FIRST_ITEM, LAST_ITEM) = EXPLODE(',', VALUE);
            VALUE = ARRAY ( strval(FIRST_ITEM), (int) LAST_ITEM );
        }
    }
    
    ECHO json_encode(ARRAY);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵