【发布时间】:2017-03-24 05:25:10
【问题描述】:
我正在尝试匹配两个数组,如果匹配,则循环遍历数组值以将它们显示在页面上。
这就是我的做法。
$productIDs = array(
'0' => array(
'product_id' => '565355',
'product_name' => 'stackPDF',
'product_file' => 'http://www.example.com/stack.pdf',
),
'1' => array(
'product_id' => '563423',
'product_name' => 'lostPDF',
'product_file' => 'http://www.example.com/lost.pdf',
),
'3' => array(
'product_id' => '4442',
'product_name' => 'No product',
'product_file' => '',
)
);
function getProducts($productIDs){
$getIDs = explode(',', $_GET['product_id']);
$intersection = array();
foreach($productIDs as $items)
{
$intersection[] = array_intersect($items, $getIDs);
}
if(!empty($intersection)){
return $intersection;
} else {
echo "There are no products available!";
}
}
$getProducts = getProducts($productIDs);
function getDownloads($getProducts){
foreach($getProducts as $item){
print_r($item);
}
}
$getDownloads = getDownloads($getProducts);
在 getProducts() 函数中,我正在检查标头中的 product_id 是否与 $productIDs 中的任何 product_id 匹配,以便仅显示标头中的可用链接。
$getProducts 变量具有可用的product_id,它已经在数组中匹配,并且在 $getDownloads 中我试图“如果 id 可用,则循环并显示来自多维数组的 product_file 参数值”但是我似乎无法遍历它,而是无法弄清楚如何匹配它/返回值。
【问题讨论】: