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

    Join Date
    Jul 2010
    Posts
    87
    Rep Power
    5

    Creat Hyperlinks with Array Keys


    Hi

    I have an array of arrays, and in this case nameOne, nameTwo etc:


    PHP Code:
    $offer = array(

    "nameOne" => array(    array('id'=>'01','logo'=>'images)),
    "nameTwo" => array(    array('
    id'=>'02','logo'=>'images)),
    "nameThree" => array(array('id'=>'03','logo'=>'images)),
    "nameFour" => array(    array('
    id'=>'04','logo'=>'images)),
    "nameFive" => array(    array('id'=>'05','logo'=>'images)),

    What I would like to be able to do create a list of hyperlinks from the nameOne, nameTwo etc and put them in a content div. So when I say click on the hyperlink for name40, it takes
    you straight down to the name40 array. I understand that this can be done using basic html:

    <a href="#nameOne">nameOne</a>
    <a name="nameOne"></a>

    I can easily loop through the array and print the keys but not sure how to do the hyperlinking:

    PHP Code:
    foreach ($offer as $k => $v)
    {
     echo 
    '<a href="  ">$v</a>'


    Any help much appreciated.

    Volterony
  2. #2
  3. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,183
    Rep Power
    9398
    $v is an array of arrays. You need a [0] to get to the values you want. Which is weird - I bet there's something else that should be fixed instead.

    And remember that variables don't work in single-quoted strings.
    PHP Code:
    echo "<a href='#{$k}'>{$k}</a>"
    (I can't see how the id or logo are supposed to be used)

    Also <a name> is deprecated in favor of putting an ID on... well, anything you want.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2010
    Posts
    87
    Rep Power
    5
    Originally Posted by requinix
    $v is an array of arrays. You need a [0] to get to the values you want. Which is weird - I bet there's something else that should be fixed instead.

    And remember that variables don't work in single-quoted strings.
    PHP Code:
    echo "<a href='#{$k}'>{$k}</a>"
    (I can't see how the id or logo are supposed to be used)

    Also <a name> is deprecated in favor of putting an ID on... well, anything you want.
    Requinix,

    This line
    PHP Code:
    echo "<a href='#{$k}'>{$k}</a>"
    Sucessfully prints all the array keys as hyperlinks - thanks!

    However when I click on the hyperlink {$k}, it does not jump to that particular item in the array?

    Do I need to add a # hash somewhere in each array so that when I click on the link it take you to the array?

    cheers
    Volterony
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Dec 2004
    Posts
    3,008
    Rep Power
    376
    what requinix is saying is that perhaps this should be the structure of the array:

    PHP Code:
     $offer = array(

    "nameOne" => array('id'=>'01','logo'=>'images),
    "nameTwo" => array('
    id'=>'02','logo'=>'images),
    ....

    then you could do

    foreach ( $offer as $key => $array ) {
    // well i was going to post the answer but i will let you work it out
    }
  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 paulh1983
    what requinix is saying is that perhaps this should be the structure of the array:

    PHP Code:
     $offer = array(

    "nameOne" => array('id'=>'01','logo'=>'images),
    "nameTwo" => array('
    id'=>'02','logo'=>'images),
    ....

    then you could do

    foreach ( $offer as $key => $array ) {
    // well i was going to post the answer but i will let you work it out
    }

    Would it still be possible if I was to keep the array structure and get to the target array elements by using a nested foreach loop within the main foreach?

    PHP Code:
    foreach ($deals as $key => $val)
    {
        foreach (
    $val as $k =>$v)
           {
           echo 
    "<a href='#{$v}'>{$v} </a>";
           }


    This solution is by no means accurate!! Just an idea (it prob makes no sense

    Unfortunately changing the array structure will mean having to re-write another script that rely's on the original structure.

    Cheers
    Volterony
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2010
    Posts
    87
    Rep Power
    5
    Is there a way to create the hyperlinks if the array is multidimensional like this:

    PHP Code:
    $offer = array( 

    "nameOne" => array('id'=>'01','logo'=>'images'), 
                         array(
    'id'=>'02','logo'=>'images'),
                          array(
    'id'=>'02','logo'=>'images')),

    "nameTwo" => array('id'=>'01','logo'=>'images'), 
                         array(
    'id'=>'02','logo'=>'images'),
                          array(
    'id'=>'02','logo'=>'images')),

    "nameThree" => array('id'=>'01','logo'=>'images'), 
                           array(
    'id'=>'02','logo'=>'images'),
                           array(
    'id'=>'02','logo'=>'images')),
     
    ); 
    This would make nameOne, nameTwo Three etc hyperlinks to their respective data.

    I have tried but no luck

    PHP Code:
    foreach ($offer as $key => $val

        foreach (
    $val as $k =>$v
           { 
           echo 
    "<a href='#{$v}'>{$v} </a>"
           } 


    Last edited by volterony; September 27th, 2013 at 08:46 AM.

IMN logo majestic logo threadwatch logo seochat tools logo