Reference PHP array by multiple indexes

前端 未结 4 845
暗喜
暗喜 2021-01-04 21:47

This may be some sort of weird longer shortcut, and please correct me if I\'m mistaken in this train of thought...

I have a matrix of data that looks like:

4条回答
  •  轮回少年
    2021-01-04 22:15

    Try something like this:

    function selectByIdOrURL($array, $data) {
        foreach($array as $row) {
           if($row['unique_id'] == $data || $row['url'] == $data) return $row;
        }
        return NULL;
    }
    
    $array = array(
               array('unique_id' => 5, 'url' => 'http://blah.com'),
               array('unique_id' => 3, 'url' => 'http://somewhere_else.com')
             );
    $found = selectByIdOrURL($array, 5); //array('unique_id' => 5, 'url' => 'http://blah.com')
    $nfound = selectByIdOrURL($array, 10); //NULL
    

提交回复
热议问题