Get single column of data from mysql_query result

405 Views Asked by At

I am currently using a mysql_query with the UNION function. This is the array that I get:

Array
(
    [0] => bob
    [added] => bob
)
Array
(
    [0] => test1
    [added] => test1
)

Is there a way that I can take this array, merge it, remove the added values, place the data in numerical order and make it look like this?:

Array
(
    [0] => bob
    [1] => test1
)

I know somebody'll ask me what have I done so far. Honestly, I have no idea where to go from here.

3

There are 3 best solutions below

6
On
array_reduce(
    array_map(function($i) {
        return $i[0];
    }, $data),
    function($result, $item) {
        $result[] = $item;
        return $result;
    },
    array()
);

or

call_user_func_array('array_merge',
    array_map(function($i) {
        return $i[0];
    }, $data)
);
0
On

When you are fetching the data you can create your array eg:

 while($row = mysqli_fetch_array($result, MYSQLI_NUM){
   $newArray[] = $row[0];
 }

and from your current array you can do

$newArray = array();
foreach($array as $value){
    $newArray = array_push($newArray,$value[0]); 
}
0
On
$array1=array_unique($array1);
$array2=array_unique($array2);
$result = array_merge ($array1,$array2);