With the following arrays i PHP:
$array1 = array(2,9,7);
$array2 = array(6,8,2);
How would write the following statement in code:
If any value of $array1 matches any single value of $array2, then $match = true;
I imagine this is simple, but I can't figure it out.
if ( count(array_intersect($array1, $array2)) > 0 ) {
$match = true;
}
Related
I have an issue,
I have this array
$items= array('ABC','DEF',GHI');
and have another two arrays
$array1 = array('ABC','DEF',GHI');
$array2 = array('DEF');
$array1 should return TRUE because all elements are in $items
$array2 should return FALSE because 'ABC' and 'GHI' arent in that array
i've tried with array_intersect and array_diff but i cant get it,
$result = array_intersect($items,$array1);
$result = !array_diff($items,$array1);
Could you please help me?
Regards
Mario
To see if the array contains at least all values in $items, just get all values from the array that are also in $items and compare it with $items:
$result = (array_intersect($array1, $items) == $items);
If you just need to compare the arrays:
$result = ($array1 == $items);
But why is this not working for you:
$result = !array_diff($items, $array1);
If you dont want to use array_diff, you can use the multidimensional arrays.
Put your arrays inside another array:
$items1= array(
array(0=>"ABC", 1=> "DEF", 2=>"GHI"),
array(0=>"ABC", 1=> "DEF", 2=>"GHI"),
array(0=>"DEF"),
);
Check them with conditional statements:
if($items1[0]==$items1[1]){
echo "true";
}if($items1[1]==$items1[2]){
echo "true 2";
}else{
echo "false";
}
I am trying to create an array that will hold the only array that contains values.
Code below works well, but I get trouble if for ex, $array2 (but can array1 or array3) doesn't contain any value. In that case, I need to merge only array1 and array3.
$array3 = array_filter( array_map( function( $term ) {
if ( ! $term = \Softing\Term::get( $term ) ) {
return false;
}
return [
'link' => $term->get_link(),
'name' => $term->get_name(),
'color' => $term->get_color(),
];
}, $terms ) );
$formatted_terms[] = array_merge($array1, $array2, $array3);
Each if three arrays are formed on the same way as $array3, but some of them could be empty, no values. Those Arrays I dont want to merge. Want to create array only from arrays that holds value.
What is the easiest way to accomplish this.
I tried using
$formatted_terms[] = array_merge((array)$array1, (array)$array2, (array)$array3);
Any advice ?
You can use array_filter() to remove empty array values. Since you have a multidimensional array, you may consider using array_map() in conjunction with array_filter().
Take the following for example:
$array1 = ['link'=>'foo', 'name'=>'bar', 'filter'=>'hello world'];
$array2 = false;
$array3 = ['link'=>'foo', 'name'=>'bar', 'filter'=>'hello world'];
$formatted_terms[] = array_merge((array)$array1, (array)$array2, (array)$array3);
$formatted_terms = array_map('array_filter',$formatted_terms);
echo '<pre>',print_r($formatted_terms),'</pre>';
https://www.php.net/manual/en/function.array-map.php
https://www.php.net/manual/en/function.array-filter.php
Just check if theyre an array and if theyre not empty. If so, use array merge. If not, just do nothing.
$a_all_arrays = array($array1, $array2, $array3);
$a_merged = array();
foreach($a_all_arrays as $arr)
{
if(is_array($arr) && !is_empty($arr))
{
$a_merged = array_merge($a_merged, $arr);
}
}
Check the array if it has values before merging,
$array1=!empty($array1) && is_array($array1)?$array1:[];
$array2=!empty($array2) && is_array($array2)?$array2:[];
$array3=!empty($array3) && is_array($array3)?$array3:[];
$formatted_terms[] = array_merge($array1, $array2, $array3);
I know there is array_intersect in php but what I want is to show like elements in two arrays.
For example:
array1 = [product, php]
array2 = [product management, html]
i want the resultant array to show result = [product managemenet]
array_intersect will give empty result here.
$result=array();
$array1 = array('product','php');
$array2 = array('product management','html');
$srcharray = $array2;
foreach($array1 as $serchstr)
{
foreach($srcharray as $key => $serchval)
{
if (strpos($serchval, $serchstr) !== false) {
$result[] = $serchval;
unset($srcharray[$key]);
}
}
}
var_dump($result);
NOTE : If you need case sensitive matching.. use 'stripos' instead of 'strpos'.
Trying to merge 4 arrays, but some may be empty at certain times.
$array_1 = array('something1', something2);
$array_2 = array('something3', something4);
$array_3 = array();
$array_4 = array('something1', something2);
$list = array_merge($array_1,$array_2,$array_3,$array_4);
print_r($list);
But if one of the arrays are empty, there will be an error.
I've been googling forever, but I can't find a solid simple answer on how to check for empty arrays before merging.
Argument #2 is not an array
Or whichever array is empty is the argument number. How do I strip out empty arrays before merging?
There is NO error with an empty array. There is only an error if the arg is NOT an array.
You could check is_array() or:
$list = array_merge(
(array)$array_1,
(array)$array_2,
(array)$array_3,
(array)$array_4
);
Okay here you go, this should do the trick (if you make array of the initial arrays):
$arrs = array();
$arrs[] = array('something1', something2);
$arrs[] = array('something3', something4);
$arrs[] = array();
$arrs[] = array('something1', something2);
$list = array();
foreach($arrs as $arr) {
if(is_array($arr)) {
$list = array_merge($list, $arr);
}
}
print_r($list);
Array merge supports empty array()
Doc:
Example #3 Simple array_merge() example
http://us1.php.net/array_merge
<?php
$array1 = array();
$array2 = array(1 => "data");
$result = array_merge($array1, $array2);
?>
Result
Array
(
[0] => data
)
You are getting notice because something2, something4 should be quoted as string or $ as variable.
PHP Notice: Use of undefined constant something2 - assumed 'something2'
I have two arrays:
$A = array('a','b','c','d')
$c = array('b','c','e','f')
I want to get a new array containing items not in array $A. So it would be:
$result = array('e','f');
because 'e' and 'f' are not in $A.
Use array_diff
print_r(array_diff($c, $A)); returns
Array
(
[2] => e
[3] => f
)
Use array_diff for this task. As somewhat annoying it does not return all the differences between the two arrays. Only the elements from the first array passed which are not found in any other array passed as argument.
$array1 = array('a','b','c','d');
$array2 = array('b','c','e','f');
$result = array_diff($array2, $array1);
array_diff()
Pseduo Code for General Implementation
Disclaimer: Not familiar with PHP, other answers indicate there are a lot quicker ways of doing this :)
Loop through your first array:
// Array of results
array results[];
// Loop through all chars in first array
for i = 0; i < A.size; i++
{
// Have we found it in second array yet?
bool matched = false;
// Loop each character in 2nd array
for j = 0; j < C.size; j++
{
// If they match, exit the loop
if A[i] == C[J] then
matched = true;
exit for;
}
// If we have a match add it to results
if matches == true then results.add(A[i])
}