Search for values in nested array

后端 未结 6 1225
滥情空心
滥情空心 2020-12-21 01:07

I have an array as follows

array(2) {
  [\"operator\"] => array(2) {
    [\"qty\"] => int(2)
    [\"id\"] => int(251)
  }
  [\"accessory209\"] =>         


        
相关标签:
6条回答
  • 2020-12-21 01:12
    $map = array();
    foreach ($arr as $v) {
        $map[$v['id']] = 1;
    }
    //then you can just do this as when needed
    $exists = isset($map[211]);
    

    Or if you need the data associated with it

    $map = array();
    foreach ($arr as $k => $v) {
        $map[$v['id']][$k] = $v;
    }
    print_r($map[211]);
    
    0 讨论(0)
  • 2020-12-21 01:13

    This function is useful in_array(211, $array['accessory']); It verifies the whole specified array to see if your value exists in there and returns true.

    in_array

    0 讨论(0)
  • 2020-12-21 01:25

    You can use

    Arr::getNestedElement($array, $keys, $default = null)
    

    from this library to get value from multidimensional array using keys specified like 'key1.key2.key3' or ['key1', 'key2', 'key3'] and fallback to default value if no element was found. Using your example it will look like:

    if (Arr::getNestedElement($array, 'accessory.id') == 211)
    
    0 讨论(0)
  • 2020-12-21 01:30
    <?php
    
    //PHP 5.3 way to do it
    
    function searchNestedArray(array $array, $search, $mode = 'value') {
    
        foreach (new RecursiveIteratorIterator(new RecursiveArrayIterator($array)) as $key => $value) {
            if ($search === ${${"mode"}})
                return true;
        }
        return false;
    }
    
    $data = array(
        array('abc', 'ffffd'),
        'ccc',
        'bbb',
        array('aaa', array('yyy', 'mp' => 555))
    );
    
    var_dump(searchNestedArray($data, 555));
    
    0 讨论(0)
  • 2020-12-21 01:31

    I used a static method because i needed it in a class, but if you want you can use it as a simple function.

    /**
     * Given an array like this
     * array(
     *   'id' => "first",
     *   'title' => "First Toolbar",
     *   'class' => "col-md-12",
     *   'items' => array(
     *       array(
     *           'tipo' => "clientLink",
     *           'id' => "clientLinkInsert"
     *       )
     *   )
     * )
     *
     * and array search like this
     * array("items", 0, "id")
     * 
     * Find the path described by the array search, in the original array
     *
     * @param array $array source array
     * @param array $search the path to the item es. ["items", 0, "tipo"]
     * @param null|mixed $defaultIfNotFound the default value if the value is not found
     *
     * @return mixed
     */
    public static function getNestedKey($array, $search, $defaultIfNotFound = null)
    {
        if( count($search) == 0 ) return $defaultIfNotFound;
        else{
            $firstElementSearch = self::array_kshift($search);
    
            if (array_key_exists($firstElementSearch, $array)) {
                if( count($search) == 0 )
                    return $array[$firstElementSearch];
                else
                    return self::getNestedKey($array[$firstElementSearch], $search, $defaultIfNotFound);
            }else{
                return $defaultIfNotFound;
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-21 01:37

    Hey dardub, you can use array_walk to verify if a particular value is within your array - array_walk iterates through al elements of you array and applys a provided function to them, so basically you would need to create that function. It is used as follows:

    $arr = array(
        'one' => array('id' => 1),
        'two' => array('id' => 2),
        'three' => array('id' => 3)
    );
    
    function checkValue($value, $key)
    {
        echo $value['id'];
    }
    
    array_walk($arr, 'checkValue');
    

    You'll just need to add to your function whatever conditionals/validations you'd need.

    Hope it helps.

    M.

    EDIT: PHP docs on array_walk http://www.php.net/manual/en/function.array-walk.php

    0 讨论(0)
提交回复
热议问题