array_merge, array_combine, array_push cant get them to do what i want!

Hi

I’m stuck, i am still learning php and at a sort of basic + level, everything i want to do is always new!,

I have two arrays

array 1
$id =
0=>123,
1=>53,
2=>92,
more random numbers (as many as in $geo.

array 2 or what i think is an array…

$geo = "
47.469554,8.307432,Ländliweg 7 5400 Baden,-,
47.390593,8.045777,Aargauerplatz 5000 Aarau,-,
47.392025,8.050795,Bahnhofplatz 5000 Aarau,-,
47.504878,8.019072,Schulstrasse 22 5070 Frick,-,"

$geo = explode (",-,", $geo);

foreach ($geo as $key => $value) {
list($lat, $lon, $add) = explode(",",$value);
// this works i get a list of lats and longs
foreach($geo as $key => $value) {
echo " “. $lat .” “. $lon .”
";
}

I want a final array, array3 that combines the two arrays $id and elements of $geo ($lat and $lon)

id lat lon (123 47.469554 8.307432)
id lat lon (53 47.390593 8.045777)
id lat lon (92 47.392025 8.019072)

ive searched various keywords merge concatenate, push, combine but of all the pages and pages i have read i havnt found anything that works the way i want it to. it should be easy ! but i just dont get the result i want!

Can anyone help?

Thanks a lot Cath

This method gives you $geo as this array (using the first three values):

[code]Array
(
[123] => Array
(
[lat] => 47.469554
[long] => 8.307432
)

[53] => Array
    (
        [lat] => 47.390593
        [long] => 8.045777
    )

[92] => Array
    (
        [lat] => 47.392025
        [long] => 8.050795
    )

)[/code]

[php]$id = array(
0=>123,
1=>53,
2=>92
);

$geo = "
47.469554,8.307432,Ländliweg 7 5400 Baden,-,
47.390593,8.045777,Aargauerplatz 5000 Aarau,-,
47.392025,8.050795,Bahnhofplatz 5000 Aarau,-,";

$geo = preg_grep("/\s+?/",preg_replace("/\n/","",explode(’,-,’,$geo)));

foreach($geo as $key => $info)
{
unset($geo[$key]);
$info = explode(’,’,$info);
$geo[$id[$key]] = array(
“lat” => $info[0]
,“long” => $info[1]
);
}
pre($geo);
[/php]

Just so you know, this assumes that the ID array and the Geo array are ‘in the right order’ as in the fourth element of the geo array, belongs to the fourth ID.

Thank you smokey
:slight_smile:
$geo[$id[$key]] , was very new to me.

I can display what i need :

echo " id[key]= ";
echo $id[$key] . “
”;
echo " info[0]= ";
echo $info[0]. “
”;
echo " info[1]= ";
echo $info[1]. “
”;

Thanks again, i dont think i would have got there on my own.

Cath

Sponsor our Newsletter | Privacy Policy | Terms of Service