2

I've got a simple array working based off the url ie www.example.com/apple/ it places in the apple text where I need it - but I also need an option of pasting in a separate fixed piece of text based off the brand.

IE so I can draw it with $brand_to_use to place in "apple" for example and $brandurl_to_use to draw in "apple.co.uk" where I need it but I'm not sure how to add this to the array based off the original brand.

Thanks

$recognised_brands = array( 
  "apple", 
  "orange", 
  "pear",
  // etc 
); 

$default_brand = $recognised_brands[0]; // defaults brand to apple

$brand_to_use = isset($_GET['brand']) && in_array($_GET['brand'], $recognised_brands) 
  ? $_GET['brand'] 
  : $default_brand;

Pseudo code update example:

recognised brands = 
apple
orange
pear

default brand = recognised brand 0


recognised brandurl =
apple = apple.co.uk
orange = orange.net
pear = pear.com



the brandurl is found from the recognised brands so that in the page content I can reference

brand which will show the text apple at certain places +
brandurl will show the correct brand url related to the brand ie apple.co.uk

2 Answers 2

1

Create your array as a key/value pair and search for a key in the array. The value part of each pair can be an object if you like.

$recognised_brands = array(  
  "apple" => "http://apple.co.uk/",  
  "orange" => "http://orange.co.uk/",  
  "pear" => "http://pear.co.uk/", 
  // etc  
);  

reset($recognised_brands);  // reset the internal pointer of the array
$brand = key($recognised_brands); // fetch the first key from the array

if (isset($_GET['brand'] && array_key_exists(strtolower($_GET['brand']), $recognised_brands))
    $brand = strtolower($_GET['brand']);

$brand_url = $recognised_brands[$brand];
Sign up to request clarification or add additional context in comments.

2 Comments

It's worth noting here that array dereferencing (i.e. array_keys($default_brand)[0]) only works in 5.4 - which is exactly why I split that operation over two lines. The array_keys() approach is arguably better though as it doesn't affect the array pointer, although potentially must less memory efficient if $recognised_brands is very large. I think I shall award you +1 for a good working answer though.
Here you go: with reset and key offering you the fastest and most memory efficient solution :)
0

I think what you are wanting to do is something like this:

$recognised_brands = array( 
  "apple" => 'http://www.apple.co.uk/', 
  "orange" => 'http://www.orange.com/', 
  "pear" => 'http://pear.php.net/',
  // etc 
); 

$default_brand = each($recognised_brands);
$default_brand = $default_brand['key'];

$brand = isset($_GET['brand'], $recognised_brands[$_GET['brand']]) 
  ? $_GET['brand'] 
  : $default_brand;
$brand_url = $recognised_brands[$brand];

If you want to dynamically set the default brand to the first element of the array, it starts to get messy very quickly. But you can do something like this:

9 Comments

in_array() looks for a matching value, not a matching key
@WouterH Darnit, meant to change that to isset() and forgot. Thanks.
Don't forget to reset() before you do each() since you don't know the current position of the internal pointer.
I did set it up like "apple" => 'apple.co.uk', but doesnt that then only allow me to draw with $brand_to_use which will then for apple draw apple.co.uk ? I need to be able to pull both apple in some instances and then the apple.co.uk in others with something like $brand_to_use for apple and $brandurl_to_use for the apple url after brand is set - sorry for my confusion
@user1523591 Err... what? Sorry, can you illustrate this in code? I don't understand what you're asking there...
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.