English 中文(简体)
如何从一行的物体中打印阵列价值?
原标题:How to print array value from object in one line?

如果我有以下几类:

class Test {
    private $field = array();

    function __construct($field) {
        $this->field = $field;
    }

    public function setField($field) {
        $this->field = $field;
    }
    public function getField() {
        return $this->field;
    }
}

我举了一个例子:

$simpleArray = (1, 2, 3, 4);
$simpleTest = new Test($simpleArray);

那么,我怎么能够把简单的阿雷拉的数值一字打印出来? 我知道这一解决办法:

$saveArray = $simpleTest->getField();
echo $saveArray[2];

我想知道,如何在获得实地服务后直接获得阵列价值,因此我不必挽救阵列以变数:

echo $simpleTest->getField()->....?
最佳回答

你可以增加一个参数,作为返回项目的一个指数,如下文所示。 添加另一个称为<代码>fieldAt($index)的方法可能是另一种解决办法。

public function getField($index = null) {
    if($index != null)
    {
        return $this->field[$index];
    }
    return $this->field;
}
$saveArray = $simpleTest->getField(2); // get the 3rd element in the array

另一种解决办法是实施<代码>ArrayAccess接口,使您能够使用阵列接入操作器([>):

class Test implements ArrayAccess{
    private $field = array();

    function __construct($field) {
        $this->field = $field;
    }

    public function setField($field) {
        $this->field = $field;
    }
    public function getField() {
        return $this->field;
    }

    public offsetExists($offset)
    {
        return isset($this->field[$offset]);
    }
    public offsetGet($offset)
    {
        if($this->offsetExists($offset))
        {
            return $this->field[$offset];
        }
        return null;
    }
    public void offsetSet($offset, $value) { } // Can implement this method, if desired
    public void offsetUnset($offset ) { } // Can implement this method, if desired
}
$testObj[2]; // get the 3rd element in the array
问题回答

Before PHP 5.4 you can t use dereference and so you can t use $this->$simpleTest->getField()[position]. So, for the moment, you can create a method like this:

public function getElement($index) {
   if ($index >= 0  &&  $index < count($this->field))
      return $this->field[$index];
   else
      return null;
}

之后,你可以打电话

echo $simpleTest->getElement(position);

位置是愤怒的。





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签