Merge multiple arrays into one with unique values php
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
add a comment |
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
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 withgenres
and another table linkingtv_shows
to genres.
– jeroen
Nov 23 '18 at 13:09
add a comment |
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
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
php mysql
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 withgenres
and another table linkingtv_shows
to genres.
– jeroen
Nov 23 '18 at 13:09
add a comment |
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 withgenres
and another table linkingtv_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
add a comment |
3 Answers
3
active
oldest
votes
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);
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
|
show 4 more comments
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
add a comment |
Not sure but try this.
$genre_arr = RecursiveIteratorIterator(new RecursiveArrayIterator($sh_genres_array));
$unique_genre = array_unique($genre_arr);
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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);
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
|
show 4 more comments
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);
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
|
show 4 more comments
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);
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);
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
|
show 4 more comments
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
|
show 4 more comments
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
add a comment |
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
add a comment |
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
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
edited Nov 23 '18 at 13:30
answered Nov 23 '18 at 13:16
Amarjit SinghAmarjit Singh
778320
778320
add a comment |
add a comment |
Not sure but try this.
$genre_arr = RecursiveIteratorIterator(new RecursiveArrayIterator($sh_genres_array));
$unique_genre = array_unique($genre_arr);
add a comment |
Not sure but try this.
$genre_arr = RecursiveIteratorIterator(new RecursiveArrayIterator($sh_genres_array));
$unique_genre = array_unique($genre_arr);
add a comment |
Not sure but try this.
$genre_arr = RecursiveIteratorIterator(new RecursiveArrayIterator($sh_genres_array));
$unique_genre = array_unique($genre_arr);
Not sure but try this.
$genre_arr = RecursiveIteratorIterator(new RecursiveArrayIterator($sh_genres_array));
$unique_genre = array_unique($genre_arr);
answered Nov 23 '18 at 13:22
Prashant Deshmukh.....Prashant Deshmukh.....
66148
66148
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 linkingtv_shows
to genres.– jeroen
Nov 23 '18 at 13:09