Is there any function for reseting an array index or maybe deletting empty values:
Lets say my array look like this:
item[0]="";
item[1]=1;
Is it possible to make my array look like?:
item[0]=1;
My array is very long, so I need something that does it automatically.
Something more:
What happens to an array when it finally contains just a value, is it still an array?
Maybe it is possible to delete the empty values of array and them, putting them inside a new one, it should cause them to be in order again.
For that I need to be able to move values from one array to another.
Any hints?
It seems to be easy like that, but it doesn't work:
<?
$unidad[0]=cero;
$unidad[1]=uno;
$unidad[3]=tres;
$unidad[4]=cuatro;
$limite = count($unidad);
for($i=0;$i<5;$i++){
if (array_key_exists('$i', $unidad)){$nuevoarray[]=$unidad[$i];}else{unset($unidad[$i]);}
$nuevoarray[]=$unidad[$i];
}
echo "$nuevoarray[1]<br>";
$limite2 = count($nuevoarray);
echo "Unidad tiene: ".$limite."<br>";
echo "Nuevoarray has those values : ".$limite2."<br>";
echo "this is value 0".$nuevoarray[0]."<br>";
echo "this is value 1".$nuevoarray[1]."<br>";
echo "this is value 2".$nuevoarray[2]."<br>";
echo "this is value 3".$nuevoarray[3]."<br>";
echo "this is value 4".$nuevoarray[4]."<br>";
if(isset($unidad[1])){echo "yes, it exist";}else{echo "no, it doesnt";}
?>
In this example $unidad[2] is not set, I'm trying to create a new array in which the index are reset so it should look like:
$nuevoarray[0]=cero;
$nuevoarray[1]=uno;
$nuevoarray[2]=tres;
$nuevoarray[3]=cuatro;
But I get nothing, this is the result of the echoing:
Unidad has those values: 4
Nuevoarray has those values: 5
this is value 0
this is value 1
this is value 2
this is value 3
this is value 4
no, it doesnt
Perhaps you're looking for something like this? array_merge(), "If only one array is given and the array is numerically indexed, the keys get reindexed in a continuous way." That should do what I think you're trying to do, basically shifting all values down to fill the next unused numerical key. If that's not what you want, I have no clue what you do want....
Bookmarks