2

我有一个基于 url 的简单数组,即 www.example.com/apple/ 它放置在我需要它的苹果文本中 - 但我还需要一个选项,根据品牌。

IE,所以我可以使用 $brand_to_use 来绘制它以放置在“apple”中,例如 $brandurl_to_use 在我需要的地方绘制在“apple.co.uk”中,但我不确定如何根据原创品牌。

谢谢

$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;

伪代码更新示例:

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
4

2 回答 2

1

将数组创建为键/值对并在数组中搜索键。如果您愿意,每对的值部分可以是一个对象。

$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];
于 2012-07-27T08:51:30.287 回答
0

认为你想要做的是这样的:

$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];

如果你想动态地将默认品牌设置为数组的第一个元素,它很快就会变得混乱。但是你可以这样做:

于 2012-07-27T08:55:49.177 回答