bigdave
bigdave

Reputation: 337

Search array returning multiple values

I'm searching a database that returns multiple entries but I'm not sure how to then query the returned data?

The search can return up to 20 results (although this is unlikely) an example of the returned array is as follows:

RESPONSE: relationships/search Array
(
    [0] => stdClass Object
        (
            [relationship_id] => 1487400
            [legal_form] => Sole Trader
            [display_name] => Jones Jones t/a 
            [relationship_status] => Prospect: Hot
            [telephone_number] => 02075387446
            [telephone_number_2] => 
            [mobile_number] => 
            [email_address] => [email protected]
            [date_of_birth] => 
            [registration_number] => 
            [vat_registration_number] => 
            [postcode] => 
            [creation_date] => 2017-09-14
        )

    [1] => stdClass Object
        (
            [relationship_id] => 1487399
            [legal_form] => Sole Trader
            [display_name] => Smith Smith t/a 
            [relationship_status] => Prospect: Hot
            [telephone_number] => 02087653458
            [telephone_number_2] => 
            [mobile_number] => 
            [email_address] => [email protected]
            [date_of_birth] => 
            [registration_number] => 
            [vat_registration_number] => 
            [postcode] => 
            [creation_date] => 2017-09-14
        )

)

Could anyone offer any advice?

EDIT

I feel I'm being a bit silly as I cant work out what I need to do to access the objects within each key?

Heres my code:

$relationship = postRequest('relationships/search', array('mobile_number' => $phone, 'email_address'=>$email, 'return_multiple_flag'=>Y));

     if(isset($relationship->relationship_id))
         foreach ($relationship as $object) {
             if($object->object_property == $phone && $email && $email != '[email protected]'){
                return $relationship->relationship_id;
             }elseif($object->object_property == $phone){
                return $relationship->relationship_id;
             }elseif($object->object_property == $email && $email != '[email protected]'){
                return $relationship->relationship_id; };
    };

I know the foreach is wrong.

Upvotes: 1

Views: 50

Answers (1)

delboy1978uk
delboy1978uk

Reputation: 12365

You have an array of stdClass objects.

You can access them through the numerical key, starting with 0:

$obj = $array[0]; // First element in the array

Or, you can loop through them:

foreach ($array as $obj) {
    // code here
}

You can access properties of the object using the -> operator:

$id = $obj->relationship_id;

Hope this helps!

Upvotes: 2

Related Questions