0

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.

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

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

<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)

How I can fix it?

3 Answers 3

4

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)

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);
Sign up to request clarification or add additional context in comments.

Comments

3

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

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

Comments

1

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>

3 Comments

YEAHHH, It was staring me in the face and I didn't see it
:) @lukasgeiter also proposed a solution that should work, though it's not quite clear from your question whether or not you need the 'price' key elsewhere in your view. If not, his solution may be better.
I do not need the price in my view, I do everything in the controller

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.