0

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.

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
user2898075
  • 79
  • 1
  • 3
  • 10

3 Answers3

2
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)
);
zerkms
  • 249,484
  • 69
  • 436
  • 539
0

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]); 
}
Emilio Gort
  • 3,475
  • 3
  • 29
  • 44
0
$array1=array_unique($array1);
$array2=array_unique($array2);
$result = array_merge ($array1,$array2);