1=>america,2=>India,3=>england
Above is my associative array. How can I bring 3=>england to front of the array?
1=>america,2=>India,3=>england
Above is my associative array. How can I bring 3=>england to front of the array?
You can do that with array_reverse, docs you can find at http://php.net/manual/en/function.array-reverse.php
You can use array_pop and array_unshift for this:
$last = array_pop($array);
array_unshift($array, $last);
I think he wants to have element 3=>england to the front so he can use it with foreach and the rest of the array has to stay on the same place
than he wants this result
$array[1] = 'america';
$array[2] = 'India';
$array[3] = 'england';
$new_array[3] = $array[3];
$new_array[1] = $array[1];
$new_array[2] = $array[2];
print_r($new_array);
there is probably a function for but i cant find it so i made one
function placeLastToFirst($array){
$newArray = array();
$newArray[count($array)] = $array[count($array)];
for($i = 1;$i < count($array);$i++){
$newArray[$i] = $array[$i ];
}
return $newArray;
}
you have to look out because this function will only work if the array begins with 1 (normal arrays begin with 0). In that case you can use this one
function placeLastToFirst($array){
$newArray = array();
$newArray[count($array)-1] = $array[count($array)-1];
for($i = 0;$i < count($array)-1;$i++){
$newArray[$i] = $array[$i];
}
return $newArray;
}