将键添加到数组PHP中尚无键的值

时间:2019-09-05 09:16:04

标签: php arrays sorting

给出休止数组$array

Array
 (
  [1] => Array
    (
        [0] => 267
        [location_id_keep] => 261
    )
  [2] => Array
    (
        [0] => 266
        [location_id_keep] => 262
    )
  [3] => Array
    (
        [0] => 2669
        [1] => 2670
        [location_id_keep] => 2668
    )
  [4] => Array
    (
        [0] => 266
        [1] => 2670
        [location_id_keep] => 2668
    )
  )

我想将没有[[location_id_keep]键的值添加到每个数组中,这些键如下:[location_id_delete],以便获得以下输出:

Array

(

[1] => Array
    (
        [location_id_delete] => 267
        [location_id_keep] => 261
    )

[2] => Array
    (
        [location_id_delete] => 266
        [location_id_keep] => 262
    )
[3] => Array
    (
        [location_id_delete] => [2669, 2670]
        [location_id_keep] => 2668
    )

[4] => Array
    (
        [location_id_delete] => [266, 2670]
        [location_id_keep] => 2668
    )
)

是否有任何方法可以在分配给没有键的现有值的数组内添加键?

6 个答案:

答案 0 :(得分:3)

这应该可行,但是我建议您重建数据的结构。 Demo

foreach($arrays as &$array){
    if($array["location_id_keep"]){
        $temp_array["location_id_keep"] = $array["location_id_keep"];
        unset($array["location_id_keep"]);
    }
    $count = count($array);
    if($count == 1) {
        $temp_array["location_id_delete"] = current($array);
    }elseif($count > 1){
        $temp_array["location_id_delete"] = array_values($array);
    }
    $array = $temp_array;
}

答案 1 :(得分:1)

您可以尝试使用array_walk()array_filter()。 array_filter用于过滤以数字为键的值(在这里您指location_id_delete)。

$array = [[0 => 267, 'location_id_keep' => 261],[0 => 266, 'location_id_keep' => 262],[0 => 2669, 1 => 2670, 'location_id_keep' => 2668], [0 => 266, 1 => 2670, 'location_id_keep' => 2668]];

array_walk($array, function (&$val) {
    $val = [
       'location_id_delete' => array_filter($val, 'is_numeric', ARRAY_FILTER_USE_KEY), 
       'location_id_keep' => $val['location_id_keep']
    ];
});

print_r($array);

工作demo

答案 2 :(得分:1)

这是摘要,

array_walk($arr, function (&$v) {
    // keeping location_id_keep aside to perform operation of replacing keys
    $temp = array_diff_key($v, ['location_id_keep' => '']);
    if (!empty($temp)) {
        // getting the count of sub arrays other than location_id_keep
        $cnt = count($temp);
        foreach ($temp as $key => $value) {
            // if one record then directly assign data
            if ($cnt == 1) {
                $v['location_id_delete'] = $value;
            } else{ // assign multiple data in an arrat
                $v['location_id_delete'][] = $value;
            }
            // unset numeric index or other than location_id_keep
            unset($v[$key]);
        }
    }
});

array_walk —将用户提供的函数应用于数组的每个成员
array_diff_key —使用比较键计算数组的差异

Demo

输出:-

Array
(
    [1] => Array
        (
            [location_id_keep] => 261
            [location_id_delete] => 267
        )

    [2] => Array
        (
            [location_id_keep] => 262
            [location_id_delete] => 266
        )

    [3] => Array
        (
            [location_id_keep] => 2668
            [location_id_delete] => Array
                (
                    [0] => 2669
                    [1] => 2670
                )

        )

    [4] => Array
        (
            [location_id_keep] => 2668
            [location_id_delete] => Array
                (
                    [0] => 266
                    [1] => 2670
                )

        )

)

答案 3 :(得分:1)

这将起作用

Array
(
    [1] => Array
        (
            [location_id_keep] => 261
            [location_id_delete] => 267
        )

    [2] => Array
        (
            [location_id_keep] => 262
            [location_id_delete] => 266
        )

    [3] => Array
        (
            [location_id_keep] => 2668
            [location_id_delete] => 2669,2670
        )

    [4] => Array
        (
            [location_id_keep] => 2668
            [location_id_delete] => 266,2670
        )

)

输出

   root
|-- userid: string (nullable = true)
|-- name: string (nullable = true)
|-- applications: array (nullable = true)
|    |-- element: struct (containsNull = true)
|    |    |-- applicationid: string (nullable = true)
|    |    |-- createdat: string (nullable = true)
|    |    |-- source_name: string (nullable = true)
|    |    |-- accounts: array (nullable = true)
|    |    |    |-- element: struct (containsNull = true)
|    |    |    |    |-- applicationcreditreportaccountid: string 
                                                   (nullable = true)
|    |    |    |    |-- account_type: integer (nullable = true)
|    |    |    |    |-- account_department: string (nullable = true)

答案 4 :(得分:1)

功能解决方案

foreach($arrays as &$array){
    // get numeric key items 
    $temp = array_filter($array, 'is_numeric',ARRAY_FILTER_USE_KEY);
    // remove them from array and return under new key
    $array = array_diff($array, $temp) + ['location_id_delete'=>$temp];
}

demo

答案 5 :(得分:1)

您可以将foreachimplodeunset一起使用

  foreach($a as $k => &$v){
    $location = $v['location_id_keep'];
    unset($v['location_id_keep']);
    $r[] = [
      'location_id_delete' => implode(',',$v), 
      'location_id_keep'   => $location
    ];
  }

实时示例:https://3v4l.org/kqCVH