Jump to content

convert all keys to lowercase using


tarquino

Recommended Posts

i am trying to convert all keys to lowercase and i was wondering if it is really possible to do using array_map to convert all keys to lowercase or using array_reduce.

what i am trying to do is to gather details from keys and display it on the page as links that redirect to the details page, i managed to convert it into lowercase, however it did not gather the records mainly due to keys being capitalised and me trying to gather those records using lowercase.

 

to put things into perspective here is the code:

 

<?php
$cakesmade = array();
$cakesmade = array(
				"kate" => array(
					"cake" => array(
						"cakeingredients" => "egg, flour"),
					"Lovely Chocolate Cake" => array(
						"cakeingredients" => "chocolate, eggs, flour"),
					"amazing cake" => array(
						"cakeingredients" => "lemons, flour")
					),	
				);
?>


 

//links
<?php foreach($cakesmade as $id => $donecake) 
foreach($donecake as $bakedcake => $description) {
echo "<a href='/user/$id/baked/$bakedcake'>$bakedcake</a>" }
?>

 

 

Link to comment
Share on other sites

Why don't you just output the strings to lower case using strtolower() : http://php.net/manual/en/function.strtolower.php  that way you don't have no fuss no muss? That's what I would do. 

 

thanks strider64, i did try that before, however that did not work out successfully. the keys in the array remained the same and the only thing it changed was the url. (i was using url rewrite in htaccess), when would click it, it would not go to the intended page.

Link to comment
Share on other sites

Have you tried using array_change_key_case()? More information can be found here:

http://php.net/manual/en/function.array-change-key-case.php

 

thanks cyberbot. will this work when one uses the array as the name of the product (ie.Lovely Chocolate Cake in the details page)  and url rewrites to convert spaces to a -.and lowercase.(ie. example.com/user/$id/baked/$bakedcake) using htaccess? or will i come accross problems?

Link to comment
Share on other sites

thanks cyberbot. will this work when one uses the array as the name of the product (ie.Lovely Chocolate Cake in the details page)  and url rewrites to convert spaces to a -.and lowercase.(ie. example.com/user/$id/baked/$bakedcake) using htaccess? or will i come accross problems?

 

I honestly don't know. If that is a problem, you could try Strider64's suggestion. Or maybe you could rethink how the array information is stored. You could, for example, store the actual name of the cake at the same level as "cakeingredients"...and then do whatever you want with the keys.

Link to comment
Share on other sites

  • 2 weeks later...

I honestly don't know. If that is a problem, you could try Strider64's suggestion. Or maybe you could rethink how the array information is stored. You could, for example, store the actual name of the cake at the same level as "cakeingredients"...and then do whatever you want with the keys.

 

hi

i managed to do that, although i still have the same problem i was trying to solve in the first place.

the thing is that whenever i gather those values to direct to the specific product page, if the values are in upper case and the url gets lowercased, then it does not gather the detail page for that specific product. 

 

i am thinking is there a way to completely make the url completely case insensitive so that when one clicks the url:

example.com/lovely-chocolate-cakes

it automatically displays the values from the array: 

Lovely%20Chocolate%20Cakes

while not replacing the url.

 

 

i know this can be done manually for each url, is there a more simpler way to do this for all urls?

 

your input is welcome.

Link to comment
Share on other sites

Why does the url 'get lowercased'? Something you are doing elsewhere?

 

If you have data stored in one way why would you not use that data correctly (as is) to build your URL? Or did you not build your file/folder structure using the same exact values as the data?

 

You do realize that you can build your anchor/links using a modified version of the data for the 'displayed' portion and still use the original stored version for the href portion? This has got to work unless you did alter how you used the data previously and now can't make it match.

 

As for the 'foreach' method of altering the case and not getting permanent results - try using & on your foreach arguments. It's in the manual.

Link to comment
Share on other sites

hi, 

i want to make all urls lowercased(and transform spaces with a hyphen) so they are uniformed throughout, all lowercased urls.
the data has always remained the same, not that i have unknowingly changed. i am using htaccess to make clean urls, currently it does make clean urls, although it does not match the array (which happens to have spaces and capital letters).

 

for example:

 

Below this it does not work: it does not go to the intended page. ie. the values of the url are not matched to those of the array

example.com/user/kate/baked/lovely-chocolate-cake

i want my urls to look like the above with hyphens and lowercase.

