PHP - Merge arrays | Numbers of arrays not known because arrays coming by loop -


apologies if posted already.

struggling array_merge function. works fine when use this.

    array_merge($array1,$array2); 

in case both arrays mysql result. see following code understanding better.

    $getfilelist = 'select *  fileartist fid in (210,209)'; // fetching 2 rows     $file = $db->query($getfilelist);      $file_tot = count($file);     for($i=0;$i<$file_tot;$i++)     {     $artist = explode(',', $file[$i]['artist']); // because artist names saved `a,b` in first row , `a,c,d` in second.      }       print_r($artist); 

this prints this.

    array // artist in first row             (               array[0] =>               array[1] => b              )      array // artist in second row             (               array[0] =>               array[1] => c               array[2] => d              ) 

i want array should -

    array              (               array[0] =>               array[1] => b               array[2] => c               array[3] => d              ) 

i tried following code.

    $getfilelist = 'select *  fileartist fid in (210,209)'; // fetching 2 rows     $file = $db->query($getfilelist);      $file_tot = count($file);     for($i=0;$i<$file_tot;$i++)     {     $artist = explode(',', $file[$i]['artist']); // because artist names saved `a,b` in first row , `a,b,c` in second.      $merged = array_merge($artist);      $unique = array_unique($merged);     }       print_r($unique); 

results

     array              (               array[0] =>               array[1] => b              )      array              (               array[0] =>               array[1] => c               array[2] => d             ) 

this not expected result. in above code, need -> array_merge($file[0], $file[1]); expected result. can't figure out how code it.

i know array_merge function require 2 or more array can't figure out how give both array array_merge function. suggest me how print a,b,c,d in above case. i'm missing something? or bad coded script? also, can post may have more better title future users.

try this

        $getfilelist = 'select *  fileartist fid in (210,209)'; // fetching 2 rows         $file = $db->query($getfilelist);          $file_tot = count($file);          $artist=[];         for($i=0;$i<$file_tot;$i++)         {        $artist = array_merge($artist,explode(',', $file[$i]['artist'])); // because artist names saved `a,b` in first row , `a,c,d` in second.          }           print_r(array_unique($artist)); 

or

    $getfilelist = 'select *  fileartist fid in (210,209)'; // fetching 2 rows     if($result = $mysqli->query($getfilelist)){        $artist=[];        while($file = $result->fetch_assoc()){           array_merge($artist,explode(',', $file['artist']));        }     }         print_r(array_unique($artist)); 

Comments

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -