Forum Moderators: coopster

Message Too Old, No Replies

Numbering Array Results

array, php, number, rows

         

phpmysql

10:56 pm on Apr 1, 2008 (gmt 0)

10+ Year Member



I have an array $data[1] that is giving me results like:

100
101
102
103
103
104
105
105
105

I want to have a 1 before each of the number....but when there is a duplicate number I want it to count up. Such as:

1100
1101
1102
1103
2103
1104
1105
2105
3105

I was trying to use a $row++ but I don't know how to determine when to count up or not.

If anyone has some incite on this, thanks in advance.

mikhaill

3:20 am on Apr 2, 2008 (gmt 0)

10+ Year Member



What you can do is flip the array so the values become the keys ie

$data[100] = 1;
$data[101] = 1;
$data[102] = 1;

then you can create another array to copy the keys into with the 1 appended. But before you add it to the array you can do a array_key_exists to see if you need to count up or not.

$original_array;
$new_array;

foreach ($original_array as $key=>$value)
{

$newkey = '1' . $key;
if (array_key_exists($newkey, $new_array)
{
$newkey++;
}
$new_array[$newkey] = 1;
}
}