Reputation: 1318
I have the following array coming from database:
Array (
[0] => stdClass Object (
[cSize] => 120x60
[cFilename] => 29955_120x60.png
[dtLastUpdated] => 2017-06-11T18:18:34-07:00
[cURL] => http://logos.formetocoupon.com/120x60/29955.png
)
[1] => stdClass Object (
[cSize] => 280x210
[cFilename] => 29955_280x210.png
[dtLastUpdated] => 2017-08-15T23:31:05-07:00
[cURL] => http://logos.formetocoupon.com/280x210/29955.png
)
[2] => stdClass Object (
[cSize] => 600x450
[cFilename] => 29955_600x450.png
[dtLastUpdated] => 2017-08-15T23:31:05-07:00
[cURL] => http://logos.formetocoupon.com/600x450/29955.png
)
[3] => stdClass Object (
[cSize] => 88x31
[cFilename] => 29955_88x31.png
[dtLastUpdated] => 2017-06-11T18:18:34-07:00
[cURL] => http://logos.formetocoupon.com/88x31/29955.png
)
)
I want to know the index of the array which contains the image size 120x60
.
I did this
$data=json_decode($value->aLogos);
$searchValue="120x60";
if (($key = array_search($searchValue, $data)) !== false) {
print_r($key);
}
But it is printing nothing. How can I solve this issue? Any kind of suggestion are highly appreciated. Thanks
Upvotes: 1
Views: 76
Reputation: 5550
You need to do roughly somthing like this using the key function
foreach ($data as $value){
if ($value->cSize=="120x60"){
echo key($data);
}
Upvotes: 1
Reputation: 3114
Well your first issue is that you're not searching an array of arrays but an array of objects.
Your second issue is that even if it was an array of arrays, array_search
doesn't do multi-dimensional arrays.
Instead consider something like this in raw PHP:
$data = json_decode($value->aLogos);
$searchValue="120x60";
foreach($data as $key => $obj) {
if ($searchValue == $obj->cSize) {
break;
}
}
echo $key; //holds key of first object with desired value
Alternatively, check out the array_search_deep
method here: http://brandonwamboldt.github.io/utilphp/#array_search_deep
Upvotes: 1