Borja
Borja

Reputation: 3551

Move an element of an array from start to end

I have this multi dimensional array:

$array48= array(
 array("2017-06-30-104",1498858541000,39.3322,-122.9027,2.11,0,"U",36)
);

OUTPUT:

Array ( 
   [0] => Array ( 
                 [0] => 2017-06-30-104
                 [1] => 1498858541000 
                 [2] => 39.3322 
                 [3] => -122.9027 
                 [4] => 2.11 
                 [5] => 0 
                 [6] => U 
                 [7] => 36                       
                ) 
      ) 

i want to move $array48[0][0] to the end, and i do this:

function id_fine($array_disordinato){
    $number= count($array_disordinato);
    for($a= 0; $a < $number; $a++){
       $element=  $array_disordinato[$a][0];

       unset($array_disordinato[$a][0]);
       array_push($array_disordinato[$a],$element);
    } 
    return $array_disordinato;
}

id_fine($array48);

Works but I don't understand why the OUTPUT is:

Array ( 
   [0] => Array ( 
                 [1] => 1498858541000 
                 [2] => 39.3322 
                 [3] => -122.9027 
                 [4] => 2.11 
                 [5] => 0 
                 [6] => U 
                 [7] => 36 
                 [8] => 2017-06-30-104 
                ) 
      ) 

The position of each element is wrong. It should be:

Array ( 
   [0] => Array (                      
                 [0] => 1498858541000 
                 [1] => 39.3322 
                 [2] => -122.9027 
                 [3] => 2.11 
                 [4] => 0 
                 [5] => U 
                 [6] => 36  
                 [7] => 2017-06-30-104                     
                ) 
      ) 

Upvotes: 1

Views: 2527

Answers (4)

Thamilhan
Thamilhan

Reputation: 13313

I would suggest you to use array_shift for moving your first to last:

array_push($array48[0], array_shift($array48[0]));

DEMO

or use:

$array48[0][]= array_shift($array48[0]);

Thanks Andreas for comment!

Single liner would do what you want to:

Array
(
    [0] => Array
        (
            [0] => 1498858541000
            [1] => 39.3322
            [2] => -122.9027
            [3] => 2.11
            [4] => 0
            [5] => U
            [6] => 36
            [7] => 2017-06-30-104
        )

)

So, no need of complex function id_fine with unset and then array_values


From DOCS:

array_shift() shifts the first value of the array off and returns it, shortening the array by one element and moving everything down. All numerical array keys will be modified to start counting from zero while literal keys won't be touched.

Upvotes: 6

axiac
axiac

Reputation: 72226

unset($array_disordinato[$a][0]); just removes the value, it doesn't reindex the array. It doesn't even know the value you asked it to unset was in an array and it wouldn't care anyway because it's not its job to care about arrays.

array_push($array_disordinato[$a],$element) also doesn't reindex the array. It just finds the greatest numeric key, increments it and stores the value there.

There are several ways to reach your goal. One of them is to use array_values() after you re-order the values. array_values() ignores the existing array keys; it gets only the values and produces new indices starting from 0.

Another option is to use array_slice() instead of unset(). It extracts a slice from the array (you need from index 1 until the end) and, by default, it reindexes the array it returns. You can then use array_push() or just [] to append the value you already saved in $element:

function id_fine(array $array_disordinato)
{
    // Use the array keys to iterate over the array; it is safer
    foreach (array_keys($array_disortinato) as $a) {
       // Copy the first element to the end
       $array_disordinato[$a][] = $array_disordinato[$a][0];
       // Ignore the first element
       $array_disordinato[$a] = array_slice($array_disordinato[$a], 1);
    }

    return $array_disordinato;
}

Upvotes: 1

CAllen
CAllen

Reputation: 836

You are getting that result because you unset the position (unset($array_disordinato[$a][0]);) therefore that position no longer exists; What you want to do is create an entire new array with the new results.

By using what you have, the bloiw code will show you how to fix the issue:

$array48= array(
 array("2017-06-30-104",1498858541000,39.3322,-122.9027,2.11,0,"U",36)
);

function id_fine($array_disordinato) {
    $number = count ($array_disordinato);

    for($a= 0; $a < $number; $a++) {
       $element = $array_disordinato [$a][0];

       unset($array_disordinato[$a][0]);

       // HERE: after you unset the array you want to re-order back so it starts from 0
       // with this line only you should be fine :)
       $array_disordinato[$a] = array_values ($array_disordinato[$a]);

       array_push($array_disordinato[$a],$element);
    } 

    return $array_disordinato;
}

var_dump (id_fine($array48));

Upvotes: 1

Dan
Dan

Reputation: 9468

Within your id_fine function reset the array values of your array using array_values

$array_disordinato[0] = array_values($array_disordinato[0]);
return $array_disordinato

Upvotes: 2

Related Questions