1
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();
}
Orion Edwards
  • 121,657
  • 64
  • 239
  • 328
Vinh
  • 41
  • 6

5 Answers5

1

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;
nickf
  • 537,072
  • 198
  • 649
  • 721
Jason
  • 2,035
  • 11
  • 13
1

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
  • 7,367
  • 4
  • 22
  • 15
0

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

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

psayre23
  • 2,766
  • 2
  • 16
  • 10
-1

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
  • 537,072
  • 198
  • 649
  • 721
  • This is exactly the same conclusion I have come to as well.. Thank you for your reply – Vinh Nov 05 '08 at 06:45
  • But that's bad advice; you CAN chain together return statements! Just google for "PHP Method Chaining." – MDCore Nov 05 '08 at 06:55
  • try going to codepad.org and putting in: printName(); – Dean Rather Nov 05 '08 at 07:12
  • I don't understand, did no one read what he wrote before voting down? – asleep Nov 05 '08 at 08:59
  • 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 Nov 05 '08 at 12:11
-1

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
  • 24,699
  • 18
  • 71
  • 77