Merge multiple arrays into one with unique values php












3















I know that there are a lot of similar threads on Stack Overflow, but none of them works in my case.



My goal is to get the unique show genres from the database. They are stored (1,2,3 or more) comma separated in the show_genres column.



$link = mysqli_connect('127.0.0.1', 'root', 'pw', 'db');
$query = "SELECT show_genres FROM tv_shows";
$result = mysqli_query($link, $query);
foreach ($result as $value) {
$sh_genres = $value['show_genres'];
$sh_genres_array = array_map('trim', explode(',',$sh_genres));

// $doesnt_work = call_user_func_array("array_merge",
$sh_genres_array); // doesn't work for me


echo '<pre>' . var_export($sh_genres_array, true) . '</pre>';
}


My result is as follows:



array (
0 => 'Drama',
1 => 'Action',
2 => 'Crime',
)
array (
0 => 'Drama',
1 => 'Crime',
)
array (
0 => 'Drama',
1 => 'Thriller',
)
array (
0 => 'DIY',
)
array (
0 => 'Drama',
1 => 'Mystery',
2 => 'Supernatural',
)


However, I need just one array which contains the unique values, such as:



array (
0 => 'Drama',
1 => 'Mystery',
2 => 'Supernatural',
4 => 'Thriller',
5 => 'DIY',
6 => 'Crime',
7 => 'etc...'

)


If I try to create some array before the foreach loop and then store the data into it, such approach doesn't give a result as well.



Perhaps, there is a more simple solution by the means of SQL!?!?










share|improve this question


















  • 1





    You should normalize your database. Then the current problem would be a simple database query.

    – jeroen
    Nov 23 '18 at 13:04











  • Can you please suggest me how to normalize it? Just a simple example. Thanks. Of course, in such a case, it should be a simplest query :-)

    – Alex Cardo
    Nov 23 '18 at 13:05













  • Add a table with genres and another table linking tv_shows to genres.

    – jeroen
    Nov 23 '18 at 13:09
















3















I know that there are a lot of similar threads on Stack Overflow, but none of them works in my case.



My goal is to get the unique show genres from the database. They are stored (1,2,3 or more) comma separated in the show_genres column.



$link = mysqli_connect('127.0.0.1', 'root', 'pw', 'db');
$query = "SELECT show_genres FROM tv_shows";
$result = mysqli_query($link, $query);
foreach ($result as $value) {
$sh_genres = $value['show_genres'];
$sh_genres_array = array_map('trim', explode(',',$sh_genres));

// $doesnt_work = call_user_func_array("array_merge",
$sh_genres_array); // doesn't work for me


echo '<pre>' . var_export($sh_genres_array, true) . '</pre>';
}


My result is as follows:



array (
0 => 'Drama',
1 => 'Action',
2 => 'Crime',
)
array (
0 => 'Drama',
1 => 'Crime',
)
array (
0 => 'Drama',
1 => 'Thriller',
)
array (
0 => 'DIY',
)
array (
0 => 'Drama',
1 => 'Mystery',
2 => 'Supernatural',
)


However, I need just one array which contains the unique values, such as:



array (
0 => 'Drama',
1 => 'Mystery',
2 => 'Supernatural',
4 => 'Thriller',
5 => 'DIY',
6 => 'Crime',
7 => 'etc...'

)


If I try to create some array before the foreach loop and then store the data into it, such approach doesn't give a result as well.



Perhaps, there is a more simple solution by the means of SQL!?!?










share|improve this question


















  • 1





    You should normalize your database. Then the current problem would be a simple database query.

    – jeroen
    Nov 23 '18 at 13:04











  • Can you please suggest me how to normalize it? Just a simple example. Thanks. Of course, in such a case, it should be a simplest query :-)

    – Alex Cardo
    Nov 23 '18 at 13:05













  • Add a table with genres and another table linking tv_shows to genres.

    – jeroen
    Nov 23 '18 at 13:09














3












3








3








I know that there are a lot of similar threads on Stack Overflow, but none of them works in my case.



My goal is to get the unique show genres from the database. They are stored (1,2,3 or more) comma separated in the show_genres column.



