Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Laravel Convert array value to upper case

I have an array with multiples values in my Laravel project:

array:1434 [▼
  0 => array:53 [▼
    "contact" => "ANA (dependienta)"
    "mail" => "[email protected]"
    "phone2" => ""
    "phone3" => ""
    "web" => "0"
    "active" => true
    "province" => "Zaragoza"

  ]
  1 => array:53 [▼
    "contact" => "JACKELINE * VIVIANA"
    "mail" => "[email protected]"
    "phone2" => ""
    "phone3" => ""
    "web" => "0"
    "active" => true
    "province" => "Barcelona"

  ]

I want transform to upper case only the province value, I want to get this result:

array:1434 [▼
  0 => array:53 [▼
    "contact" => "ANA (dependienta)"
    "mail" => "[email protected]"
    "phone2" => ""
    "phone3" => ""
    "web" => "0"
    "active" => true
    "province" => "ZARAGOZA"

  ]
  1 => array:53 [▼
    "contact" => "JACKELINE * VIVIANA"
    "mail" => "[email protected]"
    "phone2" => ""
    "phone3" => ""
    "web" => "0"
    "active" => true
    "province" => "BARCELONA"

  ]

Exists any method or way to make this with Laravel Collection or other alternatives?

like image 311
RichardMiracles Avatar asked Dec 13 '16 07:12

RichardMiracles


3 Answers

The Str::upper method converts the given string to uppercase:

use Illuminate\Support\Str;

$string = Str::upper('laravel');
like image 184
Angel Avatar answered Nov 07 '22 04:11

Angel


If you're getting data from DB by using Eloquent, you could create an accessor

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

If not, you could change it manually:

for ($i = 0; $i < count($data); $i++) {
    $data[$i]['province'] = strtoupper($data[$i]['province']);
}
like image 24
Alexey Mezenin Avatar answered Nov 07 '22 05:11

Alexey Mezenin


$collection is the array of objects, then try to use this way :

$collection = collect($array);

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

$keyed->all();
like image 31
Bara' ayyash Avatar answered Nov 07 '22 04:11

Bara' ayyash