I have an array e.g
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":""}}
)
)
And I have id like 71, 73 etc. So I want to fetch the record from array whose id is e.g 73.
How can I get it.
I know this is not so tough but I am not able to get it in a single line.I can get the record running foreach loop of array and match my id but if I have thousands of records in an array then it would be wrong way thats why I am asking any direct way to get record.
First you have to take array in some variable like
$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]);
}
}
using array_search function you can get it
first traverse this object like this....
$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
}
}
try in_array for search into array, like this :
<?php
$a = array("aa","bb","cc");
if(in_array("aa",$a))
{
echo "get it;";
//if successful do what ever you want
}
?>
I don't find a solution for this (except using a loop) in just single line. Because the function like "array_search()" or "in_array()" works only for single dimensional arrays and it looks like you have an 2D array.