example.com/user/kate/baked/Lovely-Chocolate-Cake

---

This line below it works:  this works and goes to te intended page. ie. the values of the url match those of the array, but its not how i want the url to look like (%20 and capitalized letters)

example.com/user/kate/baked/Lovely%20Chocolate%20Cake

any ideas on how to try to solve the problem. 

 

the colouring on the code isnt working, so ammended that.

Edited by tarquino
Link to comment
Share on other sites

It's not enough to just make the LINKS lowercase and with dashes. That's easy peasy to do when building the links/anchors.

 

$link = "A New Product";
$link = strtolower(str_replace(' ', '-', $link));
echo $link; // a-new-product
 

Your app has to RESPOND to those lowercased/dashed urls as well, which you haven't told us or shown us how you are doing that. Right now they respond to the upper cased links with spaces in it because you have it coded that way somewhere.

Edited by CroNiX
Link to comment
Share on other sites

It's not enough to just make the LINKS lowercase and with dashes. That's easy peasy to do when building the links/anchors.

 

$link = "A New Product";
$link = strtolower(str_replace(' ', '-', $link));
echo $link; // a-new-product
 

Your app has to RESPOND to those lowercased/dashed urls as well, which you haven't told us or shown us how you are doing that. Right now they respond to the upper cased links with spaces in it because you have it coded that way somewhere.

 

no i did not change anything. everything is as it is. for some reason the links i transform to lowercase, do not match the array.

 

This is what this script does. it checks whether the cake is valid. if the cake is valid it displays the cake name, however if the cake is invalid, it says it is.

to make it easier to troubleshoot, i decided to use the raw link instead of the htaccess pretty links.

//cake.php
<?php

$cakesmade = array();
$cakesmade = array(
				"kate" => array(
					"cake" => array(
						"cakeingredients" => "egg, flour"),
					"Lovely Chocolate Cake" => array(
						"cakeingredients" => "chocolate, eggs, flour"),
					"amazing cake" => array(
						"cakeingredients" => "lemons, flour")
					),	
					
				);

if(isset($_GET["id"])) { 
	$id = $_GET["id"]; 
	// check if user valid
	if(isset($cakesmade[$id]))
	{	
	$username = $cakesmade[$id];
		
	}
	else { echo "<h1>user does not exist</h1>"; }

} 
if(isset($_GET["product"])) {
	$products = $_GET["product"];
	if(isset($cakesmade[$id][$products])) 
	{ 
		
		$product = $cakesmade[$id];
	 }
	 else {
	 echo "<h1>cake is invalid</h1>";
	 exit;
	 }
}

?>

 <?php if(isset($username)) { ?>
<h1>User: <?php echo $id; ?></h1>
<h2>Product: <?php
$cakesmade = array_change_key_case($cakesmade, CASE_LOWER);
foreach($cakesmade as $id => $cake)
foreach($cake as $bakedcake => $description)
 {	
   $bakedcake = strtolower($bakedcake);
   $str = "cake.php?id={$id}&product={$bakedcake}";
   $lower = strtolower($str);
   $url = str_replace(' ', '-', $lower);
   echo "<a href='{$url}'>" . $url . "</a><br>";  
 } ?></h2>
<?php } ?>

if i delete the strtolower and str_replace part of the code and use simply $str it works fine. the problem only occurs when i try to lowercase url is used, it says the cake is invalid (maybe due to being case sensitive).

 

hope this makes it easier to understand what i am trying to do.

Edited by tarquino
Link to comment
Share on other sites

It seems like the problem is caused by the following line:

if(isset($cakesmade[$id][$products]))

Basically, the GET variable for "product" is passed with lower case letters. The above test is made against an array where the keys are still mixed case.

 

Have you tried moving the call to array_change_key_case() right after the $cakesmade variable is created?

Link to comment
Share on other sites

Avoid problems with spaces and lowercase entirely by restructuring your array

$cakesmade = array(
                "kate" => array(
                     0=>array(
                        "cakename" => "cake",
                        "cakeingredients" => "egg, flour"
                        ),
                     1=>array(
                        "cakename" => "Lovely Chocolate Cake",
                        "cakeingredients" => "chocolate, eggs, flour"
                        ),
                     2=>array(
                        "cakename" => "amazing cake",
                        "cakeingredients" => "lemons, flour"
                        )
                     )
                );

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.