The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> PHP Development
|
Rename Array Index
Discuss Rename Array Index in the PHP Development forum on Dev Shed. Rename Array Index PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

February 22nd, 2013, 09:23 AM
|
|
Contributing User
|
|
Join Date: Jul 2010
Posts: 60
Time spent in forums: 1 Day 2 h 54 m 32 sec
Reputation Power: 3
|
|
|
Rename Array Index
Hi there,
I have an array .....
"nikeair" => array( data in here. etc);
and to display the items in the array I have the following code..
PHP Code:
foreach ($deals as $k => $v)
{
echo '<div id="wrapper">';
echo '<h3>'.strtoupper($k).' ' . 'DEALS'.'</h3>';
foreach ($v as $key => $val)
{
if ($val['id'] =="02")
{
echo "<div id=\"wrapper1\"><img class=\"logo\" src=\"".$val['logo']."\"><div class=\"title\">".$val['productTitle']."</div>";
echo "<div class=\"vcontainer\"><img class=\"vertical-img\" src=\"".$val['productImage']."\"><div class=\"description\">".$val['productDescription']."</div><div class=\"vpricing\"><div class=\"vrrp\">".$val['rrp']."<div class=\"vsalesprice\">".$val['salePrice']."</div></div></div></div></div>";
}
else
{
echo "<div id=\"wrapper1\"><img class=\"logo\" src=\"".$val['logo']."\"><div class=\"title\">".$val['productTitle']."</div>";
echo "<div class=\"container\"><img class=\"horizontal-img\" src=\"".$val['productImage']."\"><div class=\"description\">".$val['productDescription']."</div><div class=\"dealspricing\"><div class=\"rrp\">".$val['rrp']."</div><div class=\"salesprice\">".$val['salePrice']."</div></div></div></div>";
}
}
echo "</div>";
}
So what will output is the array index (nikeair) as the <h3> and display all the products in the array below. However the code I have literally takes the array index as the name ie "nikeair" however I would like to output "Nike Air" instead.
So I somehow need to change the value of $k from "nikeair" to "Nike Air" ??
Any help appreciated.
Volterony
|

February 22nd, 2013, 09:46 AM
|
|
|
|
I assume you are using nikeair as an example and really want to generically change merged lowercase names into their respective proper names. The only solution is to create a dictionary of the proper names which can be mapped to the index names. It is simple to take a proper name, change it to all lower case and remove spaces to get the index. Going the other way is impossible short of a look-up table.
__________________
There are 10 kinds of people in the world. Those that understand binary and those that don't.
|

February 22nd, 2013, 09:55 AM
|
|
Contributing User
|
|
Join Date: Jun 2009
Posts: 297
  
Time spent in forums: 3 Days 8 h 45 m 39 sec
Reputation Power: 5
|
|
|
Are you able to post the build of your array?
|

February 22nd, 2013, 09:56 AM
|
 |
pollyanna
|
|
Join Date: Jul 2012
Location: Germany
|
|
|
Hi,
if I remember correctly, those data is actually stored in an array -- thanks to some clueless "senior developer" forcing you to do it like that.
In that case, why can't you simply change the keys in the original array? Please don't tell me your "developer" doesn't let you ...
|

February 22nd, 2013, 10:00 AM
|
|
Contributing User
|
|
Join Date: Jul 2010
Posts: 60
Time spent in forums: 1 Day 2 h 54 m 32 sec
Reputation Power: 3
|
|
Quote: | Originally Posted by gw1500se I assume you are using nikeair as an example and really want to generically change merged lowercase names into their respective proper names. The only solution is to create a dictionary of the proper names which can be mapped to the index names. It is simple to take a proper name, change it to all lower case and remove spaces to get the index. Going the other way is impossible short of a look-up table. |
Ok so I need a function that will convert the proper name into the array index (with no spaces). Not sure how to do this so will do a bit of research.
cheers
|

February 22nd, 2013, 10:06 AM
|
|
|
|
Use 'strtolower' and 'str_replace'.
|

February 22nd, 2013, 10:29 AM
|
 |
pollyanna
|
|
Join Date: Jul 2012
Location: Germany
|
|
|
Again: Why can't you simply rename the array keys?
I mean, since you're stuck with this stupid solution, at leat use it.
|

February 22nd, 2013, 11:44 AM
|
|
Contributing User
|
|
Join Date: Jun 2009
Posts: 297
  
