-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などの配列からレコードを取得したいと思います。

どうすれば入手できますか。

これはそれほど難しいことではないことはわかっていますが、1 行で取得することはできません。配列の foreach ループを実行してレコードを取得し、ID と一致させることはできますが、配列に何千ものレコードがある場合、それは間違った方法になります。そのため、レコードを取得する直接的な方法を尋ねています。

4

5 に答える 5

0

これに対する解決策は(ループを使用する場合を除いて)1行だけでは見つかりません。"array_search()" や "in_array()" のような関数は 1 次元配列に対してのみ機能し、2D 配列のように見えるためです。

于 2013-11-13T06:05:51.727 に答える
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

まず、次のような変数で配列を取得する必要があります

$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

次のように、配列を検索するには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 に答える
0

array_search 関数を使用して取得できます

于 2013-11-13T05:28:47.627 に答える