简体   繁体   English

搜索数组返回多个值

[英]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: 搜索最多可以返回20个结果(尽管这不太可能),返回数组的示例如下:

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] => smith@smith.com
            [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] => smith@smith.com
            [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? 我觉得自己有点傻,因为我无法解决访问每个键中的objects所需做的事情?

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 != 'none@none.com'){
                return $relationship->relationship_id;
             }elseif($object->object_property == $phone){
                return $relationship->relationship_id;
             }elseif($object->object_property == $email && $email != 'none@none.com'){
                return $relationship->relationship_id; };
    };

I know the foreach is wrong. 我知道foreach是错误的。

You have an array of stdClass objects. 您有一个stdClass对象数组。

You can access them through the numerical key, starting with 0: 您可以通过数字键(从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! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM