I am wondering if anyone can advise on how to remove duplicate items from just the subarray.
From the example below index 0 has blue and red, and index 2 has blue yellow blue. I want the duplicates to be removed at the index level so the output of index 0 should remain blue red and the output of index 2 should be blue yellow.
I have used a few methods but all the attempts I have made do not remove at the sub-array level.
Array
(
[0] => Array
(
[0] => blue
[1] => red
)
[1] => Array
(
[0] => green
[1] => brown
)
[2] => Array
(
[0] => blue
[1] => yellow
[2] => blue
)
)
Should return as:
Array
(
[0] => Array
(
[0] => blue
[1] => red
)
[1] => Array
(
[0] => green
[1] => brown
)
[2] => Array
(
[0] => blue
[1] => yellow
)
)
Thank You
You can use array_unique()` for this:
$array = [
['blue', 'red'],
['green', 'brown'],
['blue', 'yellow', 'blue']
];
$new_array = [];
foreach ( $array as $single ) {
$new_array[] = array_unique( $single );
}
$new_array will print_r to this:
(
[0] => Array
(
[0] => blue
[1] => red
)
[1] => Array
(
[0] => green
[1] => brown
)
[2] => Array
(
[0] => blue
[1] => yellow
)
)
Related
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
)
I have an array in PHP like this :
Array
(
[0] => Array
(
[0] => black
[2] => brown
[1] => red
)
[1] => Array
(
[2] => car
[0] => bicycle
[1] => motorcycle
)
)
How do I get the array, like this :
Result:
Array
(
[0] => Array
(
[0] => black
[1] => red
[2] => brown
)
[1] => Array
(
[0] => bicycle
[1] => motorcycle
[2] => car
)
)
Thanks
You could try like this perhaps:
$arr=array(
array(2=>'red',1=>'green',3=>'black'),
array(1=>'pink',3=>'blue',2=>'yellow')
);
array_walk( $arr, function(&$v,$k){
ksort($v);
});
echo '<pre>', print_r( $arr, true ), '</pre>';
Outputs:
Array
(
[0] => Array
(
[1] => green
[2] => red
[3] => black
)
[1] => Array
(
[1] => pink
[2] => yellow
[3] => blue
)
)
Use the krsort() function to sort an associative array in descending order, according to the key.
I have an array that I'm trying to remodel with Hash::combine.
This is my array
Array
(
[0] => Array
(
[T1] => Array
(
[second] => APPLES
[Color] => GREEN
[Color_en] => #99cc66
[nombre] => 56
)
)
[1] => Array
(
[T1] => Array
(
[second] => APPLES
[Color] => BLUE
[Color_en] => #0099ff
[nombre] => 678
)
)
[2] => Array
(
[T1] => Array
(
[second] => BANANAS
[Color] => GREEN
[Color_en] => #99cc66
[nombre] => 366
)
)
[3] => Array
(
[T1] => Array
(
[second] => BANANAS
[Color] => BLUE
[Color_en] => #0099ff
[nombre] => 2000
)
)
)
And what I want to achieve is the array below :
Array
(
[0] => Array
(
[0] => Array
(
[y] => 56
[color] => #99cc66
)
[1] => Array
(
[y] => 678
[color] => #0099ff
)
)
)
[1] => Array
(
[0] => Array
(
[y] => 366
[color] => #99cc66
)
[1] => Array
(
[y] => 2000
[color] => #0099ff
)
)
)
So first array is "APPLES" and second is "BANANAS", "y" is "nombre and "Color_en" becomes "color".
I've tried so many different ways with Hash::combine but I'm out of ideas how tho do this.
This is outside the scope of Hash::combine(). You can't use it to rename array indices.
The closest thing you could probably do is combine the array values by {n}.T1.second to group the fruits together like this:-
Hash::combine($array, '{n}.T1.nombre', '{n}.T1', '{n}.T1.second');
For what you are trying to achieve you will need to rebuild the array yourself using a foreach loop. Something like this:-
$data = [];
foreach ($array as $value) {
$data[$value['T1']['second']][] = [
'y' => $value['T1']['nombre'],
'color' => $value['T1']['Color_en']
];
}
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
)
)
I have a multidimensional array and am trying to group by a value of one the keys.
So
Array (
[0] => Array (
[name] => Edward Foo
[desc_topic] => Array (
[0] => Apple
[1] => Banana
[2] => Orange
)
[qtd_posts] => Array (
[0] => 10
[1] => 20
[2] => 50
)
)
[1] => Array (
[name] => Michael Max
[desc_topic] => Array (
[0] => Apple
[1] => Banana
[2] => Orange
)
[qtd_posts] => Array (
[0] => 10
[1] => 10
[2] => 10
)
)
[2] => Array (
[name] => Edward Foo
[desc_topic] => Array (
[0] => Apple
[1] => Banana
[2] => Orange
)
[qtd_posts] => Array (
[0] => 5
[1] => 10
[2] => 30
)
)
[3] => Array (
[name] => Michael Max
[desc_topic] => Array (
[0] => Apple
[1] => Banana
[2] => Orange
)
[qtd_posts] => Array (
[0] => 8
[1] => 8
[2] => 20
)
)
And I really need:
Array (
[0] => Array (
[name] => Edward Foo
[desc_topic] => Array (
[0] => Apple
[1] => Banana
[2] => Orange
)
[qtd_posts] => Array (
[0] => 15
[1] => 30
[2] => 80
)
)
[1] => Array (
[name] => Michael Max
[desc_topic] => Array (
[0] => Apple
[1] => Banana
[2] => Orange
)
[qtd_posts] => Array (
[0] => 18
[1] => 18
[2] => 30
)
)
)
I'm assuming the following:
every name entry in the original array has an identical desc_topic sub-array (e.g. they all have the same Apple/Banana/Orange values for every instance.
the qtd_posts sub-array has the to-be-grouped values in the same corresponding slots (e.g. all '1' entries are to be summed together, all '2' entries summed together, etc...)
You want to preserve the parent array keys so that all 'Edward Foo' entries will use the first key used by an Edward Foo entry (e.g. 0)
If that applies, then something like this should work:
$newarr = array();
$reverse_map = array();
foreach($array as $idx => $entry) {
if (isset($reverse_map[$entry['name']]) {
// have we seen this name before? retrieve its original index value
$idx = $reverse_map[$entry['name']];
} else {
// nope, new name, so store its index value
$reverse_map[$entry['name']] = $idx;
}
// copy the 'constant' values
$newarr[$idx]['name'] = $entry['name'];
$newarr[$idx]['desc_top'] = $entry['desc_topic'];
// sum the qtd_post values to whatever we previously stored.
foreach($entry['qtd_posts'] as $x => $y) {
$newarr[$idx]['qtd_posts'][$x] += $y;
}
}