2

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('lname'  =>  '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
dreftymac
  • 31,404
  • 26
  • 119
  • 182
  • If not, you can always make your own! – Anthony Forloney Jan 29 '10 at 03:23
  • Any function that would exist like that would use a foreach loop anyways, you're far better off writing your own framework using a foreach loop than searching the internet for something that isn't there. – animuson Jan 29 '10 at 03:28
  • I've already done it myself and I am not the only person who has had this idea. The question is to see how other people have done it. – dreftymac Jan 29 '10 at 03:53

3 Answers3

4

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
robbo
  • 234
  • 1
  • 2
  • 1
    see also: http://stackoverflow.com/questions/76328/is-there-a-way-to-emulate-php5s-call-magic-method-in-php4 – dreftymac Jan 29 '10 at 04:11
1

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
Amber
  • 507,862
  • 82
  • 626
  • 550
1
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.

Tyler Carter
  • 60,743
  • 20
  • 130
  • 150