0

Following up this question, I have a further problem - I have two same sub keys, but they have different combination of array in their variant key, for instance,

Array
(
    [1] => Array
        (
            [b] => Array
                (
                    [quantity_request] => 1
                    [time_created] => 1339688613
                    [variant] => Array
                        (
                         [0] => x
                         [1] => y
                        )

                )

        )

    [2] => Array
        (
            [b] => Array
                (
                    [quantity_request] => 1
                    [time_created] => 1339688631
                    [variant] => Array
                        (
                         [0] => x
                         [1] => z
                        )

                )

        )

    [3] => Array
        (
            [c] => Array
                (
                    [quantity_request] => 1
                    [time_created] => 1339688959
                    [variant] => Array
                        (
                        )

                )

        )

)

so, how can I find the match of this item,

    [b] => Array
                        (
                            [quantity_request] => 1
                            [time_created] => 1339688631
                            [variant] => Array
                                (
                                 [0] => x
                                 [1] => z
                                )

                        )

    function get_letter($letter,$array)
        {
            foreach($this->content as $k => $v)
            {
                if(array_key_exists($letter, $v))
                {
                    return $k;

                }
            }
            return false;

        }

list($key,$different) = get_letter('b',array("x","z")); // return 1

I want the result like this if there is a match,

2

Any ideas?

Community
  • 1
  • 1
Run
  • 54,938
  • 169
  • 450
  • 748

2 Answers2

0

Arrays are compared value-to-value, so this should work just fine.

$key = array_search($needle, $haystack);

where $needle is the array you want to find the key for.

http://php.net/manual/en/language.operators.comparison.php#language.operators.comparison.types

MDrollette
  • 6,887
  • 1
  • 36
  • 49
0

You just need to add:

if ($v['variant'] == $array)

inside your if statement to compare the variant array with the one passed as an argument.

function get_letter($letter, $array)
{
     foreach($this->content as $k => $v)
     {
          if(array_key_exists($letter, $v))
          {
               if ($v['variant'] == $array)
                   return $k;
          }
     }

     return false;
}
Evan Mulawski
  • 54,662
  • 15
  • 117
  • 144