2010-12-08 76 views
3

Pardonnez encore mon noobness. J'ai un tableau avec plus de 20 valeurs et j'en prends toutes les 20 dans ma base de données, puis je les coupe du recto du tableau. Je veux redémarrer les index du tableau à 0 mais à la place il commence à 20 même lorsque j'utilise array_values. J'ai aussi essayé array_merge (array(), $ string) Que faire?PHP réindexant un tableau?

if($x%20 == 0){ 
    var_dump($string) // original array 
    get_string($string, $body, $binary); //puts the 20 string into my db 

    for($y=0; $y <20; $y++) //done with the 20 so I'm removing them 
     unset($string[$y]); 

    array_values($string); //reindex set $string[20] to $string[0] PLEASE! 
    var_dump($string); // this is suppose to be reindexed 
} 

Au lieu de cela je reçois

array // original array 
    0 => string '----' (length=25) 
    1 => string '----' (length=15) 
    2 => string '----' (length=27) 
    3 => string '----' (length=22) 
    4 => string '----' (length=23) 
    5 => string '----' (length=21) 
    6 => string '----' (length=26) 
    7 => string '----' (length=23) 
    8 => string '----' (length=24) 
    9 => string '----' (length=31) 
    10 => string '----' (length=19) 
    11 => string '----' (length=22) 
    12 => string '----' (length=24) 
    13 => string '----' (length=24) 
    14 => string '----' (length=25) 
    15 => string '----' (length=12) 
    16 => string '----' (length=16) 
    17 => string '----' (length=15) 
    18 => string '----' (length=23) 
    19 => string '----' (length=15) 
    20 => string '----' (length=16) 
    21 => string '----' (length=27) 

array //reindexed array? This was suppose to be [0] and [1] 
    20 => string '----' (length=16) 
    21 => string '----' (length=27) 

Répondre

12

que je fais habituellement:

$array = array_values($array); 

On dirait que vous avez la plupart du chemin - a oublié d'attribuer le nouveau tableau à l'ancienne variable.

+0

Devinez que j'ai programmé trop ce soir. Merci beaucoup – Chenelle

+2

J'adore les incohérences de PHP! – Scuzzy

+0

Très belle suggestion Hamish. J'aime ça. – Bakhtiyor

0

Affectez la valeur de retour du tableau REINDEXer revenir:

if($x%20 == 0){ 
    var_dump($string) // original array 
    get_string($string, $body, $binary); //puts the 20 string into my db 

    for($y=0; $y <20; $y++) //done with the 20 so I'm removing them 
     unset($string[$y]); 

    $string = array_values($string); //reindex set $string[20] to $string[0] PLEASE! 
    var_dump($string); // this is suppose to be reindexed 
} 

ou, comme Brad suggère de remplacer:

for($y=0; $y <20; $y++) //done with the 20 so I'm removing them 
      unset($string[$y]); 

     $string = array_values($string); //reindex set $string[20] to $string[0] PLEASE! 

avec:

for($y=0;$y<20; $y++) 
    array_shift($string); 
0

j'aurais regarder array_shift. Cela pourrait faire ce que vous cherchez, car vous les "dépassez" du tableau.

EDIT

De plus, chaque fois que vous traitez avec des tableaux et des boucles, il est une bonne idée de garder la conscience du fait que le réseau peut trouver à court. C'est-à-dire, je suggère fortement de ne pas coder le for(... <20 ...) fixe mais d'utiliser une variable telle que $end = (count($array) < 20 ? count($array) : 20);