访问多维数组中的数据

时间:2014-10-09 03:43:14

标签: php arrays api

我正在使用API​​,当我运行查询时,我可以打印出使用var_dump返回的数组的结构。如何返回primaryAccountName

这是我的php源代码:

// Configuration
$apiKey   = 'xxxxxxxxxxxxxxxx';
$username = 'xxxxxxxxxxxxxxxx';
// End of configuration

require_once('NutshellApi.php');
$api = new NutshellApi($username, $apiKey);

// My search query
$result = $api->searchLeads(array('string' => "https://app01.nutshell.com/lead/49201", 'limit' => 1));

// var dump data is sampled below
var_dump($result);

这是浏览器中返回的数据:

array(1) { 
    [0]=> object(stdClass) #3 (14) { 
            ["stub"] => bool(true) 
            ["id"] => int(188681) 
            ["rev"] => string(2) "29" 
            ["entityType"] => string(5) "Leads" 
            ["name"] => string(12) "Lead–49201" 
            ["description"] => string(2) "RD" 
            ["status"] => int(0) 
            ["completion"] => int(61) 
            ["value"] => object(stdClass) #4 (2) { 
                ["currency"] => string(3) "USD" 
                ["amount"]=> int(150000) 
            } 
            ["primaryAccountName"] => string(11) "XYZ Industries" 
            ["primaryContactName"] => string(11) "John Doe" 
            ["isOverdue"] => bool(false) 
            ["lastContactedDate"] => string(0) "" 
            ["nextStepDueTime"] => string(24) "2014-10-29T15:52:40+0000" 
        } 
}

当我在评论$result[0]["primaryAccountName"]中尝试了这个建议时,我收到了错误

  

Fatal error: Cannot use object of type stdClass as array in /home/location/location/location/file.php on line 12

1 个答案:

答案 0 :(得分:0)

$ result是对象数组。  所以你可以尝试

 echo $result[0]->primaryAccountName;
          (or)
 $objResult=$result[0];
 echo $objResult->primaryAccountName;