This question already has answers here:
Merge two indexed arrays of indexed arrays based on first column value
(2 answers)
Closed 5 months ago.
I have 5 differents arrays in my script :
$array1 = array(
array( "id"=>"1", "title"=>"Batman" ),
array( "id"=>"2", "title"=>"Spiderman" ),
array( "id"=>"3", "title"=>"Titanic" ),
array( "id"=>"4", "title"=>"Dracula" ),
);
$array2 = array(
array( "id"=>"1", "releasedate"=>"1926" ),
array( "id"=>"2", "releasedate"=>"1956" ),
array( "id"=>"3", "releasedate"=>"2001" ),
array( "id"=>"4", "releasedate"=>"1982" ),
);
etc ...
As you see, info about movie number 1 is splitted on all the arrays (in fact -> 5 arrays). Then I would like to merge all my arrays to get something like this :
$array_FINAL = array(
array( "id"=>"1", "title"=>"Batman", "releasedate"=>"1926" ),
array( "id"=>"2", "title"=>"Spiderman", "releasedate"=>"1956" ),
array( "id"=>"3", "title"=>"Titanic", "releasedate"=>"2001" ),
array( "id"=>"4", "title"=>"Dracula", "releasedate"=>"1982" ),
);
I tried array_merge, array_combine, no good results. I also checked other topics on stackoverflow but no one help me (i may miss the one i need!)
Any help ? :)
EDIT : Sorry, i did would give a little more details ... rows in the arrays could be in misc. order, then according to my code example : the movie "Batman" can be in 1st row in the first array, but in the 3rd row in the second array...
If the order of both arrays is the same, you can simply use
$array_final = array_replace_recursive($array1, $array2);
However, if you want to merge them by the "id", you need to loop through them. A solution with the complexity O(m*n):
$array_final = array();
// loop through all movies in array1
foreach($array1 as $movie){
foreach($array2 as $movie_release) {
if ($movie['id'] == $movie_release['id']) {
$movie['releasedate'] = $movie_release['releasedate'];
break;
}
}
$array_final[] = $movie;
}
And a little less complex O(m+n):
// create arrays with the ID as key
$titles = array_column($array1, 'title', 'id');
$releasedates = array_column($array2, 'releasedate', 'id');
$array_final = array();
foreach($titles as $id => $title) {
$array_final[] = array(
'id' => $id,
'title' => $title,
'releasedate' => $releasedates[$id]
);
}
One of the solutions is:
$array1 = array(
array( "id"=>"1", "title"=>"Batman" ),
array( "id"=>"2", "title"=>"Spiderman" ),
array( "id"=>"3", "title"=>"Titanic" ),
array( "id"=>"4", "title"=>"Dracula" ),
);
$array2 = array(
array( "id"=>"1", "releasedate"=>"1926" ),
array( "id"=>"2", "releasedate"=>"1956" ),
array( "id"=>"3", "releasedate"=>"2001" ),
array( "id"=>"4", "releasedate"=>"1982" ),
);
// here we create pairs `id => releasedate`
$new_array2 = [];
foreach ($array2 as $v) {
$new_array2[$v['id']] = $v['releasedate'];
}
foreach ($array1 as &$value) {
// here we try to find key `$value['id']` in `$new_array2`
// and get it's value
if (isset($new_array2[$value['id']])) {
$value['releasedate'] = $new_array2[$value['id']];
}
}
If you're 100% sure that orders of ids are the same in both arrays you can just:
$i = 0;
foreach ($array1 as &$value) {
$value['releasedate'] = $array2[$i]['releasedate'];
$i++;
}
See this code prototype (it is simple and don't need to explanations I believe):
$fArr = [];
for ( $i = 1; $i <= 4; $i++ ) {
$fArr[] = [
'id' => $i,
'title' => $array1[$i - 1]['title'],
'releasedate' => $array2[$i - 1]['releasedate'],
];
}
Sandbox code example
Assuming all of your five arrays contain the id key, you can do this with a nested foreach loop.
foreach (array_merge($array1, $array2, $array3, $array4, $array5) as $item) {
foreach ($item as $key => $value) {
$result[$item['id']][$key] = $value;
}
}
The outer foreach iterates all the rows in all the arrays. The inner one loops over each column and assigns its value to the corresponding key in the result array.
Related
My question is how can I search an array built this way? Basically there may be a need to repeat the key and this is what I got so far to maybe solve this. If the price is the same for 2 different items I cannot have 2 keys with the same value.
Please feel free to improve on array layout.
$price_list = array(
1 => array("9.99", "EA_WTRESRVD"),
2 => array("9.99", "EA_WTRESRV")
);
Provided there will never be any duplication of the second column, you can do this:
$search = "EA_WTRESRVD"; //value to search for
$price_list = array(
1 => array("9.99", "EA_WTRESRVD"),
2 => array("9.99", "EA_WTRESRV")
);
$array = array_column($price_list, 0, 1);
echo $array[$search];
I would suggest that if you have a unique product code (SKU), you should use this to index your array.
$products = [
'EA_WTRESRVD' => [
'name' => '...',
'price' => 9.99,
// ...
],
'EA_WTRESRV' => [
'name' => '...',
'price' => 9.99,
// ...
],
];
Then you can access the price of any product by it's SKU.
$price = $products['EA_WTRESRV']['price'];
Here's one way:
<?php
$price_list = [ 1 => array("9.99", "EA_WTRESRVD"),
2 => array("9.99", "EA_WTRESRV")];
$search = "EA_WTRESRV";
foreach ($price_list as $arr) {
if (in_array( $search, $arr )) {
echo $search;
}
}
The foreach iterates over the multidimensional array whose elements are each arrays. Each array is inspected by in_array() for the search term.
However, this is not the only way. If you wish to avoid in_array(), you could also code as follows:
<?php
$price_list = [ 1 => array("9.99", "EA_WTRESRVD"),
2 => array("9.99", "EA_WTRESRV")];
$search = "EA_WTRESRV";
$len = strlen($search);
foreach ($price_list as $arr) {
$val = array_values($arr);
foreach($val as $v) {
if ( ( strpos( $v,$search )) !== false) {
if ( strlen($v) == $len) {
echo "$search is in the price list.\n";
}
}
}
}
I have a PHP array like this:
$arr = array(
'id' => 'app.settings.value.id',
'title' => 'app.settings.value.title',
'user' => 'app.settings.value.user'
);
I want to remove '.id', '.title' and '.user' in the array values. I want to insert the key of this array at the end of the value. I know, that I can get an array key by passing an array and a value to 'array_search', but I want the key within this one array definition - at the end of it's value. Is this possible?
I don't like this, but I guess it's simple and works:
$arr = array(
'id' => 'app.settings.value',
'title' => 'app.settings.value',
'user' => 'app.settings.value'
);
$result = array();
foreach ($arr AS $key => $value) {
$result[$key] = $value . '.' . $key;
}
You're storing app.settings.value which is the same for all array items, so personally I'd prefer:
$arr = array(
'id',
'title',
'user'
);
function prepend_key($key)
{
return 'app.settings.value.' . $key;
}
$result = array_map('prepend_key', $arr);
With $result containing:
array(
'app.settings.value.id',
'app.settings.value.title',
'app.settings.value.user'
);
At the end of the day, either works :)
I have a loop that builds an array of associative arrays that looks like this:
array(
'foo' => '',
'bar' => '',
'thingys' => array()
)
on each iteration of the loop, I want to search through the array for an associate array that's 'foo' and 'bar' properties match those of the current associate array. If it exists I want to append the thingys property of the current associative array to the match. Otherwise append the entire thing.
I know how to do this with for loops, but I'm wondering if there is a simpler way to do this with an array function. I'm on php 5.3.
Example
<?php
$arr = array(
array(
'foo' => 1,
'bar' => 2,
'thing' => 'apple'
),
array(
'foo' => 1,
'bar' => 2,
'thing' => 'orange'
),
array(
'foo' => 2,
'bar' => 2,
'thing' => 'apple'
),
);
$newArr = array();
for ($i=0; $i < count($arr); $i++) {
$matchFound = false;
for ($j=0; $j < count($newArr); $j++) {
if ($arr[$i]['foo'] === $newArr[$j]['foo'] && $arr[$i]['bar'] === $newArr[$j]['bar']) {
array_push($newArr[$j]['thing'], $arr[$i]['things']);
$matchFound = true;
break;
}
}
if (!$matchFound) {
array_push($newArr,
array(
'foo' => $arr[$i]['foo'],
'bar' => $arr[$i]['bar'],
'things' => array($arr[$i]['thing'])
)
);
}
}
/*Output
$newArr = array(
array(
'foo' => 1,
'bar' => 2,
'things' => array('orange', 'apple')
),
array(
'foo' => 2,
'bar' => 2,
'things' => array('apple')
),
)
*/
?>
I don't know if it is possible through a built-in function, but I think no. Something can be implemented through array_map, but anyway you have to perform a double loop.
I propose you a one-loop solution using a temporary array ($keys) as index of already created $newArr items, based on foo and bar; elements of original array are processed through a foreach loop, and if a $keys element with first key as foo value and second key as bar value exists, then the current thing value is added to the returned key index of $newArr, otherwise a new $newArray element is created.
$newArr = $keys = array();
foreach( $arr as $row )
{
if( isset( $keys[$row['foo']][$row['bar']] ) )
{ $newArr[$keys[$row['foo']][$row['bar']]]['thing'][] = $row['thing']; }
else
{
$keys[$row['foo']][$row['bar']] = array_push( $newArr, $row )-1;
$newArr[$keys[$row['foo']][$row['bar']]]['thing'] = array( $row['thing'] );
}
}
unset( $keys );
3v4l.org demo
Edit: array_map variant
This is the same solution above, using array_map instead of foreach loop. Note that also your original code can be converted in this way.
$newArr = $keys = array();
function filterArr( $row )
{
global $newArr, $keys;
if( isset( $keys[$row['foo']][$row['bar']] ) )
{ $newArr[$keys[$row['foo']][$row['bar']]]['thing'][] = $row['thing']; }
else
{
$keys[$row['foo']][$row['bar']] = array_push( $newArr, $row )-1;
$newArr[$keys[$row['foo']][$row['bar']]]['thing'] = array( $row['thing'] );
}
}
array_map( 'filterArr', $arr );
3v4l.org demo
I expect array_combine to be able to take two arrays such as:
array('sample', 'sample_two', 'sample');
array(array('info', 'info_two'), array('bananas'), array('more_stuff'));
and produce:
array(
'sample' => array(
'info', 'info_two', 'more_stuff'
),
'sample_two' => array(
'bananas'
)
);
instead I get:
array(
'sample' => array(
'more_stuff'
),
'sample_two' => array(
'bananas'
)
);
Now I know php doesn't allow for duplicate key's so how can I use array_combine or some other method to achieve the desired array? It is safe to say that, the first array, will always match the second array in terms of layout. So you can draw lines between the values of array one and array two.
Why not writing your own function?
function my_array_combine(array $keys, array $values) {
$result = array();
for ($i = 0; $i < count($keys); $i++) {
$key = $keys[$i];
if (!isset($result[$key])) {
$result[$key] = array();
}
$result[$key] = array_merge($result[$key], $values[$i]);
}
return $result;
}
I have a nested assocative array which might look something like this:
$myarray = array(
['tiger'] => array(
['people'], ['apes'], ['birds']
),
['eagle'] => array(
['rodents'] => array(['mice'], ['squirrel'])
),
['shark'] => ['seals']
);
How can I loop through the first layer (tiger, eagle, shark) in a random order and ensure that I cover them all in my loop? I was looking at the PHP function shuffle();, but I think that function messes up the whole array by shuffling all layers.
You can randomly sort an array like this, it will keep the keys and the values
<?php
$myarray = array(
'tiger' => array(
'people', 'apes', 'birds'
),
'eagle' => array(
'rodents' => array('mice', 'squirrel')
),
'shark' => 'seals'
);
$shuffleKeys = array_keys($myarray);
shuffle($shuffleKeys);
$newArray = array();
foreach($shuffleKeys as $key) {
$newArray[$key] = $myarray[$key];
}
print_r($newArray);
You can get the keys using array_keys(). Then you can shuffle the resulting key array using shuffle() and iterate through it.
Example:
$keys = array_keys($myarray);
shuffle($keys);
foreach ($keys as $key) {
var_dump($myarray[$key]);
}
According to my test, shuffle only randomizes 1 layer. try it yourself:
<?php
$test = array(
array(1,2,3,4,5),
array('a','b','c','d','e'),
array('one','two','three','four','five')
);
shuffle($test);
var_dump($test);
?>