PHP类更新数组 - 如何提高效率

时间:2016-10-07 11:02:56

标签: php arrays class

我有以下代码,我想知道如何编辑它,以便我可以通过一次调用来更新具有多个值的数组:

$var->updateArray("Value 1", "Value2", "Value 3", and so on.... );

我目前的代码是:

<?php
class MyClass {
public $myArray = array();

public function updateArray($newValue) {
    $this->myArray[] = $newValue;
}

public function showArray() {
    foreach($this->myArray as $arrayValue) {
        echo "<li>";
        echo $arrayValue;
        echo "</li>";
    }
}

}

$var = new MyClass;

$var->updateArray("Value 1");
$var->updateArray("Value 2");
$var->updateArray("Value 3");

echo $var->showArray();

?>

3 个答案:

答案 0 :(得分:1)

你可以使用类似的东西:

public function updateArray() {
    if (func_num_args() > 0) {
        $args = func_get_args();
        foreach ($args as $arg) {
            $this->myArray[] = $arg;
        }
    }
}

我没有尝试过代码,因此可能包含错误。

答案 1 :(得分:1)

  

如果您希望将其他参数作为字符串值传递,只需使用func_get_args;否则传递一个值数组并处理它。可以使用此代码段演示第一个Idea:

    class MyClass {
        public $myArray = array();

        public function updateArray($newValue) {
            $arguments  = func_get_args();
            if($arguments) {
                foreach ($arguments as $argument) {
                    $this->myArray[] = $argument;
                }
            }
        }

        public function showArray() {
            foreach($this->myArray as $arrayValue) {
                echo "<li>";
                echo $arrayValue;
                echo "</li>";
            }
        }

    }

    $var = new MyClass();

    $var->updateArray("Value 1", "Value2", "Value 3");
    $var->showArray();
  

第二个想法:传递值数组可以通过以下代码段演示:

    class MyClass {
        public $myArray = array();

        /**
         * UPDATES THE INTERNAL ARRAY USING WHATEVER VALUES PASSED IN AS ARGUMENT(S)
         * IN THIS CASE, YOU COULD PASS AS MANY ARGUMENTS AS YOU WISH AS STRING-VALUES...
         * @param $newValue
         */
        public function updateArray($newValue) {
            $arguments  = func_get_args();
            if($arguments) {
                foreach ($arguments as $argument) {
                    $this->myArray[] = $argument;
                }
            }
        }

        /**
         * UPDATES THE INTERNAL ARRAY USING A COLLECTION: AN ARRAY OF VALUES
         * @param array $collection
         */
        public function updateArrayFromCollection(array $collection) {
            if($collection) {
                foreach ($collection as $value) {
                    $this->myArray[] = $value;
                }
            }
        }

        public function showArray() {
            foreach($this->myArray as $arrayValue) {
                echo "<li>";
                echo $arrayValue;
                echo "</li>";
            }
        }

    }

    $var = new MyClass();

    $var->updateArrayFromCollection(["Value 1", "Value2", "Value 3"]);
    $var->showArray();

答案 2 :(得分:0)

public function showArray() {
    foreach($this->myArray as $arrayValue) {
        $array_collect[] =  $arrayValue; 
    }
}

然后打印

$array = Implode (',',$array_collect);
echo $array; 

根据使用Implode功能安排收集