【问题标题】:I want to display those value where price greater than 15.0 in laravel我想在 laravel 中显示价格大于 15.0 的那些值
【发布时间】:2017-07-05 02:04:49
【问题描述】:

这是我的 .json 文件

数组 ( [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 的数据价格。 如何在 Laravel

中使用 foreach 进行显示

`$ProductList=file_get_contents('C:\xampp\htdocs\Laravel\resources\data\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);

`

【问题讨论】:

  • 我以 $ProductList=file_get_contents('C:\xampp\htdocs\Laravel\resources\data\ProductList.json'); 访问文件$data=json_decode($ProductList,true);打印_r($数据);这是我直接以数组格式显示整个文件,但现在我只想显示价格大于 15.0

标签: laravel


【解决方案1】:

在刀片文件中

如果您的数组名称是 $data,那么您可以像这样使用 foreach

@foreach($data as $key => $value)
    @if ($value['price'] > 15.0)
        echo $value['price'];
    @endif
@endforeach

在上面的代码中,我只是打印价格,但您可以在“echo $value['price'];”的位置编写代码

希望对你有帮助

【讨论】:

    【解决方案2】:

    你可以使用array_filter()函数。

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

    在上面的例子中,array_filter 将循环遍历 $input 数组并仅在 price 高于 15 时将值添加到 $result 数组。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-26
      • 2015-10-11
      • 1970-01-01
      • 1970-01-01
      • 2020-06-18
      • 1970-01-01
      • 1970-01-01
      • 2021-10-06
      相关资源
      最近更新 更多