Changing array elements directly in foreach

My simple loop takes a list of coordinates and filenames like:

(59.436961) (24.753575) (Revel, Estonia) (Born)
(-34.847745) (138.507362) (Port Adelaide) (Disembarked)
(-33.177085) (138.010057) (Port Pirie) (Residence before enlistment)

      

It preg_split

them into the $ cords array, which works great. Then I repeat them in a foreach loop. My question is why is this:

foreach ($coords as &$marker){
    $marker = preg_split('/\\) \\(|\\(|\\)/', $marker, -1, PREG_SPLIT_NO_EMPTY);
}
print_r($coords);

      

Result:

Array
(
    [0] => Array
        (
            [0] => 59.436961
            [1] => 24.753575
            [2] => Revel, Estonia
            [3] => Born
        )

    [1] => Array
        (
            [0] => -34.847745
            [1] => 138.507362
            [2] => Port Adelaide
            [3] => Disembarked
        )

    [2] => Array
        (
            [0] => -34.847745
            [1] => 138.507362
            [2] => Port Adelaide
            [3] => Disembarked
        )

)

      

(Note that items [1] and [2] are identical), but this:

foreach ($coords as $marker){
    $marker_array[] = preg_split('/\\) \\(|\\(|\\)/', $marker, -1, PREG_SPLIT_NO_EMPTY);
}
print_r($marker_array);

      

Result:

Array
(
    [0] => Array
        (
            [0] => 59.436961
            [1] => 24.753575
            [2] => Revel, Estonia
            [3] => Born
        )

    [1] => Array
        (
            [0] => -34.847745
            [1] => 138.507362
            [2] => Port Adelaide
            [3] => Disembarked
        )

    [2] => Array
        (
            [0] => -33.177085
            [1] => 138.010057
            [2] => Port Pirie
            [3] => Residence before enlistment
        )

)

      

Is there something I am doing wrong or am not aware when I try to change the elements directly in the loop?

+3


source to share


1 answer


In PHP 5.6 I don't see the described behavior. How do you parse the original list?

When I run:

<?php

$coords = array('(59.436961) (24.753575) (Revel, Estonia) (Born)',
    '(-34.847745) (138.507362) (Port Adelaide) (Disembarked)',
    '(-33.177085) (138.010057) (Port Pirie) (Residence before enlistment)');

foreach ($coords as &$marker){
    $marker = preg_split('/\\) \\(|\\(|\\)/', $marker, -1, PREG_SPLIT_NO_EMPTY);
}
print_r($d);

      

this gives me the following:

Array
(
    [0] => Array
        (
            [0] => 59.436961
            [1] => 24.753575
            [2] => Revel, Estonia
            [3] => Born
        )

    [1] => Array
        (
            [0] => -34.847745
            [1] => 138.507362
            [2] => Port Adelaide
            [3] => Disembarked
        )

    [2] => Array
        (
            [0] => -33.177085
            [1] => 138.010057
            [2] => Port Pirie
            [3] => Residence before enlistment
        )

)

      



But I wouldn't use this crazy construction!

If you parse it from text, just use it preg_match_all

with a template like this:

'~\(()\)\s*\(()\)\s*\(()\)\s*\(()\)~mu'

      

Even if you have data in an array, I highly recommend using preg_match

predictable data matching the query preg_split

for this purpose. But no one became the master on the first day ...

0


source







All Articles