Jump to content

search throuh multidimentional array


jimfog
 Share

Recommended Posts

I have 2 arrays

$_POST['services'][0]);///this is '31'....but it can contain more than one value
$serviceslist);...this is a multidimentional array array(1) { [0]=> array(2) { ["service"]=> string(7) "haircut" ["serviceID"]=> int(31) } }

I tried the following code....but it does not work and the reason being it assumes that $servicelist is one-dimentional array.

 if (is_array($_POST["services"]))
           {

              $i=0;
            while($i<count($_POST['services']))
                { 
                    if ((in_array($_POST['services'][$i],$serviceslist))==false)
                            {
                     $error='nosuchservice';
                    echo json_encode($error);
                    return;
                }
                $i++;
                }
//            
            return;
           }

how can I search in $secviselist for the values contained in $_POST['services'];

The above code shoud output true since I do not use the strict paramaeter...31 exists in both.

What adjustments I have to make?

Link to comment
Share on other sites

You will have to loop through the second array for each element in the first array to compare their values.


$found = false;
foreach($_POST['services'] as $id) {
  foreach($serviceslist as $service) {
    if($service['serviceID'] == $id) {
      $found = true;
      break;
    }
  }
  if($found) break;
}

if(!$found) {
  echo json_encode('nosuchservice');
}

 

Link to comment
Share on other sites

thanks for the answer,,,I just want your opinion in this code below since I wrote it BEFORE you answer.

thanks again

   $i=0;
                           while($i<count($_POST['services'])){
                           if(array_search($_POST['services'][$i], array_column($serviceslist, "serviceID"))===false)
                                   {
                                     echo json_encode('nosuchservice');
                                     return;
                                   }
                                  $i++;  
                                }

 

Link to comment
Share on other sites

That should work, but it could be made more efficient if you store the column values in a variable declared outside of the loop. Is there a reason you replaced in_array() with array_search()?

Link to comment
Share on other sites

  • 3 weeks later...
On 4/15/2021 at 1:38 AM, Ingolme said:

That should work, but it could be made more efficient if you store the column values in a variable declared outside of the loop. Is there a reason you replaced in_array() with array_search()?

in_array does not search in multi-dimentional arrays...I did found some code to do it though but I avoided after all cause was more complex than the code above.

 

Sorry for the late response.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...