11

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?

3
  • post your php code please Commented Dec 13, 2016 at 7:50
  • don´t have php code, only have the result of my array Commented Dec 13, 2016 at 7:51
  • 2
    Down-voting for the lack of PHP code. Commented Dec 13, 2016 at 8:30

4 Answers 4

18

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']);
}
Sign up to request clarification or add additional context in comments.

2 Comments

I don´t use eloquent, I´m using a API to get the data
Sorry, I thought you want to make just first letter in upper case and the rest in lower case. Fixed the code.
4

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

use Illuminate\Support\Str;

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

Comments

2

$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();

1 Comment

@Antonio Morales I wonder why did you tick this answer? I've just tested this code and it doesn't give result you want. keyBy() will change indexes from 0, 1 etc to uppercased province string and will leave province as is.
1
for ($i = 0; $i < count($rp_shops); $i++) { 
    $rp_shops[$i]['province'] = strtoupper($rp_shops[$i]['province']); 
} 
dd($rp_shops) 

or

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.