如何也将列设置为 Upper 并删除集合 laravel 中的符号 _

How to make column to Upper too and remove symbol _ in collection laravel

所以我想将列名和值设为大写

示例:

$collection = collect([
    ['name' => 'Sally'],
    ['school_name' => 'Arkansas'],
    ['age' => 28]
]);

$flattened = $collection->flatMap(function ($values) {
    return array_map('strtoupper', $values);
});

$flattened->all();

// ['name' => 'SALLY', 'school_name' => 'ARKANSAS', 'age' => '28'];

我也想'name, school_name, age'到Upper。该代码只使值更高但不是列名

<?php

$arr = ['name' => 'Sally', 'school_name' => 'Arkansas', 'age' => '28'];

$out = [];
$replace = '';
array_walk($arr, function($val, $key) use (&$out, $replace) {
    $key2 = strtoupper($key);
    $key2 = str_replace('_', $replace, $key2);
    $out[$key2] = strtoupper($val);
});

echo "before:\n";
print_r($arr);

echo "after:\n";
print_r($out);

给出结果:

before:
Array
(
    [name] => Sally
    [school_name] => Arkansas
    [age] => 28
)
after:
Array
(
    [NAME] => SALLY
    [SCHOOLNAME] => ARKANSAS
    [AGE] => 28
)

$replace 更改为您想要的任何字符或保留为空字符串以删除下划线。

PHP 手册参考:

  1. array_walk
  2. strtoupper
  3. str_replace
  4. Anonymous functions
  5. Passing by Reference
$collection = collect([
        ['name' => 'Sally'],
        ['school_name' => 'Arkansas'],
        ['age' => 28]
    ]);


$flattened = $collection->flatMap(function ($items) {
    //iterating over the array
    foreach($items as $itemKey => $itemValue):
        //replaccing the _ with null
        $itemKey = str_replace(['_'],[],$itemKey);
        //converting the keys and value to uppercase
        return [ mb_strtoupper($itemKey) => mb_strtoupper($itemValue) ];

    endforeach;
});

$flattened->all();

实际 VS 结果

Collection {#518 ▼
  #items: array:3 [▼
    0 => array:1 [▼
      "name" => "Sally"
    ]
    1 => array:1 [▼
      "school_name" => "Arkansas"
    ]
    2 => array:1 [▼
      "age" => 28
    ]
  ]
}
Collection {#600 ▼
  #items: array:3 [▼
    "NAME" => "SALLY"
    "SCHOOLNAME" => "ARKANSAS"
    "AGE" => "28"
  ]
}

如果您想清理代码,这是我创建的宏

\Illuminate\Support\Collection\Collection::macro('makeUpperAndReplace',function($replaceItems = []){

    return $this->flatMap(function ($items) use ($replaceItems) {
        //iterating over the array
        foreach($items as $itemKey => $itemValue):
            //replaccing the $replaceItems with null
            $itemKey = str_replace($replaceItems,[],$itemKey);
            //converting the keys and value to uppercase

            return [ mb_strtoupper($itemKey) => mb_strtoupper($itemValue) ];

        endforeach;
    });
});

宏可以用作

$collection->makeUpperAndReplace(['_','-'])->dd();

如有任何问题请评论

<?php $collection = array(
'name' => 'Sally',
'school_name' => 'Arkansas',
'age' => 28);
$collection = array_change_key_case($collection, CASE_UPPER);
fixArrayKey($collection);
print_r($collection);
function fixArrayKey(&$collection){
$collection = array_combine(array_map('_map',array_keys($collection)),array_values($collection));
foreach($collection as $key=>$val)
{
    if(is_array($val)) fixArrayKey($collection[$key]);
}
}
function _map($str){return str_replace("_","",$str);}
?>

结果

Array ( [NAME] => Sally [SCHOOLNAME] => Arkansas [AGE] => 28 )