是否有从另一个读取数组的方法?
这是我的2阵列,
$productArr = [
"PT" => [
"cat" => [ "image" => "cat.jpg", "desc" => "blah blah"],
"fish"=> [ "image" => "fish.jpg", "desc" => "blah blah"],
"dog" => [ "image" => "dog.jpg","desc" => "blah blah"],
],
"KC" => [
"Ice" => [ "image" => "ice.jpg", "desc" => "mah mah mah"],
"cold"=> [ "image" => "cold.jpg", "desc" => "mah mah mah"],
"water"=> [ "image" => "water.jpg", "desc" => "mah mah mah"],
],
];
$featuredArr = [
"KC" => "Ice",
"PT" => "cat",
];
例如,从$featuredArr
开始,“Ice”将与$productArr
“Ice”链接,以生成“desc”和“image”。
答案 0 :(得分:0)
使用此:
$code = "KC";
$product = $productArr[$code][$featuredArr[$code]];
echo $product['image'];
echo $product['desc'];
答案 1 :(得分:0)
试试这个:
foreach($featuredArr as $key=>$val)
if($productArr[$key][$val])
print_r($productArr[$key][$val]);
答案 2 :(得分:0)
$productArr = [
"PT" => [
"cat" => [ "image" => "cat.jpg", "desc" => "blah blah"],
"fish"=> [ "image" => "fish.jpg", "desc" => "blah blah"],
"dog" => [ "image" => "dog.jpg","desc" => "blah blah"],
],
"KC" => [
"Ice" => [ "image" => "ice.jpg", "desc" => "mah mah mah"],
"cold"=> [ "image" => "cold.jpg", "desc" => "mah mah mah"],
"water"=> [ "image" => "water.jpg", "desc" => "mah mah mah"],
],
];
$featuredArr = [
"KC" => "Ice",
"PT" => "cat",
];
foreach ($featuredArr as $key => $value) {
if ($productArr[$key][$value]) {
echo "key $key=>$value exists: <br />";
foreach ($productArr[$key][$value] as $newKey => $newValue) {
echo "$newKey :: $newValue <br />";
}
}
}
输出:
key KC=>Ice exists: <br />image :: ice.jpg <br />desc :: mah mah mah <br />key PT=>cat exists: <br />image :: cat.jpg <br />desc :: blah blah <br />
请注意,这在很大程度上取决于您对键和值的使用方法:使用上面的代码,您可以通过在$ featuredArr数组中添加所需的key =&gt;值对来在数组中进行动态循环。
希望这有帮助。