doulan8846 2018-03-10 08:14
浏览 116
已采纳

如何在PHP中循环遍历所有Amazon API数组Book结果

I am trying to consume an Amazon Product API and I choose to search for books alone base on the user search query. However after converting the response to an array, I am having problem looping through all the result without having to specify an index. Below is my Code:

  <div class="album py-5 bg-light">
    <div class="container">

      <div class="row">

        <?php 

          foreach($formattedResponse as $response) { 

            ?>

          <div class="col-md-8">            
          <div class="card mb-4 box-shadow">
            <img class="card-img-top" data-src="" alt="Card image cap">
            <div class="card-body">
              <p class="card-text"></p>
              <div class="d-flex justify-content-between align-items-center">
                <div class="btn-group">
                  <button type="button" class="btn btn-sm btn-outline-secondary">Arthur: <?php echo $response['Item'][0]['ItemAttributes']['Author']; ?></button>
                  <button type="button" class="btn btn-sm btn-outline-secondary">Title: <?php echo $response['Item'][0]['ItemAttributes']['Title']; ?></button>
                </div>
                <small class="text-muted">Price: <?php echo $response['Item'][0]['ItemAttributes']['ListPrice']['FormattedPrice']; ?> </small>
              </div>
            </div>
          </div>
        </div>


        <?php 

        } 
        ?>

      </div>
    </div>
  </div>

Displaying the results works perfectly, the only drawback is, it fetches only the first index of the returned book response out of the thousands present because I used $response['Item'][0] if I take away the [0] from I get an error.. Below is the result

  Arthur: Harry Boone Porter
  Title: The Day of Light
  Price: $8.00

My Question: The array contains [0],[1],[2] to [1000] How can I fetch all the book results in the above format instead of the first index which I am using.

Here is my Array Code Snippet: https://pasteio.com/xlsoTVWJLGBO

  • 写回答

1条回答 默认 最新

  • doutang6819 2018-03-10 08:59
    关注

    Seems like formatted response is the named array and does not have to be iterated with foreach loop, if I'm not mistaken, you just need to:

    1. Pass to the view not the $formattedResponse but $formattedResponse['item'] and not to iterate through it. Lets call it $items.
    $items = $formattedResponse['item'];
    // pass $items to the view.
    
    1. In ['item'] element you have an indexed array with numbers. So iterate through it
    <?php foreach ($items as $item): ?>
        ... html here
       <?= htmlspecialchars($item['ItemAttributes']['ListPrice']['FormattedPrice']) ?>
    <?php endforeach; ?>
    

    Do not forget to escape output for your views. Use Twig, Blade or something like Html::encode($var) from Yii. How to escape output in PHP

    To make debugging of multidimensional large arrays easier install larapack/dd from composer, and add

     dd($largeArray)
    

    in any place of your code. It's like var_dump, but a lot easier to look for human being ;)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式