0
class Test
{
  public $data = array();

  public function addData($data = array())
  {
    array_merge($data, $this->data);
    return $this;
  }

  public function showData()
  {
    print_r($this->data);
  }
}
$test = new Test;
$test->addData(array("halo", "zaki"))->showData();

i tried to merging 2 array, but it doesn't work, maybe someone can explain to me?

2 Answers 2

2

array_merge does not modify the arrays passed to it, but rather returns the result.

Try this:

public function addData($data = array())
{
    $this->data = array_merge($data, $this->data);
    return $this;
}
Sign up to request clarification or add additional context in comments.

Comments

0

You forgot to assign the resulting array to member variable $data. It should be,

$this->data = array_merge($data, $this->data);

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.