Search code examples
phpexplode

How to Take data from Explode and Check if same data


I have case by equating the existing data

i Have 2 data with format like this

$data1 = "144|aku|1!!!122|dia|2";  data description "id|name|amount"

$data2 = "144|aku|1!!!211|dia|1";

there is the same data in 2 arrays, how to make the same data in the amount section can be added and unequal data will be inserted into the array

how to make the 2 arrays become like this

$data_result  = "144|aku|2!!!122|dia|2!!!211|dia|1";

this my full script

$data1 = "144|aku|1!!!122|dia|2";

$data2 = "144|aku|1!!!211|dia|1";


    $simpan_array = array();
    $array_status = array();
               // Pecah Array dari user dulu

               $array_code_user =  explode('!!!', $data1 );
               $jumlah_item_user = count($array_code_user) - 1;
               $x = 0;

               // Pecah Array dari lokal
               $array_cart_local   = explode('!!!',$data2 );
               $jumlah_cart_local  = count($array_cart_local) - 1;
               $j = 0;

                while( $x <= $jumlah_item_user ) {
                    $ambil_datacart_user = explode( '|', $array_code_user[$x] );

                        $idproduk_user   = $ambil_datacart_user[0];
                        $namaprod_user   = $ambil_datacart_user[1];
                        $jumprod_user    = $ambil_datacart_user[2];

                        $simpan_array_0 = $idproduk_user.'|'.$namaprod_user.'|'.$jumprod_user;
                        while( $j <= $jumlah_cart_local ) {
                            $ambil_datacart_lokal = explode( '|', $array_cart_local[$j] );

                                $idprod_lokal     = $ambil_datacart_lokal[0];
                                $namaprod_lokal   = $ambil_datacart_lokal[1];
                                $jumprod_lokal    = $ambil_datacart_lokal[2];

                                $simpan_array_1 = $idprod_lokal.'|'.$namaprod_lokal.'|'.$jumprod_lokal;
                                //Disamakan

                                    if( $idproduk_user == $idprod_lokal ) {
                                        $jumtotal = $jumprod_user + $jumprod_lokal;

                                        $simpan_array[] = $idprod_lokal.'|'.$namaprod_lokal.'|'.$jumtotal;
                                        $array_status[] = 1;
                                    } else {

                                        $simpan_array[] = $simpan_array_1;

                                        $array_status[] = 0;
                                    }

                          $j++;
                        }

                  $x++;      

                }

              $jumlah = array_sum($array_status);
              $array_jadi = join("!!!",$simpan_array);
              //$datakembali = $array_jadi;
              if ( $jumlah == 0 ) {
                        $datakembali = $simpan_array_1.'!!!'.$array_jadi;
               } else {
                        $datakembali = $array_jadi;
               }
               echo $datakembali;
               ?>

Solution

  • I loop the first array and call the output "result".
    Then as I loop the second I see if the data exists in result array and it they do I add it to result else I create the new item.

    $array1 = explode("!!!", $array1);
    $array2 = explode("!!!", $array2);
    
    Foreach($array1 as $val){
        List($id, $name, $amount) = explode("|", $val);
        $result[$id] = ['name' => $name, 'amount' => $amount];
    }
    
    Foreach($array2 as $val){
        List($id, $name, $amount) = explode("|", $val);
    
        If(isset($result[$id])){
            $result[$id]['amount'] += $amount;
        }Else{
            $result[$id] = ['name' => $name, 'amount' => $amount];
        }
    }
    

    https://3v4l.org/gQjTj

    I did not include the conversion back to your format.
    It's a cute format with all the pipes and exclamation marks but I strongly encourage you to use json_encode().
    That makes a string that can easily be decoded again without home made functions.
    See here https://3v4l.org/IEhsp