Remove the array that is already in another array [duplicate] - php

This question already has answers here:
difference between two arrays
(7 answers)
Closed 7 years ago.
How can i remove the elements of array that is from another array
Here is my Array Alpha ["1","2","4"]
And the Array Beta ["1"]
How can i remove the elements of Array Alpha that already contains in Array Beta
i.e., After removal it will remove the 1 as it is contained in Array Beta and returns only ["2","4"]

By using array_diff:
array_diff($alpha, $beta);
Working example: http://3v4l.org/fZRGD

$a = [1,2,4];
$b = [1];
$a = array_diff($a,$b);
print_r($a);
Yields:
Array ( [1] => 2 [2] => 4 )
And if you don't want to preserve original keys, you take only array of values:
$a = array_values(array_diff($a,$b));
print_r($a);
Then it gives:
Array ( [0] => 2 [1] => 4 )

Related

PHP Array Merge and remove duplicate [duplicate]

This question already has answers here:
Merge two arrays containing objects and remove duplicate values
(7 answers)
Closed 6 months ago.
the below are the array am getting out i want to mearge array and remove duplicats of it and pass too for loop not able too do i just want is remove duplicate and pass too for loop as a unique value
i tried with
print_r(array_merge($uniq_arr));
for($i = 0; $i < count($uniq_arr); $i++) {
$tag = $uniq_arr[$i];
}
Array
(
[0] => diet
[1] => exercise
)
Array
(
[0] => diet
[1] => exercise
)
Array
(
[0] => diet
[1] => exercise
)
Array
(
[0] => water intake
[1] => hygiene
[2] => diet
)
Out put in tag shold be like each unique value
You could just use array_merge() indiscriminately then use array_unique() to remove any duplicate entries.
Here is how you could implement the array_merge, array_unique solution. I am making an assumption that you have 4 arrays like so:
$tag1Array = array('diet','exercise');
$tag2Array = array('diet','exercise');
$tag3Array = array('diet','exercise');
$tag4Array = array('water intake','diet','exercise');
$commontags = array_unique(array_merge($tag1Array,$tag2Array,$tag3Array,$tag4Array));
This should result in a single array of the unique values present in any of the 4 tag arrays.

how to filter elements from an array with respect to another array? [duplicate]

This question already has answers here:
difference between two arrays
(7 answers)
Closed 6 years ago.
array('5','6','3')
array('3','2','1','5','9','0','6')
I need the elements of 2nd array removing the elements matching from the first array. ie.
array('2','1','9','0')
please help.
you can use array_diff
<?php
$array1=array('5','6','3');
$array2=array('3','2','1','5','9','0','6');
$diff = array_diff($array2, $array1);
echo "<pre>";
print_r($diff);
output:
Array
(
[1] => 2
[2] => 1
[4] => 9
[5] => 0
)
You can use the function array_diff in PHP that will compare arrays and returns the difference
$diffarray= array_diff($array2, $array1);

sort in explode variable [duplicate]

This question already has answers here:
PHP Sorting Array ASC
(3 answers)
Closed 6 years ago.
Hello everyone I have a small problem with the php sort I have basically a variable example
$ciao ="4,v#2,f#1,x#22,a"; // Can have other elements
$prova = explode("#",$ciao);
rsort($prova);
echo $prova[0];
but that's out 4,v Instead I would like so 1,x
Use sort() simply.
<?php
$ciao ="4,v#2,f#1,x#22,a"; // Can have other elements
$prova = explode("#",$ciao);
sort($prova);
echo $prova[0]; // Prints 1,x
?>
See it working live
have a look at http://php.net/manual/en/function.sort.php, here you can use second parameter i.e. sort_flags
$ciao ="4,v#2,f#1,x#22,a"; // Can have other elements
$prova = explode("#",$ciao);
sort($prova, SORT_STRING); //SORT_STRING - compare items as strings
print_r($prova);
sort($prova, SORT_NUMERIC); //SORT_NUMERIC - compare items numerically
print_r($prova);
output
Array
(
[0] => 1,x
[1] => 2,f
[2] => 22,a
[3] => 4,v
)
Array
(
[0] => 1,x
[1] => 2,f
[2] => 4,v
[3] => 22,a
)

php sort array keys in multidimensional array [duplicate]

This question already has answers here:
How can I sort arrays and data in PHP?
(14 answers)
Closed 8 years ago.
below is my array
$myarray = Array(
[1] => Array (['mytime']=>1),
[7] => Array(['mytime']=>2),
[2] => Array(['mytime']=>3),
[3] => Array(['mytime']=>4)
);
I want to sort output of this array based on keys...
$myarray = Array(
[1] => Array (['mytime']=>1),
[2] => Array(['mytime']=>3),
[3] => Array(['mytime']=>4),
[7] => Array(['mytime']=>2)
);
I have already tried ksort($myarray) it displays 1
anyways to fix this??
ksort() does this:
ksort($myarray);
Note: sorting functions do not return a new sorted array; they simply sort the array passed, and return true or false. Thus, ksort($myarray) will return 1 when successful, and $myarray will be sorted.
It's very clear if you read the docs: http://php.net/manual/en/function.ksort.php

count element in array? [duplicate]

This question already has answers here:
Working with arrays in php
(4 answers)
Closed 3 years ago.
I have an Array like this (when I print_r). How can I sum all elements in this Array?
In this example, the sum is 0+1=1
Array ( [0] => Array ( [0] => 0 )
[1] => Array ( [0] => 1 ) )
Take a look at the 'array_reduce' function: array_reduce: http://nl.php.net/manual/en/function.array-reduce.php
Taken from the documentation:
function rsum($v, $w)
{
$v += $w;
return $v;
}
$sum = array_reduce($a, "rsum");

Categories