将php数组映射到嵌套的json

山羊人

我一直在SO中寻找提示,并且有一些类似的问答,但到目前为止还没有运气。我基本上是想得到这个php数组:

array (size=xx)
  14 => 
    array (size=2)
      0 => string '14' (length=2)
      1 => float 80
  15 => 
    array (size=2)
      0 => string '15' (length=2)
      1 => float 70
  1522 => 
    array (size=4)
      0 => string '15' (length=2)
      1 => float 70
      2 => string '22' (length=2)
      3 => float 60
  1523 => 
    array (size=4)
      0 => string '15' (length=2)
      1 => float 70
      2 => string '23' (length=2)
      3 => float 70
  152329 => 
    array (size=6)
      0 => string '15' (length=2)
      1 => float 70
      2 => string '23' (length=2)
      3 => float 70
      4 => string '29' (length=2)
      5 => float 30
  152334 => 
    array (size=6)
      0 => string '15' (length=2)
      1 => float 70
      2 => string '23' (length=2)
      3 => float 70
      4 => string '34' (length=2)
      5 => float 80
  16 => 
    array (size=2)
      0 => string '16' (length=2)
      1 => float 30
  1624 => 
    array (size=4)
      0 => string '16' (length=2)
      1 => float 30
      2 => string '24' (length=2)
      3 => float 35

转换(或映射?)成这个json:

{
    "children": [
        {
            "id": "14",
            "name": "Id 14",
            "value": "80.0",
            "children": []
        },
        {
            "id": "15",
            "name": "ID 15",
            "value": "70.0",
            "children": [
                {
                    "id": "22",
                    "name": "ID 22",
                    "score": "60.0",
                    "children": []
                },
                {
                    "id": "23",
                    "name": "Id 23",
                    "score": "70.0",
                    "children": [
                        {
                            "id": "29",
                            "name": "ID 29",
                            "score": "30.0",
                            "children": []
                        },
                        {
                            "id": "34",
                            "name": "Id 34",
                            "score": "80.0",
                            "children": []
                        }
                    ]
                }
            ]
        },
        {
            "id": "16",
            "name": "ID 16",
            "score": "30.0",
            "children": [
                {
                    "id": "24",
                    "name": "ID 24",
                    "score": "35.0",
                    "children": []
                }
            ]
        }
    ]
}

我尝试使用php map()函数,但是无法正确进行转换。因此,请您发布一个摘要或为我指明正确的方向?

降落

这应该可以解决问题。我假设您当前的数组称为$oldArray

// will use this later
function reindex($array) {
    if (!empty($array['children'])) {
        $array['children'] = array_values($array['children']);
        foreach ($array['children'] as $key => $value) {
            $array['children'][$key] = reindex($value);
        }
    }
    return $array;
}

$newArray = array(
    'children' => array()
);

// refactor array into something more suitable
foreach ($oldArray as $oldArrayKey => $oldArrayValues) {

    $count = count($oldArrayValues);
    $currentArray = &$newArray['children'];

    for ($i = 0; $i < $count; $i = $i + 2) {

        $id = $oldArrayValues[$i];
        $value = $oldArrayValues[$i + 1];

        if (!isset($currentArray[$id])) {
            $currentArray[$id] = array(
                'id' => $id,
                'name' => 'ID ' . $id,
                'value' => $value,
                'children' => array()
            );
        }

        $currentArray = &$currentArray[$id]['children'];
    }
}

// reindex the children, or json_encode will treat ['children'] as an object instead of an array
$newArray = reindex($newArray);

echo json_encode($newArray);

让我知道您是否需要任何解释...

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

将php数组映射到嵌套的json

来自分类Dev

将 json 值映射到数组

来自分类Dev

将嵌套数组映射到父对象的键值对

来自分类Dev

RestKit:将嵌套数组映射到对象

来自分类Dev

使用深度嵌套的json对象映射到数组

来自分类Dev

将嵌套的json映射到余烬数据模型

来自分类Dev

Moshi将嵌套的JSON值映射到字段

来自分类Dev

如何使用 Javascript 将嵌套的 JSON 映射到 HTML 表

来自分类Dev

将SwiftyJSON映射到数组

来自分类Dev

如何将异构JSON数组映射到Java对象?

来自分类Dev

RestTemplate将JSON数组映射到对象列表

来自分类Dev

将JSON映射到Knockout可观察数组

来自分类Dev

将变化的json数据映射到可观察的数组

来自分类Dev

将数组的值作为参数映射到函数 PHP

来自分类Dev

哈希映射到JSON数组

来自分类Dev

解析 JSON 以映射到数组

来自分类Dev

将数组部分映射到新数组

来自分类Dev

将索引数组映射到坐标数组

来自分类Dev

将键数组映射到值数组

来自分类Dev

将数组映射到数组内部

来自分类Dev

将数组映射到对象数组

来自分类Dev

将数组映射到新数组

来自分类Dev

JavaScript:如何将具有嵌套数组的对象映射到表以进行React

来自分类Dev

将项目映射到嵌套数组中并将其存储在列表中

来自分类Dev

如何使用嵌套表单数组将服务器响应映射到角度反应表单

来自分类Dev

json.Marshal映射到JSON数组

来自分类Dev

将哈希数组反向映射到哈希

来自分类Dev

自动将数组映射到列表

来自分类Dev

将数组索引映射到矩阵