2

Possible Duplicate:
Any way to access array directly after method call?

In C# and other languages, I can do something like this

$value = $obj->getArray()[0];

But not in PHP. Any workarounds or am I doomed to do this all the time?

$array = $obj->getArray();
$value = $array[0];
Community
  • 1
  • 1
Extrakun
  • 19,057
  • 21
  • 82
  • 129

6 Answers6

1

If this is a one-off or occasional thing where the situation in your example holds true, and you're retrieving the first element of the return array, you can use:

$value = array_shift($obj->getArray());

If this is a pervasive need and you often need to retrieve elements other than the first (or last, for which you can use array_pop()), then I'd arrange to have a utility function available like so:

function elementOf($array, $index = 0) {
    return $array[$index];
}
$value = elementOf($obj->getArray());
$otherValue = elementOf($obj->getArray(), 2);
chaos
  • 122,029
  • 33
  • 303
  • 309
1

No, you can't do it without using array_shift (Which only gets the first element). If you want to access the third or fourth, most likely you'd want to do a function like this:

function elemnt($array, $element)
{
    return $array[$element];
}
$value = element($obj->getArray(), 4);

Also, see this question, as it is an exact duplicate: Any way to access array directly after method call?

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

I think you are doomed to do it that way :(

AntonioCS
  • 8,335
  • 18
  • 63
  • 92
1

You can do this:

$res = array_pop(array_slice(somefunc(1), $i, 1));
Kuroki Kaze
  • 8,161
  • 4
  • 36
  • 48
1

Well, maybe this could help you, in php spl, pretty usefull, you can make you a Special Array for you:

<?php

class OArray extends ArrayObject{   
    public function __set($name, $val) {
        $this[$name] = $val;
    }
    public function __get($name) {
        return $this[$name];
    }
}

class O{

    function __construct(){
        $this->array = new OArray();
        $this->array[] = 1;
        $this->array[] = 2;
        $this->array[] = 3;
        $this->array[] = 4;
    }

    function getArray(){
        return $this->array;
    }

}
$o = new O();
var_dump( $o->getArray()->{1} );
elcuervo
  • 49
  • 3
0
<?php
class MyArray implements Iterator ,ArrayAccess
{
    private $var = array();
     //--    ArrayAccess                        
    public function offsetSet($offset, $value) {
        if (is_null($offset)) {
            $this->var[] = $value;
        } else {
            $this->var[$offset] = $value;
        }
    }
    public function offsetExists($offset) {
        return isset($this->var[$offset]);
    }
    public function offsetUnset($offset) {
        unset($this->var[$offset]);
    }
    public function offsetGet($offset) {
        return isset($this->var[$offset]) ? $this->var[$offset] : null;
    }//--     Iterator
    public function __construct($array){
        if (is_array($array)) {
            $this->var = $array;
        }
    }
    public function rewind() {      
        reset($this->var);
    }
    public function current() {      
        return current($this->var);
    }
    public function key() {       
        return key($this->var);
    }
    public function next() {        
        return next($this->var);
    }
    public function valid() {     
        return ($this->current() !== false);
    }
}

$values = array(
            "one"   => 1,
            "two"   => 2,
            "three" => 3,
        );
$it = new MyArray($values);

foreach ($it as $a => $b) {
    print "$a: $b<br>";
}     
echo $it["one"]."<br>";?>
Tito100
  • 1,660
  • 1
  • 12
  • 12