Jump to content

Using a var to prepend an array name


rbrown

Recommended Posts

I have five arrays and I want to be able to select the array used based on the request_uri by prepending the array it is calling.

So if I had

/search/cars/index.php

or

/search/trucks/index.php

And I explode it using / then I want to prepend the name of the array I'm calling

So it would be

$exploded_uri['2']_array would call the $cars_array or $trucks_array depending on the url

 

I'm trying not to use a switch statement.

 

I'm trying to write the code so I can just add a new array to the master array page, create the folder and plop the index.php in and be done without having to mod the page or groups of pages.

 

Can it be done? I have tried a bunch of different ways and I can't get it to work.

Thanks...

 

 

 

 

 

Link to comment
Share on other sites

try[code[<?php
$a = '/search/cars/index.php';
//$a = '/search/trucks/index.php';
$trucks_array = array('truck1', 'Truck2');
$cars_array = array('car1', 'car2');
$a = explode('/', $a);
echo ${$a[2].'_array'}[0];
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.