Jump to content

Variables As Keys In An Array | Possible?


PHliP

Recommended Posts

Hi all :)

 

After giving this one hours of thought and tedious trial, I saw myself obliged to join and ask here...

 

I made this function to look up the price from a $product.

I have an include on pages where I need the prices in the variables like the 3 first ones in the example.

 

The problem is that my function is only working when I hardcode prices as keys.

I tried all sorts of methods to get access to what's in the vars from within the function, but I'm starting to fear this is not possible and I can't use this way?

 

OTOH, it doesn't seem like keys have to be numbers, so I hope there is something I'm not getting...

 

$kofferprijs_Disco = 6.5;
$kofferprijs_Hawaii = 8;
$kofferprijs_Pirates = 7;

function priceLookup($product){
$pricelist = array(
$GLOBALS['kofferprijs_Disco'] => 'DISCOFEEST',
$GLOBALS['kofferprijs_Hawaii'] => 'HAWAI/TROPICAL',
$GLOBALS['kofferprijs_Pirates'] => 'PIRATES',
);

if (in_array($product, $pricelist)) {
$price = array_search( $product, $pricelist );
return $price;
}
}

$productPrice = priceLookup('PIRATES'); // should give me '7'

 

Tried every variation know to man... ;)

$kofferprijs_Pirates

$$kofferprijs_Pirates

${$kofferprijs_Pirates}

$$GLOBALS['kofferprijs_Pirates']

${$GLOBALS['kofferprijs_Pirates']}

...

 

Thanks for any help! :-)

Edited by PHliP
Link to comment
Share on other sites

The problem is not so much about variable keys, or about using variable variables, or about trying to use floats as array keys, or about $pricelist being backwards, or about magic strings like "PIRATES", but more about all those things together.

$prices = array(
"DISCOFEEST" => 6.5,
"HAWAI/TROPICAL" => 8,
"PIRATES" => 7
);

$productPrice = $prices["PIRATES"];

Why can't you do it that way?

Link to comment
Share on other sites

Because that's the hardcoded way.

 

I want those prices in variables, in an easy accesible file for sitewide changing.

 

But I will try what you suggested with variables again...

 

From an example I looked up, I was supposing the "key" was the left side and often seemed numerical, therefore I started out that way.

 

Maybe not needing a function like this, will help it. Thanks :)

Link to comment
Share on other sites

If you want an easily accessible file suitable for a PHP developer to change then just stick the array in a file and include the file. If you want a file that's editable for non-PHP developers then a file format like INI or XML.

 

Even better would be to switch to a database.

Link to comment
Share on other sites

Not up to database making yet. I'd rather make them a FileMaker (database) solution that will tidily upload that central file if it's too dangerous for them :)

 

It seems to be working great at first glance.

So I was just looking too deep into it and it was much simpler...

 

TYVM for the hints :)

Edited by PHliP
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.