tags:

views:

324

answers:

6
function array_value_from_key($array,$key)
{
     return !empty($array[$key]) ? $array[$key] : null;
}

The reason I ask is because I have a class function that returns an array.

Instead of having to do

$myArray = myClass::giveMeArray();
$myValue = $myArray[$myKey];

I'd like to do something along the lines of

$myValue = array_value_from_key(myClass::giveMeArray(),$myKey);

When an object is returned, you can chain the object such as

$myValue = myClass::giveMeObject()->aValue;

Voila, nice and clean.. not being able to find what seems to be a simple and trivial function is driving me crazy...

PS.. one more example of how I'd like to use such a function

if(arrayKeyVal(aClass::giveMeArray(),$myKey)) {
    do_something();
}
+1  A: 

I haven't tried it, but:

$myValue = @$myArray[$myKey];

might work, though i honestly think you would be better off using

$myValue = (array_key_exists($myKey, $myArray)) ? $myArray[$myKey] : null;
Jason
sorry, not what i'm looking for, thank you for the reply though.
Vinh
That should be what you're looking for.
eyelidlessness
No, the point is to not temporarily store the array in a variable.
Vinh
A: 

EDIT: turns out I need to brush up on my PHP5. My answer below is incorrect and only applies to PHP4, since PHP5 has method chaining.


PHP doesn't allow you to chain together the return statements from functions like in many other languages. eg, in Javascript:

document.getElementById('abc').style.color = "#fff";

It's a bit of a pain, but that's just how it is. The equivalent which you have to do in PHP is by storing temporary variables. Here's the above as you'd have to write it, if it were PHP: (obviously these aren't real PHP functions)

$myElement = $document->getElementById('abc');
$myElement->style->color = "#fff";

One little positive of this is that it makes debugging a wee bit easier.

Note that this only applies to functions: you can "chain" properties of objects:

$myObject->myArray[5]->anotherProperty->anotherArray[3]->anotherObject->aFunction();

so, in short, if you want to use the result of a function, you have to store it temporarily first

nickf
This is exactly the same conclusion I have come to as well.. Thank you for your reply
Vinh
But that's bad advice; you CAN chain together return statements! Just google for "PHP Method Chaining."
MDCore
try going to codepad.org and putting in:<?phpclass person { function printName() { print 'bob'; } }function getPerson() { return new person(); }getPerson()->printName();
Dean
Guys, that's exactly what he said....
Vinh
I don't understand, did no one read what he wrote before voting down?
Shahin
You can chain calls to the same object by returning the object in each call. Like so:$x = new classX;$x->write("bla")->write("ble")->save();All you have to do to accomplish this is have the write method return the $this keyword
AntonioCS
+1  A: 

You could return an ArrayObject, like so.

<?
class MyClass
{
    public static function getArray()
    {
        $arr = array('dave' => 1, 'bob' => 2, 'james' => 3);
        return new ArrayObject($arr, ArrayObject::ARRAY_AS_PROPS);
    }
}

$var = MyClass::getArray()->bob;

?>
Dave Marshall
Right, but my question is if there is a native php function that already does it.
Vinh
Sorry, just trying to offer a reasonable alternative...
Dave Marshall
A: 

Why don't you change the giveMeArray() function like so:

function giveMeArray($key = false) {
    $array = $whatever;
    $toret = null;
    if ($key) {
        if (array_key_exists($key, $array)) {
            $toret = $array[$key];
        }
    } else {
        $toret = $array()
    }
    return $toret;
}

Then you can call it as

$arr = myClass::giveMeArray(); //To get the whole array
$value = myClass::giveMeArray($myKey); //To get the specific element
Jrgns
A: 

$myValue = ($tmp = myClass::giveMeArray() && $tmp[$myKey]) ? $tmp[$myKey] : null;

It's not pretty, but it'll do it all in one line. ;-)

psayre23
A: 

This questions is similar (but is more specific to the lack of subscript support on expressions): http://stackoverflow.com/questions/13109/php-access-array-value-on-the-fly

Bill Zeller