Time spent in forums: 3 Days 8 h 45 m 39 sec
Reputation Power: 5
|
|
|
I'm wondering if you need such as the index... If you post an example of an output of your array, we can maybe suggest an excellent way to use it for your goal...
|

February 27th, 2013, 07:24 AM
|
|
Contributing User
|
|
Join Date: Jul 2010
Posts: 60
Time spent in forums: 1 Day 2 h 54 m 32 sec
Reputation Power: 3
|
|
Hi there,
Sorry I have not followed on from this post and acknowledged people's help.
However I have managed to solve it.
Referring to this code:
PHP Code:
echo '<h3>'.strtoupper($k).' ' . 'DEALS'.'</h3>';
..... I wanted to change the value of $k from "nikeair" to "Nike Air".
However what I should really let you see is the full php to understand what the original problem was. If you look below, I was making a request from a directory called companies and was putting that into a variable called $make. In the companies directory, they were all named php files WITHOUT spaces, so when I was trying to output the name to <h3> is was displaying the ACTUAL directory name.
So to solve the problem, I just renamed the directory php files with an underscore and in then did this:
PHP Code:
echo '<h3>'.strtoupper(str_replace("_", " ",($make))).' '. 'SPECIAL OFFERS'.'</h3>';
So if the directory was called "nikeair.php" I called it "nike_air.php" and then stripped the underscore away. This then output the "Nike Air Special Offers" as I was aiming for.
Thanks for everyones help and advice. Much appreciated
Regards
Volterony
PHP Code:
<?php
$make = $_REQUEST['companies'];
if ($make == 'offers')
{
foreach ($deals as $k => $v)
{
echo '<div id="wrapper">';
echo '<h3>'.strtoupper(str_replace("_", " ",($k))).' ' . 'OFFERS'.'</h3>';
foreach ($v as $key => $val)
{
if ($val['id'] =="02")
{
echo "<div id=\"dealwrapper\"><img class=\"logo\" src=\"".$val['logo']."\"><div class=\"title\">".$val['productTitle']."</div>";
echo "<div class=\"vcontainer\"><img class=\"vertical-img\" src=\"".$val['productImage']."\"><div class=\"vdescription\">".$val['productDescription']."</div><div class=\"vpricing\"><div class=\"vrrp\">".$val['rrp']."<div class=\"vsalesprice\">".$val['salePrice']."</div></div></div></div></div>";
}
else
{
echo "<div id=\"dealwrapper\"><img class=\"logo\" src=\"".$val['logo']."\"><div class=\"title\">".$val['productTitle']."</div>";
echo "<div class=\"dealscontainer\"><img class=\"horizontal-img\" src=\"".$val['productImage']."\"><div class=\"dealsdescription\">".$val['productDescription']."</div><div class=\"dealspricing\"><div class=\"rrp\">".$val['rrp']."</div><div class=\"salesprice\">".$val['salePrice']."</div></div></div></div>";
}
}
echo "</div>";
}
}
else
{
echo '<div id="mainwrapper">';
echo '<h3>'.strtoupper(str_replace("_", " ",($make))).' '. 'SPECIAL OFFERS'.'</h3>';
foreach ($deals[$make] as $key => $val)
{
if ($val['id'] =="02")
{
echo "<div id=\"wrapper1\"><img class=\"logo\" src=\"".$val['logo']."\"><div class=\"title\">".$val['productTitle']."</div>";
echo "<div class=\"vcontainer\"><img class=\"vertical-img\" src=\"".$val['productImage']."\"><div class=\"vdescription\">".$val['productDescription']."</div><div class=\"vpricing\"><div class=\"vrrp\">".$val['rrp']."<div class=\"vsalesprice\">".$val['salePrice']."</div></div></div></div></div>";
}
else
{
echo "<div id=\"wrapper1\"><img class=\"logo\" src=\"".$val['logo']."\"><div class=\"title\">".$val['productTitle']."</div>";
echo "<div class=\"dealscontainer\"><img class=\"horizontal-img\" src=\"".$val['productImage']."\"><div class=\"dealsdescription\">".$val['productDescription']."</div><div class=\"dealspricing\"><div class=\"rrp\">".$val['rrp']."</div><div class=\"salesprice\">".$val['salePrice']."</div></div></div></div>";
}
}
echo "</div>";
}
?>
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|