How to Convert Array Values to Lowercase in PHP

How to convert array values to lowercase in PHP?

use array_map():

$yourArray = array_map('strtolower', $yourArray);

In case you need to lowercase nested array (by Yahya Uddin):

$yourArray = array_map('nestedLowercase', $yourArray);

function nestedLowercase($value) {
if (is_array($value)) {
return array_map('nestedLowercase', $value);
}
return strtolower($value);
}

Uppercase array keys and Lowercase array values (input from parse_str)

Flip your logic and uppercase everything and then recursively lower case the values:

parse_str(strtoupper($string), $array);    
array_walk_recursive($array, function(&$v, $k) { $v = strtolower($v); });

This will work for multiple dimensions such as:

$string = "appliCAation=webCALL&Arg1=ABC&arG2=xyZ&someMore=Dec-1950&a[yZ][zzz]=efG";

Yielding:

Array
(
[APPLICAATION] => webcall
[ARG1] => abc
[ARG2] => xyz
[SOMEMORE] => dec-1950
[A] => Array
(
[YZ] => Array
(
[ZZZ] => efg
)
)
)

After rereading the question I see you want to be able to change or control whether the keys and values are uppercase or lowercase. You can use() a parameters array to use as function names:

$params = ['key' => 'strtoupper', 'val' => 'strtolower'];
parse_str($params['key']($string), $array);
array_walk_recursive($array, function(&$v, $k) use($params){ $v = $params['val']($v); });

To change just the keys, I would use a Regular Expression on the original string:

$keys = 'strtoupper';
$string = preg_replace_callback('/[^=&]*=/', function($m) use($keys) { return $keys($m[0]); }, $string);
parse_str($string, $array);

[^=&]*= is a character class [] matching characters that are ^ not = or & 0 or more times * followed by =.

And finally, here is one that will do keys and values if you supply a function name (notice val is empty), if not then it is not transformed:

$params = ['key' => 'strtoupper', 'val' => ''];
$string = preg_replace_callback('/([^=&]*)=([^=&]*)/',
function($m) use($params) {
return (empty($params['key']) ? $m[1] : $params['key']($m[1]))
.'='.
(empty($params['val']) ? $m[2] : $params['val']($m[2]));
}, $string);
parse_str($string, $array);

strtolower() on an array

You may mean strtolower:

<?php echo strtolower($rdata['batch_id']); ?>

Can I make the contents of my array all lowercase?

echo '<span><a href="' . strtolower($url) . '"> '. strtolower($bikes) .'</a></span>';

https://www.w3schools.com/php/func_string_strtolower.asp

Change array keys to lowercase

You can normalise the keys and store them and their corresponding values in a temporary array and assign it to your original $pairs array.

Like so:

$normalised = [];

foreach($pairs as $key => $value) {
$normalised[$this->normalizeKey($key)] = $value;
};

$pairs = $normalised;

Edit 2022: Or better yet, use @Sherif's answer as it uses a standand library function

PHP Laravel Convert array value to upper case

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']);
}

Make all the strings of a string array into lower case except for the first word

Use ucfirst and array_map with strtolower

echo ucfirst(implode(', ', array_map('strtolower',$ingredient_a)));

Laravel make array and use strtolower

Solved

// get my table row
$sibarBrandsArray = SidebarManager::first();
// get my row column
$getBrandColumn = $sibarBrandsArray->brands;
// separate data in that column with comma
$separateBrands = explode(',', $getBrandColumn);
// lowercase each separated data
$brandsArray = array_map('strtolower', $separateBrands);
// dump the result
dd($brandsArray);

Result

array:2 [
0 => "no brand"
1 => "ddfg"
]


Related Topics



Leave a reply



Submit