merging two arrays and keep duplicate values - php

Is there a php function to merge 2 arrays and keep duplicates?
Like:
$a=array('a','b','c');
$b=array('b','c','b');
array_merge2($a,$b);
//result: array('a','b','c','b','c','b');
thanks
add1:
what the... i previously tested array_merge and it didn't keep duplicate values =/

In the examples for array_splice it shows how to add the contents of one array onto the end of another. Just replace the last parameter with your array.
array_splice($input, count($input), 0, array($x, $y));
So in your example:
$a=array('a','b','c');
$b=array('b','c','b');
array_splice($a, count($a), 0, $b);

array_merge keeps the duplicates.
http://codepad.org/XGcMAi3z

Related

php: How to get the second-to-last element of an array?

There is an array, the count of the element is unknown,like this:
$arr=['a','m','q','y',....'b','f','n','s'];
How to get the second-to-last element in PHP?
You can use array_slice() like this:
<?php
$arr=['a','m','q','y','b','f','n','s'];
echo array_slice($arr, -2, 1)[0];
Output:
n
Note: this will work regardless of the array type: indexed or associative. So even if the keys are not 0, 1, 2, 3, etc... then this would still work.
Since there are no defined keys, it's a little easier:
$second_to_last = $arr[count($arr) - 3];
I think this is the answer;
$arr[count($arr)-3]

Getting the number of different values in a PHP array

