Thomas Williams Thomas Williams - 13 days ago 8
PHP Question

array_merge is returning empty array when combining 2d arrays

I am attempting to use array_merge to merge two 2d arrays, but the result is always empty. There is no need to show you the whole php class, but I will paste the relevent code here. The problem is that when I use array_merge I get an empty array. I am calling my function called charts which in turn gets 2 arrays by calling 2 functions, then I wish to merge these arrays. The array functions do return a valid array, but when I merge them, I get nothing.

public function charts(){
$sales=$this->salesChart();
$expenses=$this->$this->expensesChart();

$result = array_merge($sales,$expenses);
print_r($result);
return $result;
}

private function salesChart(){
$salesArray = array(
array('2016-05', 14690),array('2016-06', 2785),array('2016-07', 14690),array('2016-08',23345),array('2016-09', 10345),array('2016-10',12456)
);
return $salesArray;
}

private function expensesChart(){
$expensesArray = array(
array('2016-05', 14690),array('2016-06', 2785),array('2016-07', 14690),array('2016-08',23345),array('2016-09', 10345),array('2016-10',12456)
);
return $expensesArray;
}

Dez Dez
Answer

You have to change this line

$expenses=$this->$this->expensesChart();

for

$expenses=$this->expensesChart();

because I tested the array_merge and it works. It is what it seems for the code you have provided.

Comments