搜索数组?那么得到子值?

时间:2011-10-25 22:03:22

标签: php

我有一个看起来像

的数组
    [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
                )

        )

我想从order_product_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
                        )

任何帮助我怎么能这样做?

3 个答案:

答案 0 :(得分:2)

function search_products($id,$products)
{
    $id = intval($id);
    foreach($products as $product)
    {
        if($product->order_product_id == $id)
        {
            return($product->data);
        }
    }
    return null;
}

对你正在寻找的东西进行有根据的猜测。称之为search_products(91385, $products)。如果返回null,则表示尚未找到产品ID。我还添加了对intval的调用,所以如果您依赖于此用户输入,则无论如何都是int。如果你已经将它消毒为int,这不会有害。

编辑:误读原帖。从数组语法更新为对象属性语法。

答案 1 :(得分:1)

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

答案 2 :(得分:0)

$prod_data = false;
foreach ($products as $product)
{
    if ($product->order_product_id == 91385)
    {
        $prod_data = $product->data;
        break;
    }
}
if ($prod_data) {
    // knock yourself out
}

与普通英语非常相似,是吗?