PHP Laravel将数组值转换为大写

时间:2016-12-13 07:50:27

标签: php laravel laravel-5 laravel-5.2 laravel-5.3

我的Laravel项目中有一个包含倍数值的数组:

array:1434 [▼
  0 => array:53 [▼
    "contact" => "ANA (dependienta)"
    "mail" => "example@mail.com"
    "phone2" => ""
    "phone3" => ""
    "web" => "0"
    "active" => true
    "province" => "Zaragoza"

  ]
  1 => array:53 [▼
    "contact" => "JACKELINE * VIVIANA"
    "mail" => "mail@example.com"
    "phone2" => ""
    "phone3" => ""
    "web" => "0"
    "active" => true
    "province" => "Barcelona"

  ]

我想将变换为大写只有省值,我想得到这个结果:

array:1434 [▼
  0 => array:53 [▼
    "contact" => "ANA (dependienta)"
    "mail" => "example@mail.com"
    "phone2" => ""
    "phone3" => ""
    "web" => "0"
    "active" => true
    "province" => "ZARAGOZA"

  ]
  1 => array:53 [▼
    "contact" => "JACKELINE * VIVIANA"
    "mail" => "mail@example.com"
    "phone2" => ""
    "phone3" => ""
    "web" => "0"
    "active" => true
    "province" => "BARCELONA"

  ]

使用Laravel Collection或其他替代品存在任何方法或方法吗?

4 个答案:

答案 0 :(得分:4)

如果您使用Eloquent从数据库获取数据,则可以create an accessor

public function getProvince($value)
{
    return strtoupper($value);
}

如果没有,您可以手动更改它:

for ($i = 0; $i < count($data); $i++) {
    $data[$i]['province'] = strtoupper($data[$i]['province']);
}

答案 1 :(得分:1)

$collection是对象数组,然后尝试使用这种方式:

$collection = collect($array);

$keyed = $collection->keyBy(function ($item) {
    return strtoupper($item['province']);
});

$keyed->all();

答案 2 :(得分:0)

for ($i = 0; $i < count($rp_shops); $i++) { 
    $rp_shops[$i]['province'] = strtoupper($rp_shops[$i]['province']); 
} 
dd($rp_shops) 

foreach ($rp_shops as $key => $rp_shop) { 
    $rp_shops[$key]['province'] = strtoupper($rp_shop['province']); 
} 
dd($rp_shops)  

答案 3 :(得分:0)

Str::upper方法将给定的字符串转换为大写:

use Illuminate\Support\Str;

$string = Str::upper('laravel');