I need to know the number of different values in an array. For example if it contains (5, 5, 5) then I should get 1. If it contains (4, 4, 2, 2) I should get 2. If (5, 5, 5, 5, 2, 2, 1, 5) then 3.
Is there a function that can achieve that directly in PHP?
echo count(array_unique($arr));
you can use array_unique which returns unique elements of array (result is also array type) and then count on result
Two simple ways to achieve that:
$diff_count = count(array_unique($input_arr));
$diff_count = count(array_flip($input_arr)); <-- recommended (performance)
array_unique() :
Removes duplicate values from an array - you can add adittional flags such as:
SORT_REGULAR - compare items normally (don't change types)
SORT_NUMERIC - compare items numerically
SORT_STRING - compare items as strings
SORT_LOCALE_STRING - compare items as strings, based on the current locale.
array_flip():
Exchanges all keys with their associated values in an array - so duplicates keys are overwritten and you get a final array with the unique values as keys.
Flipping it twice will result in an array with unique values and the highest keys there were in the original input array.
More reading:
array_unique.
array_flip.
Benchmark:
Since the array_flip uses only O(n) exactly its much faster - using an array of 10,000 numbers as an input:
array_unique + count: ~28.615 ms.
array_flip + count: ~0.5012 ms. (winner)
Is there a function that can achieve that directly in PHP?
The closest one would be array_unique() but here is a simple function to achieve what you are looking for:
$arr = array(5,5,5,5,2,2,1,5);
function array_diff_values( $array ) {
return count( array_unique($array) );
}
$count = array_diff_values( $arr );
result of $count is 3

PHP sorting of multi-dimensional array by price value

Say I have a multi-dimensional array set up as follows:
$matrix[1][1]=4.54; $matrix[2][1]="apples"; $matrix[3][1]="coles";
$matrix[1][2]=7.2140230; $matrix[2][2]="apples"; $matrix[3][2]="safeway";
$matrix[1][3]=15.56; $matrix[2][3]="oranges"; $matrix[3][3]="coles";
$matrix[1][4]=2.34; $matrix[2][4]="bananas"; $matrix[3][4]="safeway";
$matrix[1][5]=27.98; $matrix[2][5]="grapes"; $matrix[3][5]="coles";
$matrix[1][6]=17.68493403; $matrix[2][6]="oranges"; $matrix[3][6]="safeway";
And I wish to re-arrange by the pricing information which I've stored under the first 1st column, so that the new order of $matrix would be:
$matrix[1][1]=2.34; $matrix[2][1]="bananas"; $matrix[3][1]="safeway";
$matrix[1][2]=4.54; $matrix[2][2]="apples"; $matrix[3][2]="coles";
$matrix[1][3]=7.2140230; $matrix[2][3]="apples"; $matrix[3][3]="safeway";
$matrix[1][4]=15.56; $matrix[2][4]="oranges"; $matrix[3][4]="coles";
$matrix[1][5]=17.68493403; $matrix[2][5]="oranges"; $matrix[3][5]="safeway";
$matrix[1][6]=27.98; $matrix[2][6]="grapes"; $matrix[3][6]="coles";
What would be the best way to achieve this? I've read other questions about sorting multi-dimensional arrays but have had trouble implementing because those examples seemed to have associative arrays with keys and elements, whereas I am just using the different numbers to store each piece of data. I would prefer not to change the way I am storing data in the array as the actual script is quite long and complex and so this would involve a lot of re-work.
I am completely new to PHP so my apologies if I am missing something obvious here. Thanks for your help.
EDIT: Thanks everyone for your advice, scessors code is exactly what I needed. Halfer - to your 1st question - Yes, 2nd post - good point, I will implement this. Thanks again everyone!
If it's no problem that the sorted array begins with zero, you can use array_multisort:
array_multisort(
$matrix[1], SORT_ASC, SORT_NUMERIC,
$matrix[2], SORT_ASC, SORT_STRING,
$matrix[3], SORT_ASC, SORT_STRING
);
Also see my example.
I believe that phps array_multisort will do it for you:
Heres an example from docs:
<?php
$ar1 = array(10, 100, 100, 0);
$ar2 = array(1, 3, 2, 4);
array_multisort($ar1, $ar2);
var_dump($ar1);
var_dump($ar2);
?>
In this example, after sorting, the first array will contain 0, 10, 100, 100. The second array will contain 4, 1, 2, 3. The entries in the second array corresponding to the identical entries in the first array (100 and 100) were sorted as well.
From: http://php.net/manual/en/function.array-multisort.php
Using my_uksort provided by Adam Backstrom
<?php
function my_uksort($a, $b) {
global $matrix;
asort($matrix[1]);
return $matrix[1][$a] < $matrix[1][$b] ? -1 : 1;
}
uksort($matrix[2], 'my_uksort');
uksort($matrix[3], 'my_uksort');
print_r($matrix);
?>
DEMO

Ordering PHP arrays based on duplicate values

I have an array which contains duplicate values. I want to sort the array so that the values with the most duplicates appear first in line. Here's an example of my array:
array(1, 2, 3, 2, 1, 2, 2);
I want to sort this array so that it orders itself based on the amount of duplicates into the following:
array(2, 1, 3);
'2' has the most duplicates so it's sorted first, followed by values will less duplicates. Does anyone know how I can accomplish this?
$acv=array_count_values($array); // 1=>2, 2=>3,3=>1
arsort($acv); //save keys, 2=>3, 1=>2, 3=>1
$result=array_keys($acv); //get only keys 2,1,3
traverse the array, and increment OCCURENCES in another key-value associative array.
sort the second array by values.
get array_keys of second array.

Deleting from PHP array and having indexes adjusted

If I have this array:
<?php
$array[0]='foo';
$array[1]='bar';
$array[2]='foo2';
$array[3]='bar3';
?>
And I want to delete the second entry ($array[1]), but so that all the remaining entries' indexes automatically get adjusted, so the next 2 elements whose indexes are 2 and 3, become 1 and 2.
How can this be done, or is there any built in function for this?
There are several ways to do that. One is to use array_values after deleting the item to get just the values:
unset($array[1]);
$array = array_values($array);
var_dump($array);
Another is to use array_splice:
array_splice($array, 1, 1);
var_dump($array);
You can use array_splice(). Note that this modifies the passed array rather than returning a changed copy. For example:
$array[0] = 'foo';
//etc.
array_splice($array, 1, 1);
print_r($array);
I always use array_merge with a single array
$array = array_merge($array);
print_r($array);
from php.net:
http://us.php.net/array_merge
If only one array is given and the array is numerically indexed, the keys get reindexed in a continuous way.

Categories