使用 Laravel 键迭代数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28239131/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 10:47:26  来源:igfitidea点击:

Iterate array with key Laravel

phparrayslaravelforeachkey

提问by Funny Frontend

I have a ecommerce-web, In my controller I have a code that controls a list of countries that are displayed in the cart.

我有一个电子商务网站,在我的控制器中,我有一个代码来控制购物车中显示的国家/地区列表。

If the price of the country where we are is greater than or equal to the price of shipping to other countries, these countries are added to the list:

如果我们所在国家/地区的价格大于或等于运往其他国家/地区的价格,则将这些国家/地区添加到列表中:

enter image description here

在此处输入图片说明

This is my controller code:

这是我的控制器代码:

 $params['final_countries_list'] = [];

    $list_countries = Config::get('app.web_config.shipment.countries');
    $base_price = $list_countries[Config::get('app.web_country')]['price'];

    foreach($list_countries as $key=>$value) {

        if ($value['price'] <= $base_price)
            $params['final_countries_list'][$key] = $value;
    }

Well, if I do a dd($params['final_countries_list']), I get this result.

好吧,如果我执行 dd($params['final_countries_list']),我会得到这个结果。

array(13) {
["deu"] array(2) {
["name"] "Deutschland"
["price"] 9
}
["che"] array(2) {
["name"] "Schweiz"
["price"] 9
}
["fra"] array(2) {
["name"] "France"
["price"] 8
}
["gbr"] array(2) {
["name"] "United Kingdom"
["price"] 9
}

ETC,ETC...

等等,等等……

Now I want to get this(deu, fra, gbr, etc) in view cart.blade.php

现在我想在cart.blade.php 中看到这个(deu、fra、gbr 等)

In the cart.blade.php have this code to get what I want:

在cart.blade.php 中有这个代码来获得我想要的:

<select name="country" id="pais">                      
    <option value="" selected>&lt;--- Choose Country ---&gt;</option>

       <?php foreach($final_countries_list as $key => $value){?>
        <option value="<?php echo $key?>"><?php echo $value. ' ('.$key.')';?></option>
        <?php } ?>
   </select>

And I get the following error:

我收到以下错误:

ErrorException (E_UNKNOWN) Array to string conversion (View: C:\xampp\htdocs\my_web_name\app\views\cart.blade.php)

ErrorException (E_UNKNOWN) 数组到字符串的转换(视图:C:\xampp\htdocs\my_web_name\app\views\cart.blade.php)

How I can fix it?

我该如何解决?

回答by lukasgeiter

The simplest way would just be to don't assign the full value but only the name: (If you don't need the price in the view)

最简单的方法就是不分配全部价值,而只分配name:(如果您不需要视图中的价格)

foreach($list_countries as $key=>$value) {

    if ($value['price'] <= $base_price)
        $params['final_countries_list'][$key] = $value['name'];
}


An a bit nicer (in my opinion)

好一点(在我看来)

$list_countries = array_filter($list_countries, function($value){
    return $value['price'] <= $base_price
});

$params['final_countries_list'] = array_map(function($value){
    return $value['name'];
}, $list_countries);

回答by Semyon Zhikharev

You should use $country['name']

P.S.
If You are using blade you can simplify your code by using blade helper shortcodes
(e.g. @foreach).
With this in mind you can rewrite your code simpler

您应该使用$country['name']

PS
如果您使用刀片,您可以通过使用刀片助手短代码
(例如@foreach)来简化您的代码。
考虑到这一点,您可以更简单地重写代码

<select name="country">

  <option value="" selected>Select your county</option>

  @foreach($final_countries_list as $key => $country)
    <option value ="{{ $key }}">{{ $country['name'] }} ({{ $key }})</option>
  @endforeach

</select>

回答by Jeff Lambert

The issue is that you are iterating over an array of arrays, but the sub-array you are treating as if it were a string (why you're getting the error). If I understand you correctly, a simple change can get you what you want:

问题是您正在迭代一个数组数组,但是您正在处理的子数组就好像它是一个字符串(为什么会出现错误)。如果我理解正确,一个简单的改变就能让你得到你想要的:

<option value="<?php echo $key?>"><?php echo $value['name']. ' ('.$key.')';?></option>