PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 22nd, 2013, 09:23 AM
volterony volterony is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2010
Posts: 60 volterony User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #2  
Old February 22nd, 2013, 09:46 AM
gw1500se gw1500se is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Jul 2003
Posts: 2,886 gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 1 Year 2 Weeks 3 Days 8 h 22 m 27 sec
Reputation Power: 581
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.

Reply With Quote
  #3  
Old February 22nd, 2013, 09:55 AM
Triple_Nothing Triple_Nothing is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2009
Posts: 297 Triple_Nothing User rank is Corporal (100 - 500 Reputation Level)Triple_Nothing User rank is Corporal (100 - 500 Reputation Level)Triple_Nothing User rank is Corporal (100 - 500 Reputation Level)Triple_Nothing User rank is Corporal (100 - 500 Reputation Level) 
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?

Reply With Quote
  #4  
Old February 22nd, 2013, 09:56 AM
Jacques1's Avatar
Jacques1 Jacques1 is online now
pollyanna
Click here for more information.
 
Join Date: Jul 2012
Location: Germany
Posts: 1,881 Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 9 h 31 m 10 sec
Reputation Power: 813
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 ...

Reply With Quote
  #5  
Old February 22nd, 2013, 10:00 AM
volterony volterony is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2010
Posts: 60 volterony User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #6  
Old February 22nd, 2013, 10:06 AM
gw1500se gw1500se is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Jul 2003
Posts: 2,886 gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 1 Year 2 Weeks 3 Days 8 h 22 m 27 sec
Reputation Power: 581
Use 'strtolower' and 'str_replace'.

Reply With Quote
  #7  
Old February 22nd, 2013, 10:29 AM
Jacques1's Avatar
Jacques1 Jacques1 is online now
pollyanna
Click here for more information.
 
Join Date: Jul 2012
Location: Germany
Posts: 1,881 Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 9 h 31 m 10 sec
Reputation Power: 813
Again: Why can't you simply rename the array keys?

I mean, since you're stuck with this stupid solution, at leat use it.

Reply With Quote
  #8  
Old February 22nd, 2013, 11:44 AM
Triple_Nothing Triple_Nothing is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2009
Posts: 297 Triple_Nothing User rank is Corporal (100 - 500 Reputation Level)Triple_Nothing User rank is Corporal (100 - 500 Reputation Level)Triple_Nothing User rank is Corporal (100 - 500 Reputation Level)Triple_Nothing User rank is Corporal (100 - 500 Reputation Level) 
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...

Reply With Quote
  #9  
Old February 27th, 2013, 07:24 AM
volterony volterony is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2010
Posts: 60 volterony User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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>";
}
?>

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > Rename Array Index

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap