I need to test if one element of an array is in another array.
$array_one = array("gogo", "blabla", "toto");
$array_two = array("stackov", "renaul", "toto");
I would like to know if one element of array_one is in array_two ???
How to test that? Am trying in_array but it seems to have problems.
array_intersect()
$array1 = array("gogo", "blabla", "toto");
$array2 = array("stackov","renaul","toto");
$commonElements = array_intersect($array1,$array2);
var_dump($commonElements);
Try this one:
array_intersect($array_one, $array_two);
Mark's answer should be enough for your problem.
If you ever wish to find the intersect of more than 2 arrays, use this:
$arrays = array(
array(1, 2, 3),
array(2, 4, 6),
array(2, 8, 16)
);
$intersection = call_user_func_array('array_intersect', $arrays);
Related
Can someone help me for my College Exam. I tried to search but Im totally newbie in php and Im still studying
Here's what i want, Can you give me some Idea or function so that I can arrange an array of positive integers to form the largest numerical string?
For Example:
$arrnew = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
assert(getLargestNumStr($arrnew) == 98765432110, 'Basic test 9-8-7-6-5-4-3-2-1-10');
Hope you can help me.
This is a nice problem, this could be solved by this observation:
let's consider two elements in the array x and y, so assume that for the two numbers created by appending these two elements: xy > yx => in the final result, x will always be in front of y, otherwise, we could easily create a larger number by swapping the position of x and y in the result.
=> We could simply create a custom sort based on this observation, when compare two number x and y.
Give a try with below code if it solve your problem...
$array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
$text='';
foreach($array as $arr){
$text.=$arr;
}
$string = str_split($text, "1");
$new_text = implode(",", $string);
$output=explode(",",$new_text);
rsort($output);
$final = '';
foreach($output as $out){
$final.=$out;
}
echo $final;
Try sorting the array in descending order with something like arsort() and concatinate the Array-elements to a string. This should give you the highest possible number.
Try this
$arrnew = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
arsort($arrnew);
$str = str_replace(',','',join(',',$arrnew));
$arr1 = str_split($str);
arsort($arr1);
print_r($arr1);
echo implode('',$arr1);
Demo
Just a note of previous research on SO:
search under "[php] array sort" (15,000 results)
compare two arrays where values are not in same order (does not cleanly address direct issue)
Compare two associative arrays regarding the order of keys (different issue)
At least I believe that:
Given the example:
$arr1 = [21, 23, 25];
$arr2 = [25, 21, 23];
It the easiest, nondestructive manner to compare these for equal values, regardless of order
$arr1s = $arr1;
$arr2s = $arr2;
sort($arr1s, SORT_NUMERIC);
sort($arr2s, SORT_NUMERIC);
$isSameValues = ($arr1s === $arr2s);
Or is there an easier, cleaner way to do this?
Use array_count_values, which is O(n) vs O(n log n) for sorting
$a = [1, 1, 3, 2];
$b = [1, 2, 2, 3];
var_dump (array_count_values($b) == array_count_values($a)); //false
Note: this only works with arrays where all values are strings or ints.
You were very near to the right solution!
Here is your code fixed:
$arr1 = [21, 23, 25];
$arr2 = [25, 21, 23];
$arr1s = $arr1;
$arr2s = $arr2;
sort($arr1s, SORT_NUMERIC);
sort($arr2s, SORT_NUMERIC);
$isSameValues = ($arr1 == $arr2);
See it working here: http://sandbox.onlinephpfunctions.com/code/b737d3a9cf45e077a0d1f2c0195f389b81ace4a3
If the two arrays have same keys and values, a == is enough, as shown here:
I have 2 arrays to compare and find if there is at least a single value in common.
This works just fine:
$arr1 = array(1, 2, 3, 4, 5);
$arr2 = array(2, 3, 4, 5, 6);
if (array_intersect($arr1, $arr2)) {
// good, at least one match found
}
However, the question is performance. It doesn't make sense to continue looping thru the arrays after the first match was found. Is there a native PHP function or a useful snippet to achieve this?
Will a combination of foreach() and in_array() do the trick?
How about this?
foreach ($arr1 as $key => $val) {
if (in_array($val, $arr2)){
// do something, maybe return so you wouldn't need break
break;
}
}
Just compare the first value?
$arr1 = array(1, 2, 3, 4, 5);
$arr2 = array(2, 3, 4, 5, 6);
if (array_intersect($arr1, $arr2)[0]) {
// good, at least one match found
}
How to combine two arrays into single one and i am requesting this in such a way that the 3rd combination array should contains one value from one array and the next one from other array and so on.. or ( it could be random)
ex:
$arr1 = (1, 2, 3, 4, 5);
$arr2 = (10, 20, 30, 40, 50);
and combined array
$arr3 = (1, 10, 2, 20, 3, 30, ...);
If it can be random, this will solve your problem:
$merged = array_merge($arr1, $arr2);
shuffle($merged);
I also made a function for fun that will produce the exact output you had in your question. It will work regardless of the size of the two arrays.
function FosMerge($arr1, $arr2) {
$res=array();
$arr1=array_reverse($arr1);
$arr2=array_reverse($arr2);
foreach ($arr1 as $a1) {
if (count($arr1)==0) {
break;
}
array_push($res, array_pop($arr1));
if (count($arr2)!=0) {
array_push($res, array_pop($arr2));
}
}
return array_merge($res, $arr2);
}
This will return a random array:
$merged = array_merge($arr1,$arr2);
shuffle($merged);
sort($arr3 = array_merge($arr1, $arr2));
array_merge() will merge your arrays into one. sort() will sort the combined array.
If you want it random instead of sorted:
shuffle($arr3 = array_merge($arr1, $arr2));
$arr3 contains the array you're looking for.
You can use
<?php
arr3 = array_merge ($arr1 , $arr2 );
print_r(arr3);
?>
which will output in
$arr3 = (1,2,3,4,5,10,20,30,40,50)
I have an array let say $array = array(2, 1, 8, 3, 6, 0, 10, 10)and I want to get second largest value of that array.
Which sorting/searching technique will be best & how could I use it?
I'd just remove the duplicates from your array (using array_unique) and then use rsort (which uses Quicksort) with the SORT_NUMERIC flag to sort numerically from highest to lowest:
$array = array(2, 1, 8, 3, 6, 0, 10, 10);
$unique_array = array_unique($array);
rsort($unique_array, SORT_NUMERIC);
$second_highest = $unique_array[1]; // will be 8
The PHP manual has a comparison of sorting techniques.
Something like this?
$array = array(2, 1, 8, 3, 6, 0, 10, 10);
rsort($array);
echo $array[1]; // 10
this reverse sorts the array and then outputs the second element.
Edit: if you want the second highest unique value, just add an array_unique call:
$array = array(2, 1, 8, 3, 6, 0, 10, 10);
$array = array_unique($array);
rsort($array);
echo $array[1]; // 8
you could use rsort, but it will do a lot of additional operations which are not needed (quick sort has O(logn) complexity).
It might be a lot faster if you pass the array only in one pass like this (O(n)):
$largest=$array[0];
$secondLargest=null; //none present, by default
foreach($array as $value)
if($value>$largest)
{
$secondLargest=$largest;
$largest=$value;
}
it will be significantly faster on large arrays, but it is only good when you want the first, second, maybe third largest element. In all other cases it would be more reasonable to sort the array and take the N'th largest element dynamically.