Is there any function in PHP which will give an array of uncommon values from two or more arrays?
For example:
$array1 = array( "green", "red", "blue");
$array2 = array( "green", "yellow", "red");
....
$result = Function_Needed($array1, $array2,...);
print_r($result);
Should give the output:
array("blue", "yellow", ...);
Use array_diff and array_merge:
$result = array_merge(array_diff($array1, $array2), array_diff($array2, $array1));
Here's a demo.
For multiple arrays, combine it with a callback and array_reduce:
function unique(&$a, $b) {
return $a ? array_merge(array_diff($a, $b), array_diff($b, $a)) : $b;
}
$arrays = array(
array('green', 'red', 'blue'),
array('green', 'yellow', 'red')
);
$result = array_reduce($arrays, 'unique');
And here's a demo of that.
$result = array_diff($array1, $array2) + array_diff($array2, $array1);
Try array_diff.
This should do it. It can be extended to work with more than two arrays. It basically counts the common key occurrences and returns those with count of one:
$a = array('yellow', 'blue', 'red', 'green');
$b = array('blue', 'purple', 'green');
function unintersect($a, $b)
{
$x = array_fill_keys($a, 1);
foreach ($b as $v) {
$x[$v]++; // this might trigger warning
}
return array_keys(array_filter($x, function($v) {
return $v === 1;
}));
}
print_r(unintersect($a, $b));
Returns:
Array
(
[0] => yellow
[1] => red
[2] => purple
)
Related
First string:
$a = '_edit_last,_edit_lock,wpvp_fp_code,video_category';
second string:
$b = '1,1464965316:1,{"src":"http://localhost/wbg/wp-content/uploads/2016/05/PHP-Tutorial-1-Introduction-PHP-For-Beginners.mp4","splash":"http://localhost/wbg/wp-content/uploads/2016/05/default_image.jpg","width":"640","height":"360"},free 200';
Convert String into combined array.
I need out put for:
array("_edit_last"=>" 1", "_edit_lock"=>"1464965316:1", "wpvp_fp_code"=>"{"src":"http://localhost/wbg/wp-content/uploads/2016/05/PHP-Tutorial-1-Introduction-PHP-For-Beginners.mp4","splash":"http://localhost/wbg/wp-content/uploads/2016/05/default_image.jpg","width":"640","height":"360"}","video_category"=>"free 200");
Use array merges recursive function to merge two arrays
For example
$ar1 = array("color" => array("favorite" => "red"), 5);
$ar2 = array(10, "color" => array("favorite" => "green", "blue"));
$result = array_merge_recursive($ar1, $ar2);
print_r($result);
Array Combine Function:
$a = array('_edit_last', '_edit_lock', 'wpvp_fp_code', 'video_category');
$b = array('1', '1464965316:1', '"{src":"http://localhost/wbg/wp-content/uploads/2016/05/PHP-Tutorial-1-Introduction-PHP-For-Beginners.mp4","splash":"http://localhost/wbg/wp-content/uploads/2016/05/default_image.jpg","width":"640","height":"360"}','free 200');
$c = array_combine($a, $b);
echo "<pre>";
print_r($c);
WATCH DEMO
Convert sting into array
$str = '_edit_last,_edit_lock,wpvp_fp_code,video_category';
print_r (explode(", ",$str));
DEMO
I have used the double space instead of a comma(,) in string2. Because comma(,) is not a unique function
DEMO - 3
Is there any function similar to "array_intersect" but it is in mode case-insensitive and ignoring tildes?
The array_intersect PHP function compares array elements with === so I do not get the expected result.
For example, I want this code :
$array1 = array("a" => "gréen", "red", "blue");
$array2 = array("b" => "green", "yellow", "red");
$result = array_intersect($array1, $array2);
print_r($result);
Outputs gréen and red. In default array_intersect function just red is proposed (normal cause ===).
Any solution ?
Thank you in advance
$result = array_intersect(array_map('strtolower', $array1), array_map('strtolower', $array2));
<?php
function to_lower_and_without_tildes($str,$encoding="UTF-8") {
$str = preg_replace('/&([^;])[^;]*;/',"$1",htmlentities(mb_strtolower($str,$encoding),null,$encoding));
return $str;
}
function compare_function($a,$b) {
return strcmp(to_lower_and_without_tildes($a), to_lower_and_without_tildes($b));
}
$array1 = array("a" => "gréen", "red", "blue");
$array2 = array("b" => "green", "yellow", "red");
$result = array_uintersect($array1, $array2,"compare_function");
print_r($result);
output:
Array
(
[a] => gréen
[0] => red
)
Assume that I have two arrays as follow:
$array1 = array(1, 3, 5);
$array2 = array('x'=> 1, 'y'=> 2, 'z'=> 5);
How to check that the two arrays are exactly the same in most efficient and proper way and it doesn't care the keynames of the *$array2.
I want to make a function which should return true if values are exactly the same, and false if any of the ones are different both in value(s) and number of elements.
Thanks for your time and reading.
In the simplest case you can just use array_diff. It ignores the keys in your second array, but also the order of the values. It would return an empty set if the arrays are equal:
if (count(array_diff($array1, $array2)) == 0) {
// equal
You could also compare the arrays directly, after stripping keys from the second:
if ($array1 == array_values($array2)) {
That would additionally compare the order of contained values.
array_values($array1) === array_values($array2)
Assuming that arrays have same order.
Try this
$array1 = array(1, 3, 5);
$array2 = array('x'=> 1, 'y'=> 2, 'z'=> 5);
$array2 = array_values($array2);
echo $array1 == $array2 ? 'true' : 'false';
array_diff will do the job for you:
<?php
$array1 = array("a" => "green", "red", "blue", "red");
$array2 = array("b" => "green", "yellow", "red");
$result = array_diff($array1, $array2);
if(empty($result)){
// arrays contain the same values!
}
?>
Create a class containing an array and make that class implement the Comparable interface, for example http://php.net/manual/language.oop5.interfaces.php#69467
like this:
<?php
$array1 = array ("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array ("a" => "green", "yellow", "red");
$result = array_diff($array1, $array2);
if(count($result) == 0)
{
.......
}
?>
My array:
$data = array('two' => 2, 'one' => 1, 'three' => 3);
Now, with when I iterate the array, the first value that will come up will probably be
$data['two'] // = 2 # index[0]
right?
What if I want to move the $data[1] to the position of $data[0] ?
To rephrase:
How do I make the array look like this (so that 'one' comes up at $data[0])
$data = array('one' => 1, 'two' => 2, 'three' => 3
Why do I need this?
I use code igniter, the table->generate built-in function takes an assoc array and creates a table but offers no method of arranging the columns. This is why I would like to move the columns in the source array.
Two possible solutions (without using array_splice):
1) Create a new array with the new order of the keys.
$new_keys = array('one', 'two', 'three');
$new_data = array();
foreach ($new_keys as $key) {
$new_data[$key] = $data[$key];
}
$data = $new_data;
2) Move the element one upfront, remove it from $data and copy the rest of the array.
function rearrangeData($data) {
$result['one'] = $data['one'];
unset($data['one']);
return array_merge($result, $data);
}
$data = rearrangeData($data);
Take a look at daniele centamore's comment on PHP's array_splice() function, where he provides a couple of functions for moving the elements in an non-associative array.
<?php
// $input (Array) - the array containing the element
// $index (int) - the index of the element you need to move
function moveUp($input,$index) {
$new_array = $input;
if((count($new_array)>$index) && ($index>0)){
array_splice($new_array, $index-1, 0, $input[$index]);
array_splice($new_array, $index+1, 1);
}
return $new_array;
}
function moveDown($input,$index) {
$new_array = $input;
if(count($new_array)>$index) {
array_splice($new_array, $index+2, 0, $input[$index]);
array_splice($new_array, $index, 1);
}
return $new_array;
}
$input = array("red", "green", "blue", "yellow");
$newinput = moveUp($input, 2);
// $newinput is array("red", "blue", "green", "yellow")
$input = moveDown($newinput, 1);
// $input is array("red", "green", "blue", "yellow")
?>
See ksort and uksort.
Here's a working example:
<?php
$data = array('two' => 2, 'one' => 1, 'three' => 3);
print_r($data);
ksort($data);
echo "ksort:\n";
print_r($data);
uksort($data,'cmp');
echo "uksort:\n";
print_r($data);
function cmp($a, $b)
{
$num=' one two three four five six seven eight nine ten';
$ai = stripos($num,$a);
$bi = stripos($num,$b);
if ($ai>0 && $bi>0) {
return ($ai > $bi) ? 1 : -1;
}
return strcasecmp($a, $b);
}
Output:
Array
(
[two] => 2
[one] => 1
[three] => 3
)
ksort:
Array
(
[one] => 1
[three] => 3
[two] => 2
)
uksort:
Array
(
[one] => 1
[two] => 2
[three] => 3
)
Run this:
http://codepad.org/yAK1b1IP
PHP has 13 functions for sorting arrays, by key, by value, by user-defined functions where you can specify that "one" comes before "two". There's also array_shift, array_unshift, array_push and array_pop for moving things onto or off the front or end of the array. You can build a whole new array from the existing one.
I think, you should use asort function:
$data = array('two' => 2, 'one' => 1, 'three' => 3);
$dataOrdered = $data;
asort($dataOrdered);
Run this code
i want to check if one array is contained in the second array ,
but the same key and the same values,
(not need to be equal, only check that all the key and value in one array is in the second)
the simple thing that i do until now is :
function checkSameValues($a, $b){
foreach($a as $k1 => $v1){
if($v1 && $v1 != $b[$k1]){
return false;
break;
}
}
return true;
}
Is there a simpler(faster) way to check this ?
thanks
I would do
$array1 = array("a" => "green", "b" => "blue", "c" => "white", "d" => "red");
$array2 = array("a" => "green", "b" => "blue", "d" => "red");
$result = array_diff_assoc($array2, $array1);
if (!count($result)) echo "array2 is contained in array";
What about...
$intersect = array_intersect_assoc($a, $b);
if( count($intersect) == count($b) ){
echo "yes, it's inside";
}
else{
echo "no, it's not.";
}
array_intersect_assoc
array_intersect_assoc() returns an array containing all the values of array1 that are present in all the arguments.
function checkSameValues($a, $b){
if ( in_array($a,$b) ) return true;
else return false;
}
This obviously only checks depth=1, but could easily be adapted to be recursive:
// check if $a2 is contained in $a1
function checkSameValues($a1, $a2)
{
foreach($a1 as $element)
{
if($element == $a2) return true;
}
return false;
}
$a1 = array('foo' => 'bar', 'bar' => 'baz');
$a2 = array('el' => 'one', 'another' => $a1);
var_dump(checkSameValues($a2, $a1)); // true