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

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
)

Related

how can i sort 2 dimensional array based on index array in PHP?

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.

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];}));

Merge two arrays based on key

UPDATED after trying the below suggestion it now gives the correct affect but enters it to many times.
Hi I have the following code what this does is creates two arrays then blend them into one:
$list_claimed_users = get_users('meta_key=claimed');
foreach ( $list_claimed_users as $list_claimed_user ) {
$a = get_user_meta($list_claimed_user->ID , "claimed");
$nicknames[] = get_user_meta($list_claimed_user->ID , "nickname");
$unserialized[] = unserialize($a[0]);
foreach($unserialized as $key => &$metadata) array_push($metadata, $nicknames[$key]);
}
$claimed_array[] = $unserialized;
print_r($claimed_array);
BUt it now loops and adds the $a data in an extra time for each loop
Array
(
[0] => Array
(
[0] => Array
(
[0] => 31.08.2016
[1] => prize8
[2] => Array
(
[0] => test5
)
[3] => Array
(
[0] => test5
)
[4] => Array
(
[0] => test5
)
[5] => Array
(
[0] => test5
)
)
[1] => Array
(
[0] => 31.07.2017
[1] => prize7
[2] => Array
(
[0] => test6
)
[3] => Array
(
[0] => test6
)
[4] => Array
(
[0] => test6
)
)
[2] => Array
(
[0] => 31.08.2017
[1] => prize5
[2] => Array
(
[0] => test7
)
[3] => Array
(
[0] => test7
)
)
[3] => Array
(
[0] => 21.08.2017
[1] => prize6
[2] => Array
(
[0] => test8
)
)
)
)
Could anyone please advise the best way to merge the two if if there is a simplier way to achieve this when creating the arrays originally.
Any help would be hugely appreciated
After retrieving the nicknames and the meta-data, but before adding result to $claimed_array, you can do:
foreach($a as $key => &$metadata) array_push($metadata, $nicknames[$key]);
Then just add $a to the $claimed_array:
$claimed_array[] = $a;

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
)
)

php - Regular Expression to match someting that starts with

I am terribly sorry but the array looks like this:
Array
(
[1] => Array
(
[0] => msie6.0
[1] => 7
)
[2] => Array
(
[0] => safari5.0.3
[1] => 5
)
[3] => Array
(
[0] => chrome18.0.1025.308
[1] => 1
)
[4] => Array
(
[0] => firefox20.0
[1] => 4
)
[5] => Array
(
[0] => msie7.0
[1] => 915
)
and so on...
When i try to replace for example msie6.0and msie7.0 with InternetExplorer and add it occurence :
preg_match("/#^msie(.*)$#i/is", $results, $matches);
$test = $matches[0] ;
print_array($test);
$results["#^startText(.*)$#i"] = $results['InternetExplorer'];
print_array($results);
unset($results["/#^msie(.*)$#i/is]);
it not match the perfect as i want. any solution for that ?
in order to have :
Array
(
[1] => Array
(
[0] => InternetExplorer
[1] => 922
)
[2] => Array
(
[0] => safari5.0.3
[1] => 5
)
[3] => Array
(
[0] => chrome18.0.1025.308
[1] => 1
)
[4] => Array
(
[0] => firefox20.0
[1] => 4
)
After your clarification in comment. I think you don't need regex, strpos is enough too that job.
$rows["InternetExplorer"] = 0;
foreach($rows as $key => $value){
if(strpos($key,"msie") !== false){
$rows["InternetExplorer"] += $value;
unset($rows[$key]);
}
}
DEMO.

Categories