Avatar of nzrubin
nzrubinFlag for New Zealand

asked on 

check if all items are in array php

hi guys
i want to check if one array contain all the items as second one has
$array_1 = "1,4,2,5";
$array_2 = "4,5,2";
if ($array_2 == $array_1)
 {YES}
else
{NO}

thanks a lot!

[[ phpBB zone removed by PenguinMod, EE Moderator, per Q_23571807.html ]]
PHP

Avatar of undefined
Last Comment
nzrubin
Avatar of compfixer101
compfixer101
Flag of United States of America image

first, this has nothing to do with phpbb that i can tell. (i'll get a mod to remove it)

second, for being a premium service member this is a low amount of points..(the higher the point value the quicker and more experts will come.)

third, your question doesn't explain what you want.. so please explain it more..
Avatar of nzrubin
nzrubin
Flag of New Zealand image

ASKER

sorry about phpbb
(i thought easier the question less i put points.....)

ill try to explain more: (you right)
i have two arrays for example
$array_1 = "1,4,2,5";
$array_2 = "4,5,2";
i want to compare two arrays to check if they have the same amount of values and the same values (even if they not in a right order). (values are always integers)
for example if arrays looks like below
$array_1 = "1,4,2,5";
$array_2 = "4,5,2,1";

if ($array_1 == $array_2)
{ echo "Done";}


Avatar of asafadis
asafadis
Flag of United States of America image

Try this:
<?php
    function isArrayInArray($searchFor, $searchIn) {
        foreach($searchFor as $key) {
            $count += in_array($key,$searchIn);
        }
 
        return $count==sizeof($searchFor);
    }
?>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of silveuk
silveuk

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of nzrubin
nzrubin
Flag of New Zealand image

ASKER

it works with very minor adjustment

for ($i=0;$i<=count($array1);$i++)
{
   if(!$array1[$i]==$array2[$i])
   {
    echo  arrays dotn match;
     break;
   }
}
thanks
PHP
PHP

PHP is a widely-used server-side scripting language especially suited for web development, powering tens of millions of sites from Facebook to personal WordPress blogs. PHP is often paired with the MySQL relational database, but includes support for most other mainstream databases. By utilizing different Server APIs, PHP can work on many different web servers as a server-side scripting language.

125K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo