I think this is what you're trying to do:
$result = mysql_query("SELECT DISTINCT * FROM table1 WHERE uid1 = '{$uid1}' ORDER BY /* insert column name here, followed by ASC or DESC */");
$table = array();
while ($row = mysql_fetch_assoc($result))
$table[] = $row;
// at this point, each entry in the $table array is one row from 'table1'.
// shouldn't need to do array_unique or array_reverse with the modified SQL query above.
$numbers = $emails = array();
foreach ($table as $row)
{
$number = number($row["uid2"]);
if (strlen($number) > 9 && !in_array($numbers[$row["uid2"]], $number, true))
$numbers[$row["uid2"]][] = $number;
else
{
$email = email($row["uid2"]);
if (!in_array($emails[$row["uid2"]], $email, true))
$emails[$row["uid2"]][] = $email;
}
}
// shouldn't need to do array_unique with the if-statements above
var_dump($numbers);
var_dump($emails);
EDIT Answering question from comments:
Based on the logic you are using, the result will probably be the same. My example above will allow this scenario for $numbers
, while your example will not:
array(2)
{
[123] => array(2)
{
[0] => 1234567890, // same as $numbers[456][0]
[1] => 9876543210
},
[456] => array(1)
{
[0] => 1234567890 // same as $numbers[123][0]
}
}
But based on the way that $number
is generated based on $uid2
, you probably won't see any difference. What I mean is that, if number(123)
returns 1234567890
, then number(456)
probably will not return 1234567890
, so you probably wouldn't ever run into a scenario where you would see a difference.
EDIT 2 After thinking about this some more, I'm betting that your code can be greatly simplified to this:
// only selecting uid2 from the table
$result = mysql_query("SELECT DISTINCT uid2 FROM table1 WHERE uid1 = '{$uid1}' ORDER BY /* insert column name here, followed by ASC or DESC */");
$output = array();
while (list($uid2) = mysql_fetch_row($result))
{
$number = number($uid2);
if (strlen($number) > 9)
$output[$uid2]["number"] = $number;
else
$output[$uid2]["email"] = email($uid2);
}
var_dump($output);
LAST EDIT (Hopefully):
// only selecting uid2 from the table
$result = mysql_query("SELECT DISTINCT uid2 FROM table1 WHERE uid1 = '{$uid1}' ORDER BY /* insert column name here, followed by ASC or DESC */");
$numbers = $emails = array();
while (list($uid2) = mysql_fetch_row($result))
{
$number = number($uid2);
if (strlen($number) > 9)
$numbers[$uid2] = $number;
else
$emails[$uid2] = email($uid2);
}
var_dump($numbers);
var_dump($emails);