1

I have this array:

$datas = array(
    array(
        'id' => '1',
        'country' => 'Canada',
        'cities' => array(
            array(
                'city'  => 'Montreal',
                'lang'   => 'french'
            ),
            array(
                'city'  => 'Ottawa',
                'lang'   => 'english'
            )
        )
    )
);

Question 1:

How can I get the the country name when I have the id ?
I tried: $datas['id'][1] => 'country'

Question 2:

How can I loop in the cities when I have the id ?
I tried:

foreach ($datas as $data => $info) {
    foreach ($info['cities'] as $item) {
        echo '<li>'.$item['city'].'</li>';
    }
}

Thanks a lot.

3
  • You'll have to iterate over the arrays, fortunately PHP has a range of good array functions. You might want to have a look at array_filter(). Commented Jan 9, 2018 at 15:24
  • for country echo $datas[0]['country']; Commented Jan 9, 2018 at 15:28
  • You have an array inside an array here, so first of all you need to access $datas[0] to get to the inner array. That one then has a key id. And same for the cities, you want to loop over $datas[0]['cities'] Commented Jan 9, 2018 at 15:46

2 Answers 2

2

You have the ID of the array you want analyse, but your array is structured as a map, meaning that there are no keys in the outer array. You will therefore have to iterate the array first to find the object you are looking for.

While the first approach would be to search for the object that has the ID you are looking for, i suggest you map your arrays with their IDs. To do that, you can use two PHP array functions: array_column and array_combine.

array_column can extract a specific field of each element in an array. Since you have multiple country objects, we want to extract the ID from it to later use it as a key.

array_combine takes two arrays with the same size to create a new associative array. The values of the first array will then be used as keys, while the ones of the second array will be used as values.

$mappedCountries = array_combine(array_column($datas, 'id'), $datas);

Assuming that the key 1 is stored in the variable $key = 1;, you can afterwards use $mappedCountries[$key]['country'] to get the name of the country and $mappedCountries[$key]['cities'] to get the cities, over which you can then iterate.

Sign up to request clarification or add additional context in comments.

3 Comments

It's working for the question 1. Do you have an example for the question 2 please ?
@DamDam $mappedCountries[$key]['cities'] as stated in the answer, over which you then can iterate using foreach, like in your code example.
Perfect. Thanks so much for your help.
1

if there might be many arrays in $datas and you want to find one by id (or some other key) you can do something like this:

function search($datas, $key, $value) {
    foreach($datas as $data) {
    if ($data[$key] === $value) {
        return $data;   
    }
}

So if you want to find where id = 1 $result = search($datas, 'id', '1'); and then you can get country echo $result['country'] or whatever you need.

Comments

Your Answer

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