Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.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 访问此阵列数据_Php_Laravel_Jsondecoder - Fatal编程技术网

Php 访问此阵列数据

Php 访问此阵列数据,php,laravel,jsondecoder,Php,Laravel,Jsondecoder,我有一个带有print\r的数组结果: Array ( [0] => stdClass Object ( [id] => [place_id] => ChIJ7w0gwihoXz4R4F5KVQWLoH0 [label] => Address Downtown - Sheikh Mohammed bin Rashid Boulevard - Dubai - United Ar

我有一个带有print\r的数组结果:

Array
(
    [0] => stdClass Object
        (
            [id] => 
            [place_id] => ChIJ7w0gwihoXz4R4F5KVQWLoH0
            [label] => Address Downtown - Sheikh Mohammed bin Rashid Boulevard - Dubai - United Arab Emirates
            [value] => Address Downtown - Sheikh Mohammed bin Rashid Boulevard - Dubai - United Arab Emirates
            [details] => stdClass Object
                (
但我需要得到place_id属性,我怎么能得到这个

我正在尝试使用
data[0]->place\u id
data[“place\u id”],但始终返回我
local。错误:非法字符串偏移量“place\u id”

更新

if($select == "address"){
                    $aux = $google->getGoogleAddress($select);

                    print_r(json_decode($aux[0]->place_id));
                    /*$this->newData["place_id"] = $aux["place_id"];
                    $google->generateURL($this->newData);*/
解决方案

json_decode($aux)[0]->place_id

感谢您的帮助

密钥可能存在问题-它被视为一个字符数组(这会导致密钥访问错误)

为了解决此问题,您需要将结果转换为数组,尝试以下解决方法之一:

  • 如果此stdClass是通过PDO从数据库中检索的,则获取结果将作为数组而不是对象:
  • 如果是从其他位置检索的,如文件-强制将结果转换为数组:

  • 错误在于对解码工作原理的误解。你在做什么:

    json\u解码($aux[0]->place\u id)
    
    失败,因为
    $aux
    是字符串。这意味着您试图在它仍然是字符串时对其执行数组和对象属性访问

    为了接收数组,需要首先对JSON字符串进行完全解码:

    $decoded=json\u decode($aux);
    
    在此之后,
    $decoded
    是一个
    stdClass
    对象的数组,
    $decoded[0]->place\u id
    应该包含您需要的内容。但是,您可以一步完成(如果您不必使用数据的任何其他部分):

    json\u decode($aux)[0]>place\u id
    
    $data[0]->place\u id有什么问题吗
    ?您能分享实际的代码吗?看起来您可能正在操作与您想象的不同的东西。@lagbox这是您的代码错误:local。错误:试图获取非object的属性“place_id”。那么这不是您拥有的实际数据。请显示代码。不要描述你在用什么工作,因为这就是错误所在——你似乎没有用你认为应该用的东西工作。
    $result = $sth->fetchAll(\PDO::FETCH_ASSOC);
    
    $result = (array) $results;