【问题标题】:Get json array response and store in another array获取 json 数组响应并存储在另一个数组中
【发布时间】:2017-02-21 06:06:06
【问题描述】:

我有 cURL 函数,它根据我提供的 ID 调用 api 并返回数组中的数据。到目前为止,一切正常。我想要但想不通的是:

我有 foreach,它在表格中显示所有订单所在的位置

@foreach($orders as $order)

   $order->address
   $order->time_created
   $order->price
    ... etc

@endforeach

是否可以将此 cURL 放在 foreach 中并根据 $order->order_id 添加到每一行以显示来自数组的更多数据?

像这样的

@foreach($orders as $order)

   $order->address
   $order->time_created
   $order->price
    ... etc

   $url = get_curl_content_tx("http://example.com/".$order->order_id);
   $data = json_decode($url,true);

   @foreach ( $data as $moreData )
       @if ( $moreData['data'] == $order->time_created )

         // show something
       @else
         // show other thing
       @endif
   @endforeach
@endforeach

我的问题是我不知道如何循环 url,所以我可以得到所有 orders_id

$url = get_curl_content_tx("http://example.com/1");
$url = get_curl_content_tx("http://example.com/2");
$url = get_curl_content_tx("http://example.com/3");
$url = get_curl_content_tx("http://example.com/4");
$url = get_curl_content_tx("http://example.com/5");
$url = get_curl_content_tx("http://example.com/6");

然后我可以执行我的if/else 块。我使用的框架是 Laravel 4.2。

希望我想要完成的工作足够清楚。如果不是我会尝试清除的东西..

编辑:示例应该是什么:

<table>
    <thead>
        <tr>
            <th class="text-center">#</th>
            <th class="text-center">Date Created</th>
            <th class="text-center">Name</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <th>1</th>
            <th>$url = get_curl_content_tx("http://example.com/1")</th> 
            <th>Product 1</th>
        </tr>
        <tr>
            <th>2</th>
            <th>$url = get_curl_content_tx("http://example.com/2")</th> 
            <th>Product 2</th>
        </tr>       
    </tbody>
</table>

【问题讨论】:

  • 是的,这是可能的。不过我还是不确定你的问题是什么?
  • 我的问题是我循环槽订单,页面上有 10 个订单。然后我得到了 10 次具有相同 order_id 的 curl url .. 即第一个。
  • 据我了解,您希望在 foreach 中执行 CURL 请求,而 CURL 取决于 foreach 的值。但是你说 CURL 是用相同的 order_id 执行的?如果您显示的代码是正确的(即:@foreach($orders as $order) 和每个 $order-&gt;order_id 不同,那么应该可以正常工作。您能否提供更多详细信息?
  • 是的,它正在工作,但例如,如果 foreach 从数据库中获取 5 个订单并将它们显示在页面上的表格中,我应该有 5 倍这个 CURL,每个产品 ID 在末尾 $url = get_curl_content_tx("http://example.com/1...5");。相反,我有 5 次具有相同 ID 的 curl url,即 ID=1 的循环顺序 5 次。似乎我的循环中的某些内容不正确。
  • 我已经用预期的方式更新了我的问题。但是,我没有获得正确的 ID,而是在每个产品上获得了相同的 ID。

标签: php arrays json curl laravel-4


【解决方案1】:

您可以使用 laravel 的 unique 方法在对集合进行迭代之前删除重复的订单 ID。

$collection = collect([
    ['name' => 'iPhone 6', 'brand' => 'Apple', 'type' => 'phone'],
    ['name' => 'iPhone 5', 'brand' => 'Apple', 'type' => 'phone'],
    ['name' => 'Apple Watch', 'brand' => 'Apple', 'type' => 'watch'],
    ['name' => 'Galaxy S6', 'brand' => 'Samsung', 'type' => 'phone'],
    ['name' => 'Galaxy Gear', 'brand' => 'Samsung', 'type' => 'watch'],
]);

$unique = $collection->unique('brand');

$unique->values()->all();

或者您也可以使用 PHP 方法。 How to remove duplicate values from a multi-dimensional array in PHP

【讨论】:

    【解决方案2】:

    我已经设法做到了,并将其作为答案发布。因此,由于我已经有了产品的 ID,所以我直接在刀片中制作了这样的产品。也许还有改进的余地,但至少目前它正在发挥作用。

    这是在条件中没有回声的简短版本

    <td class="text-center">
    <?php
    
    $url=get_curl_content_tx("http://example.com/". $order->order_id);          
    $arr = json_decode($url, true);     
    if (is_array($arr['data']) || is_object($arr['data'])) {
            foreach($arr['data'] as $data ) {                   
                $match = false;
                if ($data['amount'] == $order->amount) {                    
                        $match = $data;
                        break;
                }
            }
            if($match !== false ) {
    
                if( .. ) {
                     // do something
                }
                else {      
                    // do something else
                }                 
            }
            else { }    
    }    
    ?>
    </td>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-22
      • 1970-01-01
      • 2017-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-28
      相关资源
      最近更新 更多