Get data from multiple array in Controller

27
December 08, 2019, at 09:50 AM

I have country list in array with multiple array, like:

public static function listCountries()
    {
        $this->country = array(
            array(1, 'SAD', 'sad.png'),
            array(2, 'Argentina', 'argentina.png'),
            array(3, 'Australija', 'australija.png'),
            array(4, 'Novi Zenland', 'noviz.png'),
            array(5, 'Belgija', 'belg.png'),
            array(6, 'Nizozemska', 'nizozemska.png')
        );
    }

But when i do foreach for array, i'm getting this:

//From DB
    $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;

But i'm getting something like this:

array:2 [▼
  0 => array:3 [▼
    0 => 5
    1 => "Belgija"
    2 => "belg.png"
  ]
  1 => array:3 [▼
    0 => 2
    1 => "Argentina"
    2 => "argentina.png"
  ]
]

1 = SAD, 4 = Novi Zenland, not Belgija and Argentina There is no good country, also no data what i want. How to fix this?

Answer 1

The indexes in arrays are 0-based, which means that:

$index = $item->country[$i];

Has to become

$index = $item->country[$i - 1];

To correlate with the country ids. Otherwise, it is always one off. This is assuming that the ids are always ordered from least to greatest, and all ids are a continuous range.

Answer 2

You can use array_filter to get the results you want, checking that the 0 index of each element in $this->country is in the $item->country array:

$country = array(
            array(1, 'SAD', 'sad.png'),
            array(2, 'Argentina', 'argentina.png'),
            array(3, 'Australija', 'australija.png'),
            array(4, 'Novi Zenland', 'noviz.png'),
            array(5, 'Belgija', 'belg.png'),
            array(6, 'Nizozemska', 'nizozemska.png')
        );
$item->country = '1,4';
$item->country = explode(',', $item->country);
$items = $item->country;
$item->country = array_filter($country, function ($v) use ($items) {
    return in_array($v[0], $items);
});
var_dump($item->country);

Output:

array(2) {
  [0]=>
  array(3) {
    [0]=>
    int(1)
    [1]=>
    string(3) "SAD"
    [2]=>
    string(7) "sad.png"
  }
  [3]=>
  array(3) {
    [0]=>
    int(4)
    [1]=>
    string(12) "Novi Zenland"
    [2]=>
    string(9) "noviz.png"
  }
}

Demo on 3v4l.org

Answer 3

You can use this foreach loop to go through the other array and swap the string if the number matches:

$item->country = "1,4";
$item->country = explode(",", $item->country);
for($i=0; $i < count($item->country); $i++) {
    $index = $item->country[$i];
    foreach($this->country as $c) {
        if($c[0] == $index) {
            $item['country'][$i] = $c[1];   // or $item['country'][$i] = $c; if you want all three items
            break;
        }
    }
}
$item->country = implode(",", $item->country);
echo $item->country;
// Should output: SAD,Novi Zenland
READ ALSO
Parsing JSON in PHP using json_decode

Parsing JSON in PHP using json_decode

I'm trying to get request from web service which insert my data in Json formatMy PHP code which is not succeed is shown below:

48
How to get results of more than one condition at a time

How to get results of more than one condition at a time

I even don't know how to ask my question!

58
PHP: When using javascript in a html file I could assign var mythis=this in the global scope. Is there a similiar way in PHP?

PHP: When using javascript in a html file I could assign var mythis=this in the global scope. Is there a similiar way in PHP?

When using javascript in a html file I could assign var mythis=this in the global scopeIs there a similiar way in PHP?

53
Is there a Phar available for Microsoft Graph?

Is there a Phar available for Microsoft Graph?

I am trying to work with Microsoft Graph APIs using PHP as per https://docsmicrosoft

49