2

I have the following code and I was wondering how I can edit this so that I can update the array with multiple values with one call of:

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

My current code at the moment is:

<?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 Answers 3

1

Simply use func_get_args if you wish to pass the other arguments as string values; otherwise pass an Array of values and Process it. The first Idea could be demonstrated with this snippet:

    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();

The second idea: passing an Array of values could be demonstrated by the snippet below:

    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();
Sign up to request clarification or add additional context in comments.

Comments

1

You could use something like that:

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

I didn't try the code so it could contains errors.

1 Comment

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

Then print

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

arrange collection according to use Implode function

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.