Alternate arrays merge every third item - php

I have two multi-dimensional arrays (but for simplicity sake let's just say they are single dimension) I need to merge them both into a single one, but alternating elements. First array is larger, and I need second array to be merged in every third item.
Just as an example:
$array1 = array("Hello", "Bonjour", "Hola", "Ciao", "Привет", "Hallo","Nihao");
$array2 = array("World", "Monde", "Mundo");
And I want the final array to look like so
["Hello", "Bonjour", "World", "Hola", "Ciao", "Monde", "Привет", "Hallo", "Mundo", "Nihao"]
If there are much more items in the first array, then it should just keep adding them, regardless whether second array has any left or not.
How would I do that?

In your example, the third item of array2 is inserted after the third item of array1, in first and the second are inserted after the second.
Assuming this was a mistake; and $array1 is always large enough; you could use something like this:
$i=2; while($v = array_shift($array2))
{
array_splice($array1, $i, 0, [$v]);
$i+=3;
}

Related

Unset keys in an array with matching values in another array

How can I unset the keys in one array where the values contained in a second array match the values in the first array?
Actual array:
$fruits = array('Banana','Cherry','Orange','Apple');
Elements I want to remove:
$remove = array('Banana','Apple');
Need to return:
$array = array('Cherry','Orange');
I know it's possible to remove each one with unset, but I'm looking to make it in one line with two array.
Thanks.
Take a look at this function
link
$arrayWithoutTheDesiredElements = array_diff($originalArr, $toRemoveArray)
EDIT:
for your case: $array = array_diff($fruits, $remove);

Swap my element's order to be the first in an array

Let's say I have an array like so:
array(
[0]=>1
[1]=>3
[3]=>5
[15]=>6
);
Arbitrarily I want array[15] to be the first:
array(
[15]=>6
[0]=>1
[1]=>3
[3]=>5
);
What is the fastest and most painless way to do this?
Here are the things I've tried:
array_unshift - Unfortunately, my keys are numeric and I need to keep the order (sort of like uasort) this messes up the keys.
uasort - seems too much overhead - the reason I want to make my element the first in my array is to specifically avoid uasort! (Swapping elements on the fly instead of sorting when I need them)
Assuming you know the key of the element you want to shift, and that element could be in any position in the array (not necessarily the last element):
$shift_key = 15;
$shift = array($shift_key => $arr[$shift_key]);
$arr = $shift + $arr;
See demo
Updated - unset() not necessary. Pointed out by #FuzzyTree
You can try this using a slice and a union operator:
// get last element (preserving keys)
$last = array_slice($array, -1, 1, true);
// put it back with union operator
$array = $last + $array;
Update: as mentioned below, this answer takes the last key and puts it at the front. If you want to arbitrarily move any element to the front:
$array = array($your_desired_key => $array[$your_desired_key]) + $array;
Union operators take from the right and add to the left (so the original value gets overwritten).
If #15 is always last you can do
$last = array_pop($array); //remove from end
array_unshift($last); //push on front
To reorder the keys for sorting simply add
$array = array_values($array); //reindex array
#Edit - if we don't assume its always last then I would go with ( if we always know wwhat the key is, then most likely we will know its position or it's not a numerically indexed array but an associative one with numeric keys, as op did state "arbitrarily" so one has to assume the structure of the array is known before hand. )
I also dont see the need to reindex them as the op stated that it was to avoid sorting. So why would you then sort?
$item = $array[15];
unset($array[15]); //....etc.

Compare two multidimensional arrays with different number of elements

I have two multidimensional arrays with different number of elements:
$complete = array(array("24","G:\TVShows\24"),array("Lost","G:\TVShows\Lost"),array("Game of Thrones","G:\TVShows\Game of Thrones"));
$subset = array(array("24","G:\TVShows\24","English"));
The first one ($complete) is the complete list of my tv shows on disk (name of the show, path to files). The second one ($subset) come from my database and include the spoken language as a third column / element.
I would like to return the shows that I have on disk but that do not appear in databse. How can I compare those to array with different number of elements?
Thank you for your help!
Since its a multi leveled array, you could use and combine array_map() and serialize()/unserialize(). Consider this example:
$complete = array(
array("24","G:\TVShows\24"),
array("Lost","G:\TVShows\Lost"),
array("Game of Thrones","G:\TVShows\Game of Thrones"),
array("The Walking Dead","G:\TVShows\The Walking Dead"),
array("Breaking Bad","G:\TVShows\Breaking Bad"),
array("Heroes","G:\TVShows\Heroes"),
);
$subset = array(
array("24","G:\TVShows\24","English"),
array("The Walking Dead","G:\TVShows\The Walking Dead","English"),
array("Heroes","G:\TVShows\Heroes","English"),
);
$shows_not_in_db = array();
// properly format the subsets for comparison on complete
foreach($subset as $key_s => $value_s) {
array_pop($value_s); // remove the last element "English"
$subset[$key_s] = serialize($value_s);
}
// serialize each complete arrays
$complete = array_map('serialize', $complete);
$shows_not_in_db = array_map('unserialize', array_diff($complete, $subset)); // array diff them, then unserialize
print_r($shows_not_in_db);
Sample Output
Edit: For case insensitive comparisons, you may use this alternative:
$shows_not_in_db = array_map('unserialize', array_udiff($complete, $subset, 'strcasecmp'));
// sample: The walking dead - The Walking Dead

PHP array : renumber elements when new element added

I have a PHP array with elements:
$myarray = array ( "tom", "dick", "Harry" );
. I need to keep the array fixed in size of just 3 elements
. I need to add a new element "jerry" such that the array now looks like
$myarray = array ( "jerry", "tom", "dick");
so in a way I am moving the elements along and the 4th one drops out, with the newest element going at the beginning. I could write all of this by hand, renumbering the elements etc etc.
I just wondered if there was a quicker way to do this though.
Many thanks!
J
One way to do this is to utilize array_pop and array_unshift:
# Pop the last element off the array
array_pop($myarray);
# Insert the new value
array_unshift($myarray, "jerry");
Or, you can use array_merge and array_slice:
$myarray = array_merge(array("jerry"), array_slice($myarray, 0, 2));
Both of these methods reset the keys, so they will be renumbered from 0 to 2.
You might want to have a look at SplQueue. On every addition of a new element, check whether the number of elements is higher than x and dequeue if necessary.
$myarray = array ( "tom", "dick", "Harry" );
array_pop( $myarray ); //remove the last element
array_unshift( $myarray, "jerry" ); //prepend the new element

How do I make a new array containing just one instance of duplicates from 2 separate arrays while combining them in php?

So far I have done the following:
$row = mysql_fetch_assoc($result);
$row2 = mysql_fetch_assoc($result);
$duplicates = array_intersect($row, $row2);
How do I combine the 2 arrays and make a new one that just contains one instance of the previously repeated variables? (so if array $row contained the variable 'apple' 2 times and the array $row2 contained the variable 'apple' 3 times, in the new, merged array, 'apple' would only appear once.
edit: I didn't realize that the array_merge() function works differently for numbers than compared to strings. I gave the 'apple' example above but my arrays are dealing with product IDs which are numbers. PHP manual says
If, however, the arrays contain numeric keys, the later value will not
overwrite the original value, but will be appended.
and I need help in merging arrays with numbers, what should I do?
You can use
$c = array_merge($a, $b);
http://php.net/manual/en/function.array-merge.php
if you want to remove duplicate values after the merge then use array_unique();
so...
$c = array_unique(array_merge($a, $b));

Categories