Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/237.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel和JSON响应_Php_Json_Laravel - Fatal编程技术网

Php Laravel和JSON响应

Php Laravel和JSON响应,php,json,laravel,Php,Json,Laravel,我的JSON响应如下所示: { "success": true, "ownersList": [ { "propertiesList": [], "email": "email@email.com" }, { "propertiesList": [], "email": "email2@email.com" } ] } $obj = json_decode($json); $item = null; ar

我的JSON响应如下所示:

{
    "success": true,
    "ownersList": [
       { "propertiesList": [],
         "email": "email@email.com"  },
       { "propertiesList": [],
         "email": "email2@email.com" }
    ]
}
$obj = json_decode($json);
$item = null;
array_walk($obj->ownersList, function($i) use (&$item) {
    if($i->email == 'email@email.com') $item = $i;
});

// Print out the item
dd($item);

如何获取与特定电子邮件值关联的所有属性列表?

我认为您不需要Laravel提供任何花哨的东西来实现这一点。您可能会想使用Guzzle,它是一个Laravel依赖项,因为它是PHP的一个漂亮而简单的CURL包装器

// assumes the API response is in variable $api_response_body_as_a_string
$o_api_response = json_decode($api_response_body_as_a_string);
$owners_list = $o_api_response->owners_list;
$propertiesList = NULL;
foreach($owners_list as $this_owner) {
    if('email@to.find' === $this_owner->email) {
        $propertiesList = $this_owner->$propertiesList;
        break;
    }
}
// $propertiesList has the result or is NULL
澄清前的原始问题:

在您的问题中,您目前没有任何与Laravel相关的代码,因此我冒昧猜测您正在查询一个表,其中propertiesList是ownersList模型的外键:


否则,需要更多关于设置模型和关系的信息来帮助您。

您可以尝试以下方法:

{
    "success": true,
    "ownersList": [
       { "propertiesList": [],
         "email": "email@email.com"  },
       { "propertiesList": [],
         "email": "email2@email.com" }
    ]
}
$obj = json_decode($json);
$item = null;
array_walk($obj->ownersList, function($i) use (&$item) {
    if($i->email == 'email@email.com') $item = $i;
});

// Print out the item
dd($item);

我使用了硬编码'email@email.com“,您可以使用变量或任何您拥有的东西。

我意识到这个问题已经很老了,并且已经得到了回答,但是使用Laravel的一个很好的方法是:

$json = json_decode($response, true);

$properties = collect($json['ownersList'])
    ->where('email', 'email@email.com')
    ->pluck('propertiesList')->all();
从内部来看,这与其他问题类似,但更流畅,更容易阅读和理解


请参阅获取更多信息。

否,这是一个JSON响应,我从一个API调用中得到,我正在尝试获取行$owners\u list=$o\u API\u response->owners\u list上非对象的属性;让它与您的概念一起工作,只需稍作修改:thnx!你的问题不清楚,请重新措辞。