Home  >  Q&A  >  body text

php - Remove the outermost key of a multi-dimensional array and retain the value

This is the array. After using array_values, I found that it seems to have been reordered according to the ID impolde prompts that the array cannot be used as a parameter, the object is also used, and the json is also converted. How should I go?

renew:

This is enough, but after my new retlist 0 array_colum, the printed result [0] went to the end of the array.

phpcn_u1582phpcn_u15822698 days ago2483

reply all(3)I'll reply

  • 代言

    代言2017-06-23 09:13:13

    If you want to convert JSON to a PHP array, the method is as follows

    First of all, your data format is JSON, which needs to be converted into a PHP array first.

    $a = json_decode($a, TRUE);
    The second parameter of

    json_decode is TRUE, which means the key names are retained. Otherwise, after the JSON is converted to the PHP array, the key names of the PHP array will be reordered.

    Then use array_column to process the $a['data'] values ​​in your data, or use array_values to rearrange the array values.

    $a['Data'] = array_column($a['Data'], NULL);
    // 或者
    $a['Data'] = array_values($a['Data']);

    If you want to convert PHP to JSON, but make Data in Data be an array[].

    As long as in the PHP array, first process $a['Data'] according to the above array_column or array_values, and then run it directly

    $a = json_encode($a);

    Note that the second parameter of json_encode must not be used JSON_FORCE_OBJECT, otherwise Data will still become an object{}.

    Update

    After reading your code, I seem to know what you are going to do. Do you want to find the item in the original array whose ID value is equal to $makeupId, and then advance it to the first one in the array? If so, your code will be complicated to write. Through this magical function array_column(), you can easily achieve your needs. The code is as follows

    $newarr = [];
    $ret = array_column($ret, NULL, 'ID');
    
    # 上面这行代码做了一个神奇的事情,就是将源代码的顺序保持不变,然后将ID作为了键值,
    # 生成了一个关联数组。
    #
    # 下面简单讲一下array_column的神奇之处,它可以将像你这种数据格式的
    # 二维数组(矩阵型二维数组)中的某一列拆出来,作为键值,另外一列拆出来作为键名,
    # 形成关联数组。
    #
    # 当然,这个函数的定义array_column($source, $value_field[, $key_field]);
    # 其中,
    #     $source为源数组,
    #     $value_field为需要提取作为键值的名字,如果为NULL则将整个内层数组作为值
    #         (也就是保持原来的值不变,这个一般用于数组键名的重新生成)
    #     $key_field为需要提取作为键名的名字,此处为'ID',如果为NULL或者省略,
    #         则会重新从0开始编号(配合上面$value_field为NULL可以重新排序数组)
    #
    # 然后给个简单说明:
    # $a = [
    #     't' => ['id' => 1, 'name' => 'a'],
    #     'u' => ['id' => 2, 'name' => 'b']
    # ];
    #
    # 通过array_column($a, NULL)得到的是
    # $a = [
    #     ['id' => 1, 'name' => 'a'],
    #     ['id' => 2, 'name' => 'b']
    # ];
    #
    # 通过array_column($a, 'id')得到的是
    # $a = [1, 2];
    #
    # 通过array_column($a, NULL, 'id')得到的是
    # $a = [
    #     1 => ['id' => 1, 'name' => 'a'],
    #     2 => ['id' => 2, 'name' => 'b']
    # ];
    #
    # 通过array_column($a, 'name', 'id')得到的是
    # $a = [
    #     1 => 'a'
    #     2 => 'b'
    # ];
    
    # 说了这么多你应该理解了这个函数的用法,上面那句
    # $ret = array_column($ret, NULL, 'ID')将$ret变成了关联数组,
    # 这样$ret[$makeupId]就可以直接找到你需要提取的项了,是不是很方便?
    #
    # 下面接着给代码
    
    if(!empty($ret[$makeupId])) {
        $newarr = $ret[$makeupId]; // 取出新对象
        unset($ret[$makeupId]); // 删除原有数组中的对象
    }
    
    $ret = array_column($ret, NULL);
    # 上面这行将关联数组再转回了顺序数组,但是别忘了,
    # 因为满足$makeupId的项被取到了$newarr,然后原数组中的$makeupId项被删掉,
    # 所以转回来的数组是不含满足$makeupId的项的。
    
    # 接下来,将$newarr推到数组之前,
    # 此处注意,array_unshift()方法直接操作原数组,所以不需要赋值
    array_unshift($ret, $newarr);

    End of code


    Then, let me talk about why you use array_values() and array_column() to mess up the order. Because these two functions will be renumbered, and the order of their numbers is not the label order of your key names, that is to say, if you manually write the key name as 0,1,2,3, it will not cause it to be renumbered. Time is sorted according to 0,1,2,3. The order in which it is renumbered depends on the order in which your code is run. In other words, your last...[0] = $newarr is not actually at the beginning of the array, but at the end of the entire array. It's just that its key name is 0, so no matter how hard you try, this 0 corresponds to The elements will all run to the end.


    Update 2

    There are many comments and explanations in the above code, here is the pure code:

    $newarr = [];
    $ret = array_column($ret, NULL, 'ID');
    
    if(!empty($ret[$makeupId])) {
        $newarr = $ret[$makeupId]; // 取出新对象
        unset($ret[$makeupId]); // 删除原有数组中的对象
    }
    
    $ret = array_column($ret, NULL);
    array_unshift($ret, $newarr);
    
    var_dump($ret);

    Update 3

    Let’s add some more content. Here are several feasible solutions besides array_column.

    Option 1:

    This solution is based on your original code modification.
    Add to the second to last line of your code ($retlist = ...

    above)
    ksort($ret_list, SORT_NUMERIC);

    Option 2:

    This solution is also based on your original code modification.
    Change $retlist[0] = $newarr; in your code to array_unshift($retlist, $newarr);

    As mentioned above, array_column and array_values will be renumbered. The order of renumbering depends on the order in which you define the corresponding values, not the numerical order of the key names, so the above modification is pushed by the array header. The entry replaces the value definition and initialization of $retlist[0].

    Option 3:

    This is also modified based on your code.
    Before you traverse, define $retlist[0] = [], and then when you traverse to an item that matches $makeupId, directly assign it to $retlist[0]. In this way, due to the prior definition $retlist[0], so even if it is renumbered, its order will still be first.

    reply
    0
  • 滿天的星座

    滿天的星座2017-06-23 09:13:13

    Does this mean that just removing the key value is equivalent to removing one layer of the multi-dimensional array? If so, I wrote a rough example, you can follow this idea to implement it, as shown in the picture above

    reply
    0
  • 欧阳克

    欧阳克2017-06-23 09:13:13

    It is best to convert json data into a php array for easier processing. Otherwise it will be difficult to deal with.

    reply
    0
  • Cancelreply