Php 搜索数组?然后得到子值?
我有一个数组,看起来像Php 搜索数组?然后得到子值?,php,Php,我有一个数组,看起来像 [products] => Array ( [0] => stdClass Object ( [order_product_id] => 91385 [order_id] => 5065 [nid] => 2140
[products] => Array
(
[0] => stdClass Object
(
[order_product_id] => 91385
[order_id] => 5065
[nid] => 2140
[title] => Gi Treasure
[manufacturer] =>
[model] => giftcard
[qty] => 5
[cost] => 0.00000
[price] => 25.00000
[weight] => 0
[data] => Array
(
[gift_description] => HJello!
[gift_email] => dubccom
[gift_sender] => Hello
[gift_sendDate] => 2011-10-25
[gift_title] => Thesure
[gift_card] => 2130
[gift_price] => 25
[gift_qty] => 5
[gift_name] => Steveek
[module] => uc_product
[cert_code] => 8-x8mfqXyUYXze
)
[order_uid] => 1
)
[1] => stdClass Object
(
[order_product_id] => 91386
[order_id] => 5065
[nid] => 2140
[title] => Gift asure 2
[manufacturer] =>
[model] => giftcard
[qty] => 1
[cost] => 0.00000
[price] => 35.00000
[weight] => 0
[data] => Array
(
[gift_description] => Hello There!
[gift_email] => dubcaom
[gift_sender] => Hello
[gift_sendDate] => 2011-10-25
[gift_title] => The Holida
[gift_card] => 2134
[gift_price] => 35
[gift_qty] => 1
[gift_name] => Steven
[module] => uc_product
[cert_code] => 9-8xsxgDW9yrMq
)
[order_uid] => 1
)
)
我想从产品数组中获取数据数组,其中订单产品id
(如果是91385,我会得到
[data] => Array
(
[gift_description] => Hello
[gift_email] => dubccom
[gift_sender] => Hello
[gift_sendDate] => 2011-10-25
[gift_title] => Thesure
[gift_card] => 2130
[gift_price] => 25
[gift_qty] => 5
[gift_name] => Steveek
[module] => uc_product
[cert_code] => 8-x8mfqXyUYXze
)
有什么可以帮我的吗
$prod_data = false;
foreach ($products as $product)
{
if ($product->order_product_id == 91385)
{
$prod_data = $product->data;
break;
}
}
if ($prod_data) {
// knock yourself out
}
很像普通英语,是吗
function search_products($id,$products)
{
$id = intval($id);
foreach($products as $product)
{
if($product->order_product_id == $id)
{
return($product->data);
}
}
return null;
}
很像普通英语,是吗
function search_products($id,$products)
{
$id = intval($id);
foreach($products as $product)
{
if($product->order_product_id == $id)
{
return($product->data);
}
}
return null;
}
一个有根据的猜测你在寻找什么。像search\u products(91385,$products)
那样调用它。如果它返回null,它就没有找到产品ID。我还添加了对intval
的调用,所以如果你依赖用户输入,不管它是什么,它都将是一个int。如果你已经将它净化为int,这不会有什么坏处
编辑:误读原始帖子。从数组语法更新为对象属性语法
function data_by_order ($arr, $orderId) {
foreach ($arr as $item) { // Loop the array
if ($item->order_product_id == $orderId) { // Test this item for the right order id
return $item->data; // Return the data array
}
}
return FALSE; // Return false if we didn't find it
}
// Example usage:
$data = data_by_order($array,91385);
print_r($data);
一个有根据的猜测你在寻找什么。像search\u products(91385,$products)
那样调用它。如果它返回null,它就没有找到产品ID。我还添加了对intval
的调用,所以如果你依赖用户输入,不管它是什么,它都将是一个int。如果你已经将它净化为int,这不会有什么坏处
编辑:误读了原始帖子。从数组语法更新为对象属性语法。仅供参考,不需要调用
返回null;
作为函数的最后一行,因为这是隐式发生的。@DaveRandom:Hmm,确实如此。无论如何,它不能仅作为参考,不需要调用返回null;
作为最后一行函数的,因为这是隐式发生的。@DaveRandom:Hmm,确实如此。无论如何,这不会有什么坏处
function data_by_order ($arr, $orderId) {
foreach ($arr as $item) { // Loop the array
if ($item->order_product_id == $orderId) { // Test this item for the right order id
return $item->data; // Return the data array
}
}
return FALSE; // Return false if we didn't find it
}
// Example usage:
$data = data_by_order($array,91385);
print_r($data);