开发者

Declarative access to structured PHP variable without foreach loops

Background

Assume I have the following nested variable in PHP.

$data = Array(
Array('lname'  =>  'Simpson','fname'  =>  'Homer','age'    =>  '35','motto'  =>  '_blank_'),
Array('lname'  =>  'Simpson','fname'  =>  'Marge','age'    =>  '34','motto'  =>  '_blank_'),
Array('lname'  =>  'Flintstone','fname'  =>  'Fred','age'    =>  '33','motto'  =>  '_blank_'),
Array('lna开发者_JS百科me'  =>  'Flintstone','fname'  =>  'Wilma','age'    =>  '29','motto'  =>  '_blank_')
);

Assume also the standard methods for accessing specific values:

print($data[0]['fname']);  // Homer
print($data[1]['age']);    // 34

Question

Is there an existing library or framework that would allow me to easily acess specific values declaratively, without using foreach loops?

$test = $data->get_record_by_fname['Homer']
print $test['age'] //35


If you really wanted to overkill everything, you could try an approach using magical methods!

class Simpsons
{
    protected $_data = array();

    public function __construct(array $data)
    {
        $this->_data = array_map(function ($i) { return (object)$i; }, $data);
    }

    public function __call($method, $args)
    {
        if (count($args) == 0)
            return NULL;

        foreach ($this->_data as $row)
        {
            if (property_exists($row, $method) && $row->$method == $args[0])
            {
                return $row;
            }
        }

        return NULL;
    }
}

Usage:

$p = new Simpsons($data); // Stored in the format provided

var_dump($p->fname('Homer')); // Gets the record with fname = Homer


Is there a particular reason you don't want to use foreach loops? If it's merely for conciseness, you could just declare the function yourself, it's fairly trivial:

function get_record($set, $field, $value) {
    foreach($set as $key => $val) {
        if($val[$field] === $value) return $set[$key];
    }
    return NULL;
}

Then your example would become:

$test = get_record($data, 'fname', 'Homer');
print $test['age']; //35


class SomeClass{

    // Stores the Array of Data
    public $data;

    // Sets up the object. Only accepts arrays
    public function __construct(array $data)
    {
        $this->data = $data;
    }

    // Gets a record based on the key/value pair
    public function getByKey($key, $value)
    {
        foreach($this->data as $array)
        {
                if(is_array($array)
                {
                    if(array_key_exists($key, $array) && $array[$key] == $value)
                    {
                        return $array;
                    }
                 }
        }
    }

}

$array = array( 1 => array("Test" => "Hello"));
$obj = new SomeClass($array);

$record = $obj->getByKey('Test', 'Hello');

This lets you get a record based on what a key/value pair inside the array is. Note, the type hinting in the constructor is PHP 5.3(?)

BTW, No, there is no way to escape the foreach as even internal PHP functions (anything beginning with array_) uses a foreach or some other type of loop. However, if you encapsulate the loop into a class, you don't have to think about it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