Skip to main content
Known Participant
October 12, 2009
Answered

creating assoiated array in php

  • October 12, 2009
  • 1 reply
  • 1108 views

I have the following code that creates the category and associate link for each category. I like to know how can I have the category name different than the keywords for each category. For example I like to search for ‘superman’ but like to call the link ‘superman movie’

Any help will be greatly appreciated

thanks

<? $categories = array('Superman', 'Batman', 'Iron Man', 'Incredible Hulk'); ?>

<h2>Categories:</h2>
<? foreach (
$categories as $category) { ?>
  <a href="/category.php?psps_search=<?= urlencode($category) ?>"><?= $category ?></a><br/>

This topic has been closed for replies.
Correct answer NateBaldwin

Assuming it's not as simple as adding the string " Movie" after each search term, you could make a nested associative array that stores both bits of information under the appropriate key (without getting a database or XML file involved). Something like:

<?php

$categories = array(

array('search'=>'Superman', 'link'=>'Superman Movie'),

array('search'=>'Batman', 'link'=>'Batman Movie'),

array('search'=>'Iron Man', 'link'=>'Iron Man Movie'),

array('search'=>'Incredible Hulk', 'link'=>'Incredible Hulk Movie')

);

?>

<?php foreach ($categories as $category) { ?>

<a href="/category.php?psps_search=<?= urlencode($category['search']) ?>">

<?= $category['link'] ?>

</a><br/>

<?php } ?>

1 reply

NateBaldwinCorrect answer
Inspiring
October 13, 2009

Assuming it's not as simple as adding the string " Movie" after each search term, you could make a nested associative array that stores both bits of information under the appropriate key (without getting a database or XML file involved). Something like:

<?php

$categories = array(

array('search'=>'Superman', 'link'=>'Superman Movie'),

array('search'=>'Batman', 'link'=>'Batman Movie'),

array('search'=>'Iron Man', 'link'=>'Iron Man Movie'),

array('search'=>'Incredible Hulk', 'link'=>'Incredible Hulk Movie')

);

?>

<?php foreach ($categories as $category) { ?>

<a href="/category.php?psps_search=<?= urlencode($category['search']) ?>">

<?= $category['link'] ?>

</a><br/>

<?php } ?>

Known Participant
October 18, 2009

Dear Mr Baldwin

Thank you for this very precise info. I have created two groups follwing your suggestion: one for category and one for brandI just wanted to know if i like to have a heading for the appropriate link( ie if it is a brand the heading would be the given brand or if it is category than the heading will be given category). i tried the following . and i am sure there is something i am missing :

<?php if ($psps_search=urlencode($category['search'])) echo $category['link']; elseif ($psps_search=urlencode($brand['search'])) echo $brand['link'];?>

psps_search is the search parameter

thanks

October 19, 2009

<?php

$categories = array(

     array('search'=>'Superman', 'link'=>'Superman Movie'),

     array('search'=>'Batman', 'link'=>'Batman Movie'),

     array('search'=>'Iron Man', 'link'=>'Iron Man Movie'),

     array('search'=>'Incredible Hulk', 'link'=>'Incredible Hulk Movie')

);

foreach($categories as $category){

     $psps_search = urlencode($category['search']);

     echo $category['link'];

}

Didn't you forget the foreach statement?