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'
如果我有身份证,我如何获得国家/地区名称?我试过了:$ 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.
非常感谢。
2 个解决方案
#1
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.
您有要分析的数组的ID,但是您的数组是结构化的,这意味着外部数组中没有键。因此,您必须首先迭代数组才能找到您要查找的对象。
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
.
虽然第一种方法是搜索具有您正在寻找的ID的对象,但我建议您使用其ID映射阵列。为此,您可以使用两个PHP数组函数:array_column和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_column可以提取数组中每个元素的特定字段。由于您有多个国家/地区对象,因此我们希望从中提取ID以便稍后将其用作关键字。
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.
array_combine采用两个大小相同的数组来创建一个新的关联数组。然后,第一个数组的值将用作键,而第二个数组的值将用作值。
$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.
假设密钥1存储在变量$ key = 1;中,您可以使用$ mappedCountries [$ key] ['country']来获取国家/地区的名称和$ mappedCountries [$ key] ['cities']获得城市,然后你可以迭代。
#2
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:
如果$ datas中可能有很多数组,并且你想通过id(或其他一些键)找到一个数组,你可以这样做:
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.
所以如果你想找到id = 1 $ result = search($ datas,'id','1')的地方;然后你可以获得country echo $ result ['country']或者你需要的任何东西。
#1
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.
您有要分析的数组的ID,但是您的数组是结构化的,这意味着外部数组中没有键。因此,您必须首先迭代数组才能找到您要查找的对象。
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
.
虽然第一种方法是搜索具有您正在寻找的ID的对象,但我建议您使用其ID映射阵列。为此,您可以使用两个PHP数组函数:array_column和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_column可以提取数组中每个元素的特定字段。由于您有多个国家/地区对象,因此我们希望从中提取ID以便稍后将其用作关键字。
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.
array_combine采用两个大小相同的数组来创建一个新的关联数组。然后,第一个数组的值将用作键,而第二个数组的值将用作值。
$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.
假设密钥1存储在变量$ key = 1;中,您可以使用$ mappedCountries [$ key] ['country']来获取国家/地区的名称和$ mappedCountries [$ key] ['cities']获得城市,然后你可以迭代。
#2
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:
如果$ datas中可能有很多数组,并且你想通过id(或其他一些键)找到一个数组,你可以这样做:
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.
所以如果你想找到id = 1 $ result = search($ datas,'id','1')的地方;然后你可以获得country echo $ result ['country']或者你需要的任何东西。