Jump to content

Array value exists


Recommended Posts

I think the OP is wanting to see if there are duplicate values in the array. So, if he is looping through the array and finds that element '2' has a value of 'foo', how would he determine if there is another value 'foo' in the array with a higher index.

 

It would be helpful if you were to explain the process you are trying to accomplish. I can think of a few different solutions, but which one I would go with would be directly tied to what the goal is.

 

But, most likely I would be using array_count_values(): http://php.net/manual/en/function.array-count-values.php

Link to comment
Share on other sites

I think the OP is wanting to see if there are duplicate values in the array. So, if he is looping through the array and finds that element '2' has a value of 'foo', how would he determine if there is another value 'foo' in the array with a higher index.

 

It would be helpful if you were to explain the process you are trying to accomplish. I can think of a few different solutions, but which one I would go with would be directly tied to what the goal is.

 

But, most likely I would be using array_count_values(): http://php.net/manual/en/function.array-count-values.php

 

If it does involve removing duplicate values, array_unique is there and works wonders.

Link to comment
Share on other sites

Basically Here is my array (this only has 2 values this can have lots of values):

Array
(
    [0] => Array
        (
            [thread] => Resource id #14
            [processing] => 1
            [thread_id] => 0
        )

    [1] => Array
        (
            [thread] => Resource id #15
            [processing] => 1
            [thread_id] => 1
        )

)

 

I want to check the above array and return something when the value "processing" of all the arrays equal 0 a.k.a false does that make more sense?

Link to comment
Share on other sites

foreach ($mainArray as $eachArray)
{
      foreach ($eachArray as $key => $val)
      {
               if ($key=="processing" && $val==false)
               {
                        // do action here
                        break 2; // ends foreach loops
               }
       }
}

 

Did that help?

Link to comment
Share on other sites

With that array format, you have no option other than iterating over each of the array values to check if the sub-array has a processing of 0 or 1. However, if this array is coming from a DB query, then you should simply do an array to see if all of the target values have a processing value of 0.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.