1

I can't use array_values because i use primary key for my key array i use 123, 124, 125 etc .. like this

Array ( [123] => Array ( [123] => 100 [124] => 0.71428571428571 [125] => 0.46428571428571 [126] => 0.35714285714286 ) [124] => Array ( [123] => 0.71428571428571 [124] => 100 [125] => 0.53571428571429 [126] => 0.35714285714286  ) [125] => Array ( [123] => 0.46428571428571 [124] => 0.53571428571429 [125] => 100 [126] => 0.17857142857143 )

I use unset to delete [127] and success like this

[123] = Array 
[124] = Array 
[125] = Array 
[126] = Array 
[128] = Array 
[129] = Array 
[130] = Array 

but i can't arrange array key with array_values ..

i want to display like this .. may you know ?

[123] = Array 
[124] = Array 
[125] = Array 
[126] = Array 
[127] = Array 
[128] = Array 
[129] = Array 
Rizier123
  • 58,877
  • 16
  • 101
  • 156
TARA
  • 529
  • 1
  • 6
  • 23
  • 1
    I think you have the answer here: http://stackoverflow.com/questions/5943149/rebase-array-keys-after-unsetting-elements – WinterChild Feb 01 '15 at 12:34
  • @Rambo007 yes .. but array_values arrange my array to [0], [1], [2]... but i want to [123],[124],[125].. – TARA Feb 01 '15 at 14:07

1 Answers1

0

Try this:

$array = array(/* your data here */);

// Remove the value(s) you don't need   
unset($array[127]);

// Re-index the keys, starting with the first key from $array
// Get the first key of $array
$keys  = array_keys($array);
$start = $keys[0];
// Compute the last key after re-index
$end   = $start + count($array) - 1;
// Generate the new keys, combine them with the values
$fixed = array_combine(
    range($start, $end),         // generates array($start, $start+1, ... $end);
    array_values($array)
);
axiac
  • 68,258
  • 9
  • 99
  • 134
  • thank you axiac .. i can't access $start = array_keys($array)[0]; because [0].. why ? – TARA Feb 01 '15 at 13:33
  • Direct access to the values of an array returned by a function started working on PHP 5.4. For older PHP versions it needs to be done in two steps using a variable: `$keys = array_keys($array); $start = $keys[0];`. Modified the answer. – axiac Feb 01 '15 at 14:14