#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2010
    Posts
    87
    Rep Power
    5

    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
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,501
    Rep Power
    594
    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.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    677
    Rep Power
    7
    Are you able to post the build of your array?
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    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 ...
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2010
    Posts
    87
    Rep Power
    5
    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
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,501
    Rep Power
    594
    Use 'strtolower' and 'str_replace'.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  12. #7
  13. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Again: Why can't you simply rename the array keys?

    I mean, since you're stuck with this stupid solution, at leat use it.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    677
    Rep Power
    7
    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...
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2010
    Posts
    87
    Rep Power
    5
    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>";
    }
    ?>

IMN logo majestic logo threadwatch logo seochat tools logo