$link = mysqli_connect('127.0.0.1', 'root', 'pw', 'db');
$query = "SELECT show_genres FROM tv_shows";
$result = mysqli_query($link, $query);
foreach ($result as $value) {
$sh_genres = $value['show_genres'];
$sh_genres_array = array_map('trim', explode(',',$sh_genres));

// $doesnt_work = call_user_func_array("array_merge",
$sh_genres_array); // doesn't work for me


echo '<pre>' . var_export($sh_genres_array, true) . '</pre>';
}


My result is as follows:



array (
0 => 'Drama',
1 => 'Action',
2 => 'Crime',
)
array (
0 => 'Drama',
1 => 'Crime',
)
array (
0 => 'Drama',
1 => 'Thriller',
)
array (
0 => 'DIY',
)
array (
0 => 'Drama',
1 => 'Mystery',
2 => 'Supernatural',
)


However, I need just one array which contains the unique values, such as:



array (
0 => 'Drama',
1 => 'Mystery',
2 => 'Supernatural',
4 => 'Thriller',
5 => 'DIY',
6 => 'Crime',
7 => 'etc...'

)


If I try to create some array before the foreach loop and then store the data into it, such approach doesn't give a result as well.



Perhaps, there is a more simple solution by the means of SQL!?!?










share|improve this question














I know that there are a lot of similar threads on Stack Overflow, but none of them works in my case.



My goal is to get the unique show genres from the database. They are stored (1,2,3 or more) comma separated in the show_genres column.



$link = mysqli_connect('127.0.0.1', 'root', 'pw', 'db');
$query = "SELECT show_genres FROM tv_shows";
$result = mysqli_query($link, $query);
foreach ($result as $value) {
$sh_genres = $value['show_genres'];
$sh_genres_array = array_map('trim', explode(',',$sh_genres));

// $doesnt_work = call_user_func_array("array_merge",
$sh_genres_array); // doesn't work for me


echo '<pre>' . var_export($sh_genres_array, true) . '</pre>';
}


My result is as follows:



array (
0 => 'Drama',
1 => 'Action',
2 => 'Crime',
)
array (
0 => 'Drama',
1 => 'Crime',
)
array (
0 => 'Drama',
1 => 'Thriller',
)
array (
0 => 'DIY',
)
array (
0 => 'Drama',
1 => 'Mystery',
2 => 'Supernatural',
)


However, I need just one array which contains the unique values, such as:



array (
0 => 'Drama',
1 => 'Mystery',
2 => 'Supernatural',
4 => 'Thriller',
5 => 'DIY',
6 => 'Crime',
7 => 'etc...'

)


If I try to create some array before the foreach loop and then store the data into it, such approach doesn't give a result as well.



Perhaps, there is a more simple solution by the means of SQL!?!?







php mysql






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 '18 at 12:58









Alex CardoAlex Cardo

719




719








  • 1





    You should normalize your database. Then the current problem would be a simple database query.

    – jeroen
    Nov 23 '18 at 13:04











  • Can you please suggest me how to normalize it? Just a simple example. Thanks. Of course, in such a case, it should be a simplest query :-)

    – Alex Cardo
    Nov 23 '18 at 13:05













  • Add a table with genres and another table linking tv_shows to genres.

    – jeroen
    Nov 23 '18 at 13:09














  • 1





    You should normalize your database. Then the current problem would be a simple database query.

    – jeroen
    Nov 23 '18 at 13:04











  • Can you please suggest me how to normalize it? Just a simple example. Thanks. Of course, in such a case, it should be a simplest query :-)

    – Alex Cardo
    Nov 23 '18 at 13:05













  • Add a table with genres and another table linking tv_shows to genres.

    – jeroen
    Nov 23 '18 at 13:09








1




1





You should normalize your database. Then the current problem would be a simple database query.

– jeroen
Nov 23 '18 at 13:04





You should normalize your database. Then the current problem would be a simple database query.

– jeroen
Nov 23 '18 at 13:04













Can you please suggest me how to normalize it? Just a simple example. Thanks. Of course, in such a case, it should be a simplest query :-)

– Alex Cardo
Nov 23 '18 at 13:05







Can you please suggest me how to normalize it? Just a simple example. Thanks. Of course, in such a case, it should be a simplest query :-)

– Alex Cardo
Nov 23 '18 at 13:05















Add a table with genres and another table linking tv_shows to genres.

– jeroen
Nov 23 '18 at 13:09





Add a table with genres and another table linking tv_shows to genres.

– jeroen
Nov 23 '18 at 13:09












3 Answers
3






active

oldest

votes


















4














Please try this code



In SQL



$query = "SELECT show_genres FROM tv_shows GROUP BY show_genres";



In PHP



$newArray  = array();

foreach ($result as $value) {

$sh_genres = $value['show_genres'];
$sh_genres_array = array_map('trim', explode(',',$sh_genres));

$newArray = array_merge($newArray , $sh_genres_array );

}

$newUniqueArray = array_unique($newArray);





share|improve this answer


























  • I might try to do like this, but my arrays are unnamed

    – Alex Cardo
    Nov 23 '18 at 13:03











  • I understand the idea, but in such a case I would have to create some additional foreach loop to mark my arrays some way. But I have no idea how to accomplish it.

    – Alex Cardo
    Nov 23 '18 at 13:08











  • Please check my edit

    – Sree
    Nov 23 '18 at 13:10











  • This is exactly how I tried to cope with my goal, but I stumbled upon two issues: 1) When I'm trying to var_dump $newArray, I can't get a result

    – Alex Cardo
    Nov 23 '18 at 13:14













  • 2) $newUniqueArray gives me just about 26 genres, which is impossible )))

    – Alex Cardo
    Nov 23 '18 at 13:16



















2














try using this code



    $link = mysqli_connect('127.0.0.1', 'root', 'pw', 'db');
$query = "SELECT show_genres FROM tv_shows";
$result = mysqli_query($link, $query);
$sh_genres_array = ;
foreach ($result as $value) {
$sh_genres = $value['show_genres'];
$sh_genres_array = array_merge(
$sh_genres_array ,
array_map('trim', explode(',',$sh_genres))
);
}
echo '<pre>' . var_export(array_unique($sh_genres_array), true) . '</pre>';


Update



Please consider normalizing your database






share|improve this answer

































    0














    Not sure but try this.



     $genre_arr = RecursiveIteratorIterator(new RecursiveArrayIterator($sh_genres_array));

    $unique_genre = array_unique($genre_arr);





    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53447178%2fmerge-multiple-arrays-into-one-with-unique-values-php%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      4














      Please try this code



      In SQL



      $query = "SELECT show_genres FROM tv_shows GROUP BY show_genres";



      In PHP



      $newArray  = array();

      foreach ($result as $value) {

      $sh_genres = $value['show_genres'];
      $sh_genres_array = array_map('trim', explode(',',$sh_genres));

      $newArray = array_merge($newArray , $sh_genres_array );

      }

      $newUniqueArray = array_unique($newArray);





      share|improve this answer


























      • I might try to do like this, but my arrays are unnamed

        – Alex Cardo
        Nov 23 '18 at 13:03











      • I understand the idea, but in such a case I would have to create some additional foreach loop to mark my arrays some way. But I have no idea how to accomplish it.

        – Alex Cardo
        Nov 23 '18 at 13:08











      • Please check my edit

        – Sree
        Nov 23 '18 at 13:10











      • This is exactly how I tried to cope with my goal, but I stumbled upon two issues: 1) When I'm trying to var_dump $newArray, I can't get a result

        – Alex Cardo
        Nov 23 '18 at 13:14













      • 2) $newUniqueArray gives me just about 26 genres, which is impossible )))

        – Alex Cardo
        Nov 23 '18 at 13:16
















      4














      Please try this code



      In SQL



      $query = "SELECT show_genres FROM tv_shows GROUP BY show_genres";



      In PHP



      $newArray  = array();

      foreach ($result as $value) {

      $sh_genres = $value['show_genres'];
      $sh_genres_array = array_map('trim', explode(',',$sh_genres));

      $newArray = array_merge($newArray , $sh_genres_array );

      }

      $newUniqueArray = array_unique($newArray);





      share|improve this answer


























      • I might try to do like this, but my arrays are unnamed

        – Alex Cardo
        Nov 23 '18 at 13:03











      • I understand the idea, but in such a case I would have to create some additional foreach loop to mark my arrays some way. But I have no idea how to accomplish it.

        – Alex Cardo
        Nov 23 '18 at 13:08











      • Please check my edit

        – Sree
        Nov 23 '18 at 13:10











      • This is exactly how I tried to cope with my goal, but I stumbled upon two issues: 1) When I'm trying to var_dump $newArray, I can't get a result

        – Alex Cardo
        Nov 23 '18 at 13:14













      • 2) $newUniqueArray gives me just about 26 genres, which is impossible )))

        – Alex Cardo
        Nov 23 '18 at 13:16














      4












      4








      4







      Please try this code



      In SQL



      $query = "SELECT show_genres FROM tv_shows GROUP BY show_genres";



      In PHP



      $newArray  = array();

      foreach ($result as $value) {

      $sh_genres = $value['show_genres'];
      $sh_genres_array = array_map('trim', explode(',',$sh_genres));

      $newArray = array_merge($newArray , $sh_genres_array );

      }

      $newUniqueArray = array_unique($newArray);





      share|improve this answer















      Please try this code



      In SQL



      $query = "SELECT show_genres FROM tv_shows GROUP BY show_genres";



      In PHP



      $newArray  = array();

      foreach ($result as $value) {

      $sh_genres = $value['show_genres'];
      $sh_genres_array = array_map('trim', explode(',',$sh_genres));

      $newArray = array_merge($newArray , $sh_genres_array );

      }

      $newUniqueArray = array_unique($newArray);






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Nov 23 '18 at 13:19

























      answered Nov 23 '18 at 13:02









      SreeSree

      796520




      796520













      • I might try to do like this, but my arrays are unnamed

        – Alex Cardo
        Nov 23 '18 at 13:03











      • I understand the idea, but in such a case I would have to create some additional foreach loop to mark my arrays some way. But I have no idea how to accomplish it.

        – Alex Cardo
        Nov 23 '18 at 13:08











      • Please check my edit

        – Sree
        Nov 23 '18 at 13:10











      • This is exactly how I tried to cope with my goal, but I stumbled upon two issues: 1) When I'm trying to var_dump $newArray, I can't get a result

        – Alex Cardo
        Nov 23 '18 at 13:14













      • 2) $newUniqueArray gives me just about 26 genres, which is impossible )))

        – Alex Cardo
        Nov 23 '18 at 13:16



















      • I might try to do like this, but my arrays are unnamed

        – Alex Cardo
        Nov 23 '18 at 13:03











      • I understand the idea, but in such a case I would have to create some additional foreach loop to mark my arrays some way. But I have no idea how to accomplish it.

        – Alex Cardo
        Nov 23 '18 at 13:08











      • Please check my edit

        – Sree
        Nov 23 '18 at 13:10











      • This is exactly how I tried to cope with my goal, but I stumbled upon two issues: 1) When I'm trying to var_dump $newArray, I can't get a result

        – Alex Cardo
        Nov 23 '18 at 13:14













      • 2) $newUniqueArray gives me just about 26 genres, which is impossible )))

        – Alex Cardo
        Nov 23 '18 at 13:16

















      I might try to do like this, but my arrays are unnamed

      – Alex Cardo
      Nov 23 '18 at 13:03





      I might try to do like this, but my arrays are unnamed

      – Alex Cardo
      Nov 23 '18 at 13:03













      I understand the idea, but in such a case I would have to create some additional foreach loop to mark my arrays some way. But I have no idea how to accomplish it.

      – Alex Cardo
      Nov 23 '18 at 13:08





      I understand the idea, but in such a case I would have to create some additional foreach loop to mark my arrays some way. But I have no idea how to accomplish it.

      – Alex Cardo
      Nov 23 '18 at 13:08













      Please check my edit

      – Sree
      Nov 23 '18 at 13:10





      Please check my edit

      – Sree
      Nov 23 '18 at 13:10













      This is exactly how I tried to cope with my goal, but I stumbled upon two issues: 1) When I'm trying to var_dump $newArray, I can't get a result

      – Alex Cardo
      Nov 23 '18 at 13:14







      This is exactly how I tried to cope with my goal, but I stumbled upon two issues: 1) When I'm trying to var_dump $newArray, I can't get a result

      – Alex Cardo
      Nov 23 '18 at 13:14















      2) $newUniqueArray gives me just about 26 genres, which is impossible )))

      – Alex Cardo
      Nov 23 '18 at 13:16





      2) $newUniqueArray gives me just about 26 genres, which is impossible )))

      – Alex Cardo
      Nov 23 '18 at 13:16













      2














      try using this code



          $link = mysqli_connect('127.0.0.1', 'root', 'pw', 'db');
      $query = "SELECT show_genres FROM tv_shows";
      $result = mysqli_query($link, $query);
      $sh_genres_array = ;
      foreach ($result as $value) {
      $sh_genres = $value['show_genres'];
      $sh_genres_array = array_merge(
      $sh_genres_array ,
      array_map('trim', explode(',',$sh_genres))
      );
      }
      echo '<pre>' . var_export(array_unique($sh_genres_array), true) . '</pre>';


      Update



      Please consider normalizing your database






      share|improve this answer






























        2














        try using this code



            $link = mysqli_connect('127.0.0.1', 'root', 'pw', 'db');
        $query = "SELECT show_genres FROM tv_shows";
        $result = mysqli_query($link, $query);
        $sh_genres_array = ;
        foreach ($result as $value) {
        $sh_genres = $value['show_genres'];
        $sh_genres_array = array_merge(
        $sh_genres_array ,
        array_map('trim', explode(',',$sh_genres))
        );
        }
        echo '<pre>' . var_export(array_unique($sh_genres_array), true) . '</pre>';


        Update



        Please consider normalizing your database






        share|improve this answer




























          2












          2








          2







          try using this code



              $link = mysqli_connect('127.0.0.1', 'root', 'pw', 'db');
          $query = "SELECT show_genres FROM tv_shows";
          $result = mysqli_query($link, $query);
          $sh_genres_array = ;
          foreach ($result as $value) {
          $sh_genres = $value['show_genres'];
          $sh_genres_array = array_merge(
          $sh_genres_array ,
          array_map('trim', explode(',',$sh_genres))
          );
          }
          echo '<pre>' . var_export(array_unique($sh_genres_array), true) . '</pre>';


          Update



          Please consider normalizing your database






          share|improve this answer















          try using this code



              $link = mysqli_connect('127.0.0.1', 'root', 'pw', 'db');
          $query = "SELECT show_genres FROM tv_shows";
          $result = mysqli_query($link, $query);
          $sh_genres_array = ;
          foreach ($result as $value) {
          $sh_genres = $value['show_genres'];
          $sh_genres_array = array_merge(
          $sh_genres_array ,
          array_map('trim', explode(',',$sh_genres))
          );
          }
          echo '<pre>' . var_export(array_unique($sh_genres_array), true) . '</pre>';


          Update



          Please consider normalizing your database







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 23 '18 at 13:30

























          answered Nov 23 '18 at 13:16









          Amarjit SinghAmarjit Singh

          778320




          778320























              0














              Not sure but try this.



               $genre_arr = RecursiveIteratorIterator(new RecursiveArrayIterator($sh_genres_array));

              $unique_genre = array_unique($genre_arr);





              share|improve this answer




























                0














                Not sure but try this.



                 $genre_arr = RecursiveIteratorIterator(new RecursiveArrayIterator($sh_genres_array));

                $unique_genre = array_unique($genre_arr);





                share|improve this answer


























                  0












                  0








                  0







                  Not sure but try this.



                   $genre_arr = RecursiveIteratorIterator(new RecursiveArrayIterator($sh_genres_array));

                  $unique_genre = array_unique($genre_arr);





                  share|improve this answer













                  Not sure but try this.



                   $genre_arr = RecursiveIteratorIterator(new RecursiveArrayIterator($sh_genres_array));

                  $unique_genre = array_unique($genre_arr);






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 23 '18 at 13:22









                  Prashant Deshmukh.....Prashant Deshmukh.....

                  66148




                  66148






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53447178%2fmerge-multiple-arrays-into-one-with-unique-values-php%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      404 Error Contact Form 7 ajax form submitting

                      How to know if a Active Directory user can login interactively

                      TypeError: fit_transform() missing 1 required positional argument: 'X'