0

there

I have a question in array manipulate.

I have a source array like this

 array:6 [
  0 => array:13 [
    "id" => 1
    "name" => "companyName"
    "code" => "1"
    "orderNumber" => 1
    "enabled" => true
    "createSpace" => false
    "description" => "description"
    "lft" => 1
    "lvl" => 0
    "rgt" => 4
    "createdAt" => DateTime {#911
      +"date": "2017-05-04 01:51:22.000000"
      +"timezone_type": 3
      +"timezone": "Asia/Shanghai"
    }
    "updatedAt" => DateTime {#909
      +"date": "2017-05-04 01:51:22.000000"
      +"timezone_type": 3
      +"timezone": "Asia/Shanghai"
    }
    "children" => array:1 [
      0 => array:13 [
        "id" => 7
        "name" => "departmentName"
        "code" => "7"
        "orderNumber" => 7
        "enabled" => true
        "createSpace" => false
        "description" => null
        "lft" => 2
        "lvl" => 1
        "rgt" => 3
        "createdAt" => DateTime {#914
          +"date": "2017-05-09 05:36:55.000000"
          +"timezone_type": 3
          +"timezone": "Asia/Shanghai"
        }

and now I use array_map function to rename the key name for my requirement, "name" to "text",

PHP code is here

$trees = array_map(function($tree) {
                return array(
                    'id' => $tree['id'],
                    'text' => $tree['name'],
                    'children' => $tree['children']
                );
            }, $trees);

and the result is

    array:6 [
      0 => array:3 [
        "id" => 1
        "text" => "departmentName"
        "children" => array:1 [
          0 => array:13 [
            "id" => 7
            "name" => "departmentName"
            "code" => "7"
            "orderNumber" => 7
            "enabled" => true
            "createSpace" => false
            "description" => null
            "lft" => 2
            "lvl" => 1
            "rgt" => 3
            "createdAt" => DateTime {#914
              +"date": "2017-05-09 05:36:55.000000"
              +"timezone_type": 3
              +"timezone": "Asia/Shanghai"
            }
            "updatedAt" => DateTime {#903
              +"date": "2017-05-09 05:36:55.000000"
              +"timezone_type": 3
              +"timezone": "Asia/Shanghai"
            }
            "children" => []
          ]
        ]
      ]
      1 => array:3 [
        "id" => 2
        "text" => "departmentName"
        "children" => []
      ]
      2 => array:3 [
        "id" => 3
        "text" => "departmentName"
        "children" => []
      ]
      3 => array:3 [
        "id" => 4
        "text" => "departmentName"
        "children" => []
      ]
      4 => array:3 [
        "id" => 5
        "text" => "departmentName"
        "children" => []
      ]
      5 => array:3 [
        "id" => 6
        "text" => "departmentName"
        "children" => []
      ]
]

as you can see, it's not work in the sub array that's in the "children" key, how could I change it with the unknown multidimensional array. Thank you.

Tony Gao
  • 25
  • 4

1 Answers1

0

add new key-value pair to your array, then remove the old pair. refer here

$arr[$newkey] = $arr[$oldkey];
unset($arr[$oldkey]);
Community
  • 1
  • 1
LF00
  • 27,015
  • 29
  • 156
  • 295