-2

我有一个数组,例如

Array
(
    [0] => stdClass Object
        (
            [id] => 71
            [sku] => cp1038
            [name] => Dual tone stripped pillow
            [slug] => dual-tone-stripped-pillow-222
            [route_id] => 152
            [description] => 

Address Home, a brand new Luxe Lifestyle boutique, Everything from plus bed and table Linen to cushions, curtain and upholstery fabric in silk, cotton, satin, velvet, jacquard and polyester, embellished with foil printing, contemporary embroidery, crystals, quilting and other types of texturing, includin so much I JUST knew there was something really exciting coming up. And how right I was!

            [excerpt] => Dual tone stripped pillow 2 exec
            [price] => 1000.00
            [saleprice] => 0.00
            [free_shipping] => 0
            [shippable] => 1
            [taxable] => 1
            [fixed_quantity] => 0
            [weight] => 10
            [track_stock] => 1
            [quantity] => -93
            [related_products] => 
            [images] => {"0c2ef0ca18f628641cd55fdfbcbfb4ad":{"filename":"0c2ef0ca18f628641cd55fdfbcbfb4ad.jpg","alt":"","caption":"","primary":true},"ac632a9f43216dce4b643b6164bb5891":{"filename":"ac632a9f43216dce4b643b6164bb5891.jpg","alt":"","caption":""},"4623ecf1d9448868fb5ec868ba2292f8":{"filename":"4623ecf1d9448868fb5ec868ba2292f8.jpg","alt":"","caption":""},"1df39f0e7e6d95c7a6f429ed105cd045":{"filename":"1df39f0e7e6d95c7a6f429ed105cd045.jpg","alt":"","caption":""}}

        )

    [1] => stdClass Object
        (
            [id] => 73
            [sku] => cp1040
            [name] => Dual tone stripped pillow1
            [slug] => dual-tone-stripped-pillow-224
            [route_id] => 154
            [description] => 

Dual tone stripped pillow 2 desc

            [excerpt] => Dual tone stripped pillow 2 exec
            [price] => 1000.00
            [saleprice] => 0.00
            [free_shipping] => 0
            [shippable] => 1
            [taxable] => 1
            [fixed_quantity] => 0
            [weight] => 10
            [track_stock] => 1
            [quantity] => -8
            [related_products] => 
            [images] => {"bf2b5529299fa57b586cb393f374b69a":{"filename":"bf2b5529299fa57b586cb393f374b69a.jpg","alt":"","caption":""},"97c99c58f7d5e32b63a4f9f0b35b3167":{"filename":"97c99c58f7d5e32b63a4f9f0b35b3167.jpg","alt":"","caption":""},"e1b38b5aabc82e26c4920e0c36d28ceb":{"filename":"e1b38b5aabc82e26c4920e0c36d28ceb.jpg","alt":"","caption":"","primary":true},"fd19de4ef12d84dd5e0e6e9fca67f1c1":{"filename":"fd19de4ef12d84dd5e0e6e9fca67f1c1.jpg","alt":"","caption":""}}

        )

    [2] => stdClass Object
        (
            [id] => 37
            [sku] => cp202
            [name] => Dual tone stripped pillow
            [slug] => dual-tone-stripped-pillow21
            [route_id] => 96
            [description] => Address Home, a brand new Luxe Lifestyle boutique, Everything from plus 
bed and table Linen to cushions, curtain and upholstery fabric in silk, 
cotton, satin, velvet, jacquard  and polyester, embellished with foil 
printing, contemporary  embroidery, crystals, quilting and other types 
of texturing, including tiny metallic pieces.


            [excerpt] => silk two tone pillow with diagonal stripes
            [price] => 1500.00
            [saleprice] => 0.00
            [free_shipping] => 0
            [shippable] => 1
            [taxable] => 1
            [fixed_quantity] => 0
            [weight] => 10
            [track_stock] => 1
            [quantity] => -43
            [related_products] => 
            [images] => {"0687014a0caff0874107272a5296e465":{"filename":"0687014a0caff0874107272a5296e465.jpg","alt":"","caption":""},"c0a6037628b6f19d6cb8c244cd79fb10":{"filename":"c0a6037628b6f19d6cb8c244cd79fb10.jpg","alt":"","caption":"","primary":true},"6e7dc08cedd9b54ca44bf7fd832f20c7":{"filename":"6e7dc08cedd9b54ca44bf7fd832f20c7.jpg","alt":"","caption":""},"4a74346e35a4b986e03ab69044cad44b":{"filename":"4a74346e35a4b986e03ab69044cad44b.jpg","alt":"","caption":""}}

        )
)

我有 71、73 等 id。所以我想从 id 为 73 的数组中获取记录。

我怎么才能得到它。

我知道这不是那么难,但我无法在一行中得到它。我可以让记录运行数组的 foreach 循环并匹配我的 id,但如果我在数组中有数千条记录,那么这将是错误的方式这就是为什么我要求任何直接的方式来获得记录。

4

5 回答 5

0

首先,您必须将数组放入一些变量中,例如

$detail_array = array(); // your array
for($i=0;$i<count($detail_array);$i++)
{
  echo $detail_array[$i]->id; // you can get id
  if($detail_array[$i]->id == 73)
  {
     print_r($detail_array[$i]);
  }
}
于 2013-11-13T05:25:59.690 回答
0

使用 array_search 函数你可以得到它

于 2013-11-13T05:28:47.627 回答
0

首先像这样遍历这个对象....

$result = array();
foreach ($your_array as $key => $value) {
    $result[] = $value->id;
}
print_r($result);
for($i=0;$i<sizeof($result);$i++)
{
if($result[$i]==73)
{
//do something
}
}
于 2013-11-13T05:37:27.390 回答
0

我没有在单行中找到解决方案(使用循环除外)。因为像“array_search()”或“in_array()”这样的函数只适用于一维数组,看起来你有一个二维数组。

于 2013-11-13T06:05:51.727 回答
0

尝试in_array搜索数组,如下所示:

<?php
$a = array("aa","bb","cc");
if(in_array("aa",$a))
{
  echo "get it;";
  //if successful do what ever you want
}
?>
于 2013-11-13T05:51:39.717 回答