Hi excuse my stupid question, but I am having troubles in finding the way to return true or false in a function that go through an array like the following one
Array
(
[0] => Array
(
[0] => 1389
),
[1] => Array
(
[0] => 1389
),
[2] => Array
(
[0] => 14568
),
[3] => Array
(
[0] => 14568
)
)
I should be able to return true
or false
if in the array two equal elements are present or not. For example in the example array I should be able to return true
, as 1389 and 14568 are both present.
On the opposite, if the array is made like this:
Array
(
[0] => Array
(
[0] => 1389
),
[1] => Array
(
[0] => 1389
),
)
the output should then be false
I tried to set the cycle as this, but obviously it doesn't work!
$products_id_array []= array( $product_id); // my array
$count = count($products_id_array);
$products = $products_id_array[0][0];//set the first term of teh array
$i=0;
while ($i<$count) {
if ( $products_ids[$i][0] == $products)
{
$products = $products_ids[$i][0];
$mixed_products = false;
}
else
{
$mixed_products = true;
}
$i++;
}
return $mixed_products;
What am I doing wrong? Thanks!
You can do it using array_diff
& array_intersect
functions:
$arr1 = [[1389], [1389], [14568], [14568]];
$arr2 = [[1389], [1389]];
$arr3 = [3, 3, 4, 4, 5 ];
function checkDoubleDuplicate(array $argArray) {
$iflag = 0;
$arrayUnique = array_unique($argArray, SORT_REGULAR);
foreach ($arrayUnique as $key => $value) {
$occuranceCount = count(array_intersect($argArray, array($value)));
if ($occuranceCount == 2)
$iflag++;
}
if (2 == $iflag)
return TRUE;
else
return FALSE;
}
echo checkDoubleDuplicate($arr3) ? 'yes' : 'no';
The only thing you'd better do is to convert your complicated/redundant array(using array for every single value, thus making memory/code complicated) to a simple form like $arr3
. I've suggested solution with this ToDo in mind
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments