tarquino Posted January 8, 2015 Share Posted January 8, 2015 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>" } ?> Quote Link to comment Share on other sites More sharing options...
Strider64 Posted January 8, 2015 Share Posted January 8, 2015 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. Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted January 8, 2015 Share Posted January 8, 2015 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 2 Quote Link to comment Share on other sites More sharing options...
tarquino Posted January 9, 2015 Author Share Posted January 9, 2015 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. Quote Link to comment Share on other sites More sharing options...
tarquino Posted January 9, 2015 Author Share Posted January 9, 2015 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? Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted January 9, 2015 Share Posted January 9, 2015 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. Quote Link to comment Share on other sites More sharing options...
tarquino Posted January 21, 2015 Author Share Posted January 21, 2015 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. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted January 21, 2015 Share Posted January 21, 2015 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. Quote Link to comment Share on other sites More sharing options...
tarquino Posted January 21, 2015 Author Share Posted January 21, 2015 (edited) 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 January 21, 2015 by tarquino Quote Link to comment Share on other sites More sharing options...
ginerjm Posted January 21, 2015 Share Posted January 21, 2015 Whatever. I'm confused and can't make sense from your writings. Quote Link to comment Share on other sites More sharing options...
CroNiX Posted January 21, 2015 Share Posted January 21, 2015 (edited) 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 January 21, 2015 by CroNiX Quote Link to comment Share on other sites More sharing options...
tarquino Posted January 22, 2015 Author Share Posted January 22, 2015 (edited) 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 January 22, 2015 by tarquino Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted January 22, 2015 Share Posted January 22, 2015 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? Quote Link to comment Share on other sites More sharing options...
Barand Posted January 22, 2015 Share Posted January 22, 2015 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" ) ) ); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.