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
https://forums.phpfreaks.com/topic/235938-array-value-exists/#findComment-1212890
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
https://forums.phpfreaks.com/topic/235938-array-value-exists/#findComment-1212925
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
https://forums.phpfreaks.com/topic/235938-array-value-exists/#findComment-1212952
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
https://forums.phpfreaks.com/topic/235938-array-value-exists/#findComment-1212956
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
https://forums.phpfreaks.com/topic/235938-array-value-exists/#findComment-1212958
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.