Distinct Values from multiple arrays into one array - php

I am working on my PHP project where I need to list all genres in database but uniquely. please help me.
$q_cat = "SELECT DISTINCT `genres` FROM `movie_meta`";
$ex = mysqli_query($con, $q_cat);
$array = array();
while ($res=mysqli_fetch_assoc($ex)) {
foreach($res as $value) {
$x = rtrim($value,',');
$array = explode(',', $x);
}
print_r($array);
}
I am getting this arrays in output using above code:
Array
(
[0] => Action
[1] => Crime
[2] => Thriller
)
Array
(
[0] => Action
[1] => Adventure
[2] => Comedy
)
Array
(
[0] => Crime
[1] => Drama
[2] => Mystery
)
Array
(
[0] => Action
[1] => Comedy
[2] => Crime
)
Array
(
[0] => Action
[1] => Comedy
[2] => Science Fiction
)
Array
(
[0] => Action
[1] => Adventure
[2] => Fantasy
)
Array
(
[0] => Action
[1] => Adventure
[2] => Science Fiction
)
Array
(
[0] => Comedy
[1] => Crime
[2] => Drama
)
Array
(
[0] => Action
[1] => Adventure
[2] => Thriller
)
Array
(
[0] => Drama
[1] => Sport
)
Array
(
[0] => Comedy
)
Array
(
[0] => Horror
[1] => Thriller
)
Array
(
[0] => Biography
[1] => Comedy
[2] => Drama
)
Array
(
[0] => Action
[1] => Thriller
)
Array
(
[0] => Action
[1] => Science Fiction
[2] => Thriller
)
Array
(
[0] => Adventure
[1] => Drama
[2] => Thriller
)
Array
(
[0] => Drama
[1] => History
[2] => Thriller
)
Array
(
[0] => Animation
[1] => Comedy
[2] => Family
)
Array
(
[0] => Action
[1] => Comedy
)
Array
(
[0] => Adventure
[1] => Animation
[2] => Comedy
)
Array
(
[0] => Action
[1] => Drama
[2] => Thriller
)
Array
(
[0] => Action
[1] => Adventure
[2] => Biography
)
Array
(
[0] => Horror
[1] => Science Fiction
[2] => Thriller
)
Array
(
[0] => Action
[1] => Adventure
[2] => Animation
)
Array
(
[0] => Adventure
[1] => Biography
[2] => Drama
)
Array
(
[0] => Adventure
[1] => Comedy
[2] => Family
)
Array
(
[0] => Drama
[1] => Mystery
[2] => Thriller
)
Array
(
[0] => Adventure
[1] => Family
[2] => Fantasy
)
Array
(
[0] => Comedy
[1] => Drama
[2] => Romance
)
Array
(
[0] =>
)
Array
(
[0] => Adventure
[1] => Drama
[2] => Science Fiction
)
Array
(
[0] => Adventure
[1] => Science Fiction
)
Array
(
[0] => Action
[1] => Crime
[2] => Drama
)
Array
(
[0] => Comedy
[1] => Drama
)
Array
(
[0] => Science Fiction
[1] => Thriller
)
I want output like this.
Array
(
[0] => Action
[1] => Crime
[2] => Thriller
[3] => Adventure
[4] => Comedy
[5] => Drama
[6] => ......
[7] => ....
)
I mean All Distinct Values from all array into one array.

Use array_merge() function. it will merge two or more array in one
http://php.net/manual/en/function.array-merge.php

You need to make a single array for all the unique data, store data in the array and make the value as key for getting the unique value. finally use the array_values function to get the values as result.
$array = array();
while ($res=mysqli_fetch_assoc($ex)) {
foreach($res as $value) {
$x = rtrim($value,',');
foreach(explode(',', $x) as $val){
$array[$val] = $val;
}
}
}
print_r(array_values($array));

Make associative array so while making associative array duplicate values will be removed
foreach($array as $value){
$temp[$value]=$value;
}
then remove key by using php function array_values
$unique_array= array_values($temp);
print_r($unique_array);
You will get this as Output
Array
(
[0] => Action
[1] => Crime
[2] => Thriller
[3] => Adventure
[4] => Comedy
[5] => Drama
)

Related

How to merge two arrays (song&title) into one multidimensional array?

I have two arrays:
Array
(
[0] => Black
[1] => Five Hours
[2] => Bvulgari
[3] => Imaginary
)
Array
(
[0] => Pearl Jam
[1] => Deorro
[2] => Daddy's Groove
[3] => Brennan Heart
)
I want to be able to achieve the following:
I want to have the song title and the Artist into one 'dimension' of the array,
This is how I want it to be:
Array
(
[0] => Array
(
[0] => Black
[1] => Pearl Jam
)
[1] => Array
(
[0] => Five Hours
[1] => Deorro
)
[2] => Array
(
[0] => Bvulgari
[1] => Daddy's Groove
)
[3] => Array
(
[0] => Imaginary
[1] => Brennan Heart
)
)
The two arrays that are the input can change depending on the requestor's input.
You can use a simple foreach loop:
$songs_artists = array();
foreach ($songs as $key => $title) {
$songs_artists[] = array($title, $artists[$key]);
}
Output is as you desire. Demo on 3v4l.org
As an alternative you could use array_map and use both array's:
$result = array_map(function ($s, $a) {
return [$s, $a];
}, $songs, $artists);
print_r($result);
See a Php demo
Result
Array
(
[0] => Array
(
[0] => Black
[1] => Pearl Jam
)
[1] => Array
(
[0] => Five Hours
[1] => Deorro
)
[2] => Array
(
[0] => Bvulgari
[1] => Daddy's Groove
)
[3] => Array
(
[0] => Imaginary
[1] => Brennan Heart
)
)
Another way to do is Array_Combine
$songs_artists = array_combine($songs,$artists);
Array (
[Black] => Pearl Jam
[Five Hours] => Deorro
[Bvulgari] => Daddy's Groove
[Imaginary] => Brennan Heart
)

PHP merge and reorder multidimensional array

I have 2 PHP arrays that look like this..
$array1
--------
Array
(
[0] => Array
(
[0] => 64
[1] => Apple
)
[1] => Array
(
[0] => 22
[1] => Pear
)
[2] => Array
(
[0] => 3
[1] => Raisin
)
[3] => Array
(
[0] => 15
[1] => Grape
)
[4] => Array
(
[0] => 11
[1] => Banana
)
[5] => Array
(
[0] => 4
[1] => Orange
)
)
$array2
--------
Array
(
[0] => Array
(
[0] => 22
[1] => Pear
)
[1] => Array
(
[0] => 11
[1] => Banana
)
)
I want to merge the arrays together but put the matching items from $array2 at the top so the result would look like this...
$array3
-------
Array
(
[0] => Array
(
[0] => 22
[1] => Pear
)
[1] => Array
(
[0] => 11
[1] => Banana
)
[2] => Array
(
[0] => 64
[1] => Apple
)
[3] => Array
(
[0] => 3
[1] => Raisin
)
[4] => Array
(
[0] => 15
[1] => Grape
)
[5] => Array
(
[0] => 4
[1] => Orange
)
)
I'm not sure how to approach, should I merge the two first and then try and do some ordering, or is there a more efficient approach?
Get the 2nd array and then a rest of the 1st array
array_merge($arr2, array_udiff($arr1, $arr2, function($i1, $i2) {return $i1[0]-$i2[0];}));

Combine single-dimensional array with multi-dimensional array

I'm trying to combine a single-dimensional array with a multi-dimensional array. I see that there is an array_combine function and an array_merge function, but they don't seem to give me the result I need. I have the following arrays:
$days = Array (
[0] => Array (
[0] => 3
[1] => 6
)
[1] => Array (
[0] => 6
[1] => 12
)
[2] => Array (
[0] => 2
[1] => 4
)
)
$names = Array (
[0] => Joe Smith
[1] => John Doe
[2] => Jack Frost
)
and this is the result I get when using array_merge($days,$names):
$result = Array (
[0] => Array (
[0] => 3
[1] => 6
)
[1] => Array (
[0] => 6
[1] => 12
)
[2] => Array (
[0] => 2
[1] => 4
)
[3] => Joe Smith
[4] => John Doe
[5] => Jack Frost
)
How do I get the following result:
$result = Array (
[0] => Array (
[0] => John Smith
[1] => Array (
[0] => 3
[1] => 6
)
)
[1] => Array (
[0] => John Doe
[1] => Array (
[0] => 6
[1] => 12
)
)
[2] => Array (
[0] => Jack Frost
[1] => Array (
[0] => 2
[1] => 4
)
)
)
Any ideas? Thanks
Using example #4 from the docs for array_map(), here's a cool way to do it:
$result = array_map(null, $names, $days);

How to find some elements in a multi-dimensional array, with PHP?

I have a a multi-dimensional array, whose name is: $MyArray = array();.
This array can be used for showing the relation between things.
Array
(
[parent] => Array
(
[0] => Array
(
[1] => tom
[2] => bob
)
[1] => Array
(
[1] => liza
[2] => bob
)
[2] => Array
(
[1] => bob
[2] => john
)
[3] => Array
(
[1] => pet
[2] => john
)
)
[gender] => Array
(
[0] => Array
(
[1] => tom
[2] => male
)
[1] => Array
(
[1] => liza
[2] => female
)
[2] => Array
(
[1] => bob
[2] => male
)
[3] => Array
(
[1] => pet
[2] => female
)
)
)
Now, I want to return an array, which must be satisfied with all conditions:
Finds in the gender array, what line has the male value. This value is called: TempArray.
In what line of the TempArray; and, continue to finds what name in Sol_1 into the parent array.
Then returns into a multi-dimensional array: ResArray, like:
Array
(
[father] => Array
(
[0] => Array
(
[1] => tom
[2] => bob
)
[1] => Array
(
[1] => bob
[2] => john
)
)
[mother] => Array
(
[0] => Array
(
[1] => liza
[2] => bob
)
[1] => Array
(
[1] => pet
[2] => john
)
)
)
How to do it, with PHP?

how to sort Multi dimesional array in php?

Hello I have array like :
Array
(
[0] => Array
(
[0] => Product_1
[1] => Gold
)
[1] => Array
(
[0] => Product_1
[1] => Silver
)
[2] => Array
(
[0] => Product_1
[1] => Black
)
[3] => Array
(
[0] => Product_1
[1] => Red
)
[4] => Array
(
[0] => Product_2
[1] => Silver
)
[5] => Array
(
[0] => Product_2
[1] => Gold
)
[6] => Array
(
[0] => Product_2
[1] => Black
)
How can I sort this array into
[Product_1] => Array
(
[0] Silver
[1] Black
[2] Red
)
[Product_2] => Array
(
[0] Silver
[1] Gold
[2] Black
)
Can someone help?
Or you can simply use array_walk as
$result = [];
array_walk($array,function($v,$k)use(&$result){$result[$v[0]][] = $v[1];});
print_r($result);
Output:
Array
(
[Product_1] => Array
(
[0] => Gold
[1] => Silver
[2] => Black
[3] => Red
)
[Product_2] => Array
(
[0] => Silver
[1] => Gold
[2] => Black
)
)
Just loop through your $array and push each color value to a $result array having the product name as the key.
$array = [
['Product_1', 'Gold'],
['Product_1', 'Silver'],
['Product_1', 'Black'],
['Product_1', 'Red'],
['Product_2', 'Silver'],
['Product_2', 'Gold'],
['Product_2', 'Black'],
];
$result = [];
foreach ($array as $values) {
list ($product, $color) = $values;
$result[$product][] = $color;
}
print_r($result);
Output:
Array
(
[Product_1] => Array
(
[0] => Gold
[1] => Silver
[2] => Black
[3] => Red
)
[Product_2] => Array
(
[0] => Silver
[1] => Gold
[2] => Black
)
)

Categories