Jump to content

search throuh multidimentional array


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 post
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 post
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 post
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 post
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 post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...