foreach ($adm2_province_array_2 as $value)
{
if($value['feature code'] == 'ADM1')
{
$adm1 = $value['town_province'];
}
if($value['feature code'] == 'ADM2')
{
$adm2 = $value['town_province'];
}
$adm2_Array[$value['country']] [$adm1] [$adm2] [] = $value['town_province'];
}
Why $adm1 and $adm2 value are not working here???
Notice: Undefined variable: adm1
Notice: Undefined variable: adm2
jgetner
October 20, 2016, 10:58pm
2
they do not exists unless the if condition passes. Below should fix the problem. But if those values are not set by the conditions you will probably get an index undefined error
foreach ($adm2_province_array_2 as $value)
{
$adm1,
$adm2;
if($value['feature code'] == 'ADM1')
{
$adm1 = $value['town_province'];
}
if($value['feature code'] == 'ADM2')
{
$adm2 = $value['town_province'];
}
$adm2_Array[$value['country']] [$adm1] [$adm2] [] = $value['town_province'];
}
Thanks buddy!
My array contains stuff like this:
Array
(
[0] => Array
(
[country] => España
[feature code] => PPLA3
[country code] => ES
[admin1code] => 51
[admin2code] => H
[town_province] => Zufre
[id] => 108164
)
However there are 1 like this:
Array
(
[0] => Array
(
[country] => España
[feature code] => ADM1
[country code] => ES
[admin1code] => 51
[town_province] => Andalucia
[id] => 108164
)
and
Array
(
[0] => Array
(
[country] => España
[feature code] => ADM2
[country code] => ES
[admin1code] => 51
[admin2code] => H
[town_province] => huelva
[id] => 108164
)
How to make the foreach work with this?
What is the desired end result ?
You can initialize the variable before if condition. Otherwise you can’t use it. If the procedure not go in the condition that you assign value to the variable, It just like the variable were not defined.
Having trouble understanding what you are trying to accomplish but you can do this by using two foreach loops:
// One loop to set the adm values
$adm1 = null;
$adm2 = null;
foreach ($adm2_province_array_2 as $value) {
if($value['feature code'] == 'ADM1') {
$adm1 = $value['town_province'];
}
if($value['feature code'] == 'ADM2') {
$adm2 = $value['town_province'];
}
}
// Second loop to extract values
$adm2_Array = [];
foreach ($adm2_province_array_2 as $value) {
$adm2_Array[$value['country']] [$adm1] [$adm2] [] = $value['town_province'];
}
This gives you what you asked for but I doubt if it is what you actually need. I suspect your adm1/admn2 logic needs to be rethought.
system
Closed
January 25, 2017, 8:48pm
7
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.