2

I have function in my Contoller

public function dataCountry()
    {
        $this->country = array(
            1 =>    'Serbia',
            2 =>    'USA',
            3 =>    'Croatia',
            4 =>    'Russia',
            5 =>    'China'
        );
    }

Also at start class i have

$this->country = array();

So i have sql query where i am calling some info from database. I have result as

$item->country

Where i get result like this:

[country] => 1,4

So how can i change or effect that to replace and have "," between in result. I will do this before do view, i will change $item->country = "THAT REPLACE FROM COUNTRY". But i don't know how to do that.

Please help, thank you.

4
  • doesn't your result already have a comma? Or if you wanted to replace the comma, replace it with what? Commented Dec 7, 2019 at 14:38
  • Yes, my result already have a comma, like. [country] => 7,9,17. So i just want to replace that number with string. But don't know how to replace. Or there is better way to use country to display. Sometimes there is only one, but sometimes there is more than one. Commented Dec 7, 2019 at 14:41
  • Oh, yes, let me show something, tell me if it works. Commented Dec 7, 2019 at 14:43
  • I'll wait for that. Thank you for trying. Commented Dec 7, 2019 at 15:09

1 Answer 1

1

This should work for you:

public function dataCountry()
{
    $this->country = array(
        1 =>    'Serbia',
        2 =>    'USA',
        3 =>    'Croatia',
        4 =>    'Russia',
        5 =>    'China'
    );

    //after database query you end up with
    $item->country = "1,4";

    $item->country = explode(",", $item->country);

    for($i=0; $i < count($item->country); $i++) {
        $index = $item->country[$i];

        if( !empty($this->country[$index]) ) {
            $item->country[$i] = $this->country[$index];
        }
    }

    $item->country = implode(",", $item->country);

    echo $item->country;
    // Should output: Serbia,Russia
}
Sign up to request clarification or add additional context in comments.

3 Comments

Sorry for bothering you, but can you tell me also how to implement this in multiple array like link where if i have 2 country, i can use and ID and Flag and Country name. I have array(2, 'Argentina', 'argentina.png'), in already array. Is this easy? I'm trying sometning but not working. :D thank you again.
@PooX Sure, instead of if( !empty($this->country[$index]) ) { you would have a foreach() that goes through all the top level indexes of the array and then you could check if the value is in the sub-array using $someItem[0] if we called the variable in the foreach $someItem. It's best to ask this in a new question, it will be much easier to communicate in code.
Thanks, i open new question there: link

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.