所以...我是PHP dummie,我试图过滤一个数组。从mysql_query结果过滤数组值
我有以下PHP函数从MySQL
检索数据function hook_plan1($vars){
$result = mysql_query("SELECT * FROM tblpricing WHERE relid=12 AND type='product'");
$products = array();
while($data = mysql_fetch_assoc($result)){
array_push($products, $data);
}
return array(
"plan1" => $products);
}
该函数呈现以下的数组:
->plan1 = Array (7)
0 => Array (16)
id => "71"
type => "product"
currency => "1"
...
1 => Array (16)
id => "80"
type => "product"
currency => "3"
...
2 => Array (16)
id => "402"
type => "product"
currency => "14"
...
我想过滤的“货币”该阵列(其配在$ _SESSION),所以我可以得到一个单一的数组,如下所示:
->plan1 = Array (16)
id => "402"
type => "product"
currency => "14"
...
我敢肯定它是容易的,所以我尝试以下滤镜阵列:
function hook_plan1($vars){
$currency_id = $_SESSION['currency'];//this is a number
$result = mysql_query("SELECT * FROM tblpricing WHERE relid=12 AND type='product'");
while ($data = mysql_fetch_assoc($result)) {
$products = $data;
}
$filter = (is_array($products) && $products['currency'] == $currency_id);
$filtered_product = (array_filter($products, $filter));
return array(
"plan1" => $filtered_product);
}
但它不工作:( 任何想法?
你为什么不只是添加到查询了'WHERE'条款? – Barmar
'AND currency ='$ currency_id'' – Barmar
1)你的'$ filter'不是一个函数,它应该是; 2)你试图过滤'$ data',而不是'$ products'。 – raina77ow