2017-02-16 43 views
0

這是我的上傳.json文件更大我想顯示的那些值,其中的價格比15.0 laravel

陣列 ( [0] =>數組 ( [ID] => 1 [名稱] =>冰雕塑 [價格] => 12.5 [標籤] =>數組 ( [0] =>冷 [1] =>冰 )

 [dimensions] => Array 
      (
       [length] => 7 
       [width] => 12 
       [height] => 9.5 
      ) 

     [warehouselocation] => Array 
      (
       [latitude] => -78.75 
       [longitude] => 20.4 
      ) 

    ) 

[1] => Array 
    (
     [id] => 2 
     [name] => A blue mouse 
     [price] => 25.5 
     [tags] => Array 
      (
       [0] => cold 
       [1] => ice 
      ) 

     [dimensions] => Array 
      (
       [length] => 3.1 
       [width] => 1 
       [height] => 1 
      ) 

     [warehouselocation] => Array 
      (
       [latitude] => 54.4 
       [longitude] => -32.7 
      ) 

    ) 

[2] => Array 
    (
     [id] => 2 
     [name] => A blue mouse 
     [price] => 25.5 
     [dimensions] => Array 
      (
       [length] => 3.1 
       [width] => 1 
       [height] => 1 
      ) 

     [warehouselocation] => Array 
      (
       [latitude] => 54.4 
       [longitude] => -32.7 
      ) 

    ) 

[3] => Array 
    (
     [id] => 3 
     [name] => A Chainsmoker 
     [price] => 99.5 
     [tags] => Array 
      (
       [0] => king 
       [1] => wils 
      ) 

     [dimensions] => Array 
      (
       [length] => 7.1 
       [width] => 25 
       [height] => 7 
      ) 

     [warehouselocation] => Array 
      (
       [latitude] => 54.4 
       [longitude] => -32.7 
      ) 

    ) 

這裏我想只顯示數據大於15.0的數據。 如何顯示使用的foreach在Laravel

`$產品列表=的file_get_contents( 'C:\ XAMPP \ htdocs中\ Laravel \資源\數據\ ProductList.json'); $ data = json_decode($ ProductList,true);
// print_r($ data);

foreach($data as $key => $value) { 
     if ($key == 'price' && $value > 15) 
     echo $value; 
    } 
    print_r($value); 

`

+0

我訪問該文件作爲 $產品列表=的file_get_contents('C :\ XAMPP \ htdocs中\ Laravel \資源\數據\ ProductList.json'); \t $ data = json_decode($ ProductList,true); \t \t print_r($ data); 這是我直接以陣列格式顯示整個文件,但現在我只想顯示價格大於15.0 – Carlos

回答

0
刀片文件

如果陣列的名稱爲$的數據,那麼你可以使用的foreach這樣

@foreach($data as $key => $value) 
    @if ($value['price'] > 15.0) 
     echo $value['price']; 
    @endif 
@endforeach 
在上面的代碼中

我剛打印的價格,但你可以將代碼寫在「echo $ value ['price'];」

我希望它對您有所幫助

0

您可以使用array_filter()函數。

$result = array_filter($input, function($value) { 
    return $value['price'] > 15; 
}); 

在上面的例子中,通過輸入$陣列array_filter將循環和添加值$導致陣列僅當價格高於15