How to merge arrays and use array_unique function to get unique values

i have the following array :

Array ( [0] => 398 [1] => ) Array ( [0] => 397 [1] => ) Array ( [0] => [1] => )

through the code:

  $exp_up_id= explode("  ", $update_id1);
     //for ($x=0;$x<count($exp_up_id);$x++){
     foreach ($exp_up_id as $v) {
    $comma= $v.",";
    
    $exp= explode(",",$comma);
    
     print_r(array_unique($exp));}

what i want is to merge the array in to

Array( [0] => 398 [1] => 397 [2] => 400 [3]=> 397)

so that i can

use array_unique function

to get the unique values avoiding duplicate entries. How to do it in PHP can anybody help me with this.

“How to merge arrays and use array_unique”

Well… have you considered array_merge?

Oh sorry. It’s all 1 array to start with, but 2-dimensional, numerically indexed. I got you now. Threw me off with the title.
Are you only interested in the 0th element of the array, or are you wanting both elements of the original array? your OP is throwing me off because a ‘400’ and an extra ‘397’ appear out of nowhere.

Regardless, you should still be able to merge a 2-dimensional array using the splat operator:
array_merge(... $originalarray)

@m_hutley hi, i’m currently facing a new problem (i managed to use array_unique function successfully but i face this problem)with my php loop. i couldn’t seem to increment in my foreach loop to get the value of an array.
here is my code:


$update_id1=Array ( [0] => 398 [1] => 397 [2] => 393 [3] => 391 [8] => );
echo "update_id1:- "; //print_r($update_id1);
//$x=0;
 foreach ($update_id1 as $i => $v) {

     $ex= explode(",", $v);
     $unique=array_unique($ex);
   //   for ($x=0;$x>count($unique);$x++){
         // echo 'count:-'. $x;
        print_r($unique[$i]);
//}

//$x++;
}

i tried every method nothing seems to work can anyone help me in this regard please.

OP, does this have to do with your other thread where you are trying to manually increment id’s? If so, you are focusing on fixing your attempted solution to the real problem that as far as I have seen, have not answered what that is.

https://www.sitepoint.com/community/t/how-to-get-the-values-and-proceed-to-the-next-incrementing-id-at-the-same-time-in-mysql-using-select-st/345469

Why are you trying to explode something that is already a singular thing?

$update_id1=Array ( [0] => 398 [1] => 397 [2] => 393 [3] => 391 [8] => );
foreach ($update_id1 as $i => $v) {
so, the first time through the loop, $i is 0, and $v is 398.

If you need to make update_id1 unique, do it outside your foreach, and then walk through the unique array, echoing the values.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.