从单行中获取数组记录

时间:2013-11-13 05:21:49

标签: php arrays

我有一个数组,例如

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为73的数组中获取记录。

我怎样才能得到它。

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

5 个答案:

答案 0 :(得分: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]);
  }
}

答案 1 :(得分:0)

使用array_search函数你可以得到它

答案 2 :(得分: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
}
}

答案 3 :(得分:0)

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

<?php
$a = array("aa","bb","cc");
if(in_array("aa",$a))
{
  echo "get it;";
  //if successful do what ever you want
}
?>

答案 4 :(得分:0)

我不能在一行中找到解决方案(除了使用循环)。因为函数像&#34; array_search()&#34;或&#34; in_array()&#34;仅适用于单维数组,看起来你有一个2D数组。