dtf1111 2015-03-04 19:58
浏览 36
已采纳

如何使用php打印数组值

I have an array which is populating data from an API, I can print the value of the array but I can't print selected fields from the array. Here is the code snippet through which I can print the whole array...

if($apiResponse['response']['status'] === 1) {
        // No errors encountered
        echo 'API call successful';
        echo PHP_EOL;

        echo print_r($apiResponse['response']['data'], true);

        echo PHP_EOL;
    }
    else {
        // An error occurred
        echo 'API call failed (' . $apiResponse['response']['errorMessage'] . ')';
        echo PHP_EOL;
        echo 'Errors: ' . print_r($apiResponse['response']['errors'], true);
        echo PHP_EOL;
    }

Here $apiResponse['response']['data'] is the array which contains the following value...I want to fetch the [name], [offer_url] and [preview_url] values from the array...here is the array which I am able to print...

( [3228] => Array ( [OfferUrl] => Array ( [id] => 3228 [offer_id] => 232 
[name] => larl [offer_url] => http://www.nsssa.com/brands/loreacdl-paris.html/?utm_source=abcd&utm_medium=Affiliates [preview_url] => http://www.nsssa.com/brands/larl-paris.html/?utm_source=abcd&utm_medium=Affiliates 
[status] => active [created] => 2014-10-23 03:15:29 [modified] => 0000-00-00 00:00:00 ) ) 
[3230] => Array ( [OfferUrl] => Array ( [id] => 3230 [offer_id] => 232 
[name] => Schwarzkopf [offer_url] => http://www.nsssa.com/brands/schwarzkopf.html/?utm_source=abcd&utm_medium=Affiliates [preview_url] => http://www.nsssa.com/brands/schwarzkopf.html/?utm_source=abcd&utm_medium=Affiliates 
[status] => active [created] => 2014-10-23 03:16:48 
[modified] => 0000-00-00 00:00:00 ) )

Can anyone please help me with this.

  • 写回答

3条回答 默认 最新

  • dongzhi2014 2015-03-04 20:09
    关注

    Do you just mean this?

    echo 'Name: ' . $apiResponse['response']['data'][3228]['OfferUrl']['name'] . PHP_EOL;
    

    Or

    foreach ($apiResponse['response']['data'] as $data) {
        echo $data['OfferUrl']['name'] . PHP_EOL;
        // etc
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容