PHliP Posted December 12, 2012 Share Posted December 12, 2012 (edited) 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 December 12, 2012 by PHliP Quote Link to comment https://forums.phpfreaks.com/topic/271933-variables-as-keys-in-an-array-possible/ Share on other sites More sharing options...
requinix Posted December 13, 2012 Share Posted December 13, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/271933-variables-as-keys-in-an-array-possible/#findComment-1399049 Share on other sites More sharing options...
PHliP Posted December 13, 2012 Author Share Posted December 13, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/271933-variables-as-keys-in-an-array-possible/#findComment-1399070 Share on other sites More sharing options...
requinix Posted December 13, 2012 Share Posted December 13, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/271933-variables-as-keys-in-an-array-possible/#findComment-1399072 Share on other sites More sharing options...
PHliP Posted December 13, 2012 Author Share Posted December 13, 2012 (edited) 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 December 13, 2012 by PHliP Quote Link to comment https://forums.phpfreaks.com/topic/271933-variables-as-keys-in-an-array-possible/#findComment-1399073 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.