PHP - 如何在函数返回数组时从数组中获取对象?

时间:2022-12-06 01:41:48

how can I get a object from an array when this array is returned by a function?

当函数返回此数组时,如何从数组中获取对象?

class Item {
    private $contents = array('id' => 1);

    public function getContents() {
        return $contents;
    }
}

$i = new Item();
$id = $i->getContents()['id']; // This is not valid?

//I know this is possible, but I was looking for a 1 line method..
$contents = $i->getContents();
$id = $contents['id'];

3 个解决方案

#1


You should use the 2-line version. Unless you have a compelling reason to squash your code down, there's no reason not to have this intermediate value.

您应该使用2行版本。除非你有令人信服的理由压缩你的代码,否则没有理由没有这个中间值。

However, you could try something like

但是,你可以尝试类似的东西

$id = array_pop($i->getContents())

#2


Keep it at two lines - if you have to access the array again, you'll have it there. Otherwise you'll be calling your function again, which will end up being uglier anyway.

保持两行 - 如果你必须再次访问该阵列,你将在那里。否则你将再次调用你的功能,无论如何最终会变得更加丑陋。

#3


I know this is an old question, but my one line soulution for this would be:

我知道这是一个老问题,但我对此的一线理由是:

PHP >= 5.4

PHP> = 5.4

Your soulution should work with PHP >= 5.4

你的soulution应该使用PHP> = 5.4

$id = $i->getContents()['id'];

PHP < 5.4:

PHP <5.4:

class Item
{
    private $arrContents = array('id' => 1);

    public function getContents()
    {
        return $this->arrContents;
    }

    public function getContent($strKey)
    {
        if (false === array_key_exists($strKey, $this->arrContents)) {
            return null; // maybe throw an exception?
        }

        return $this->arrContents[$strKey];
    }
}

$objItem = new Item();
$intId   = $objItem->getContent('id');

Just write a method to get the value by key.

只需编写一个方法来按键获取值。

Best regards.

#1


You should use the 2-line version. Unless you have a compelling reason to squash your code down, there's no reason not to have this intermediate value.

您应该使用2行版本。除非你有令人信服的理由压缩你的代码,否则没有理由没有这个中间值。

However, you could try something like

但是,你可以尝试类似的东西

$id = array_pop($i->getContents())

#2


Keep it at two lines - if you have to access the array again, you'll have it there. Otherwise you'll be calling your function again, which will end up being uglier anyway.

保持两行 - 如果你必须再次访问该阵列,你将在那里。否则你将再次调用你的功能,无论如何最终会变得更加丑陋。

#3


I know this is an old question, but my one line soulution for this would be:

我知道这是一个老问题,但我对此的一线理由是:

PHP >= 5.4

PHP> = 5.4

Your soulution should work with PHP >= 5.4

你的soulution应该使用PHP> = 5.4

$id = $i->getContents()['id'];

PHP < 5.4:

PHP <5.4:

class Item
{
    private $arrContents = array('id' => 1);

    public function getContents()
    {
        return $this->arrContents;
    }

    public function getContent($strKey)
    {
        if (false === array_key_exists($strKey, $this->arrContents)) {
            return null; // maybe throw an exception?
        }

        return $this->arrContents[$strKey];
    }
}

$objItem = new Item();
$intId   = $objItem->getContent('id');

Just write a method to get the value by key.

只需编写一个方法来按键获取值。

Best regards.