Reputation: 379
I am a little confused about the array_search function (or maybe I am trying to use the incorrect thing.) I have a bunch of transaction objects, (transactions about a customer) each is an array of values. After passing in an email address, I get an object that is one transaction where that email address was used. Example is below. I get it from the command print_r($results)
:
stdClass Object
(
[OverallStatus] => OK
[RequestID] => 4564564654-65465464565-4654654
[Results] => Array
(
[0] => stdClass Object
(
[thing1] =>
[thing2] =>
[Status] => Active
[ID] => 5555555555
[email_addy] => [email protected]
)
[1] => stdClass Object
(
[thing1] =>
[thing2] =>
[Status] => Active
[ID] => 6666666666
[email_addy] => [email protected]
)
[2] => stdClass Object
(
[thing1] =>
[thing2] =>
[Status] => Active
[ID] => 6666666666
[email_addy] => [email protected]
)
)
)
I get this output with no trouble at all. My issue is that I need to identify when someone has a specific ID. I was trying to use a foreach but I am not getting back what I need. The code and output is below.
foreach ($results as $key => $value) {
echo "Key: $key; Value: $value<br />\n";
}
Output is
Key: OverallStatus; Value: OK
Key: RequestID; Value: 4564564654-65465464565-4654654
Key: Results; Value: Array
All I really need to know is if the customer has an ID of 5555555555
. This number will always stay the same. Am I going in the wrong direction here?
Upvotes: 1
Views: 105
Reputation: 8334
You have array of object
, so you should get first the array results
, and then iterate over it,try this :
foreach ($results->Results as $key => $value) {
if($value->ID == 55555555) echo 'found at position'.$key;//if id is unique , add a break;
}
Upvotes: 3
Reputation: 2106
It would be something like the following:
$transactions = $results->Results; //get results array
foreach ($transactions as $transaction) { // loop through each transaction object.
echo $transaction->ID . '<br />'; // print out the id of each transaction.
}
Upvotes: 0
Reputation: 8929
You need to iterate through $results->Results.
foreach ($results->Results as $key => $value) {
if($value->ID == 5555555555)
print "Match found";
}
Upvotes: 2
Reputation: 10717
You should loop again for results
array. Try following foreach
foreach ($results as $key => $value) {
if($key == "Results") {
foreach($value as $v) {
if($v->ID == "5555555555") {
echo "I found you";
break;
}
}
} else {
echo "Key: $key; Value: $value<br />\n";
}
}
Upvotes: 1
Reputation: 580
Loop through '$results->Results' and check in loop if the ID=5555555555 like this:
foreach ($results->Results as $value) {
if($value->ID == 5555555555) {
//do something
}
}
Upvotes: 0