2016-10-12 15 views
0

私は提供するIDに基づいてapiを呼び出し、配列にデータを返すcURL関数を持っています。これまでのところすべてが完璧に機能します。私は何をしたいと把握することはできません、このです:json配列レスポンスを取得して別の配列に格納する

私はすべての注文は、foreachの内部でこのカールを入れて、に基づいて、各列に追加する

@foreach($orders as $order) 

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

@endforeach 

ことが可能ですがうそのテーブルで表示します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 = 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"); 

すなわち、すべてのorders_idを得ることができるので、それでは、私は私のif/elseを行うことができますどのようにループのURLを把握することはできませんということですブロック。私が使用するフレームワークはLaravel 4.2です。

私は達成したいことが十分にはっきりしていることを願っています。ではない場合、私は物事をクリアしようとします。..

EDIT:例それがどうあるべきか:

<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> 
+0

におけるエコーせずにはい、それは可能です短いバージョンです。それでもあなたの問題は何か分かりません。 – Suraj

+0

私の問題は、私がループトラフの注文とページ上で10オーダーです。その後、同じorder_idで10回のカールURLを取得しました。つまり、最初のURLです。 – Garg

+0

foreach内でCURLリクエストを実行したいと思っているから、CURLはforeachの値に依存します。しかし、CURLが同じorder_idで実行されているとしますか?あなたが表示したコードが正しい場合(つまり '@foreach($ orders as $ order)'と各 '$ order-> order_id'が異なる場合、それは正しく動作するはずです...詳細を提供できますか? –

答えて

0

私はそれを行うために管理していると答えとしてそれを投稿します。だから私は既に製品の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> 
1

あなたはそれらを反復する前に、重複した注文IDを削除するには、コレクションのためにlaravelのuniqueメソッドを使用することができます。

$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

関連する問題