-1

How I can load two associative arrays into an index array through two separate functions like this:

 $allData =[];

 function func1(){
  $func1_arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
   array_push($allData, $func1_arr);
}
func1();

function func2(){
  $func2_arr = array('a' => 45, 'b' => 23, 'c' => 88, 'd' => 33, 'e' => 82);
array_push($allData, $func2_arr);
}
func2();

echo json_encode($allData);

I also tried passing the $allData as parameter of func1($allData) and func2($allData) but I am getting empty [ ] in return

$allData =[];

function func1($allData){
  $func1_arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
   array_push($allData, $func1_arr);
}
func1($allData);

function func2($allData){
  $func2_arr = array('a' => 45, 'b' => 23, 'c' => 88, 'd' => 33, 'e' => 82);
array_push($allData, $func2_arr);
}
func2($allData);

echo json_encode($allData);

Update

Here is what I have tried with with return

$allData =[];

function func1($allData){
  $func1_arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
   return array_push($allData, $func1_arr);
}
func1($allData);

function func2($allData){
  $func2_arr = array('a' => 45, 'b' => 23, 'c' => 88, 'd' => 33, 'e' => 82);
 return array_push($allData, $func2_arr);
}
func2($allData);

echo json_encode($allData);

and still getting empty array in result

$allData =[];

function func1($allData){
  $func1_arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
    array_push($allData, $func1_arr);
    return $allData;
}
func1($allData);

function func2($allData){
  $func2_arr = array('a' => 45, 'b' => 23, 'c' => 88, 'd' => 33, 'e' => 82);
  array_push($allData, $func2_arr);
  return $allData;
}
func2($allData);

echo json_encode($allData);
halfer
  • 19,824
  • 17
  • 99
  • 186
Mona Coder
  • 6,212
  • 18
  • 66
  • 128

1 Answers1

0

Ok I got it myself ,I just do not know why some genius! are voting down my question

$allData =[];

function func1(){
global $allData;
  $func1_arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
   array_push($allData, $func1_arr);
}
func1();

function func2(){
global $allData;
  $func2_arr = array('a' => 45, 'b' => 23, 'c' => 88, 'd' => 33, 'e' => 82);
array_push($allData, $func2_arr);
}
func2();

echo json_encode($allData);
Mona Coder
  • 6,212
  • 18
  • 66
  • 128
  • 2
    You really shouldn't use `global` especially if you don't have to. **return** your data, which you first pass to the function! – Rizier123 Sep 18 '15 at 19:42
  • so what is the solution?! what do you mean by `return your data, which you first pass to the function!` thanks – Mona Coder Sep 18 '15 at 19:50
  • I guess you still didn't read: http://stackoverflow.com/questions/32659626/how-to-push-associate-arrays-to-one-2d-array/32659840?noredirect=1#comment53165809_32659626 – Rizier123 Sep 18 '15 at 19:51
  • I read it that's why I pass the `$allData` through function parameters but it didnt work! – Mona Coder Sep 18 '15 at 19:54
  • I also tried the `function func2() use ($allData){}` but getting this error `syntax error, unexpected 'use' (T_USE), expecting ` – Mona Coder Sep 18 '15 at 19:56
  • and about the `return` I am not sure what should I here?! as i said I am uploading an array into another array what should I return here?! – Mona Coder Sep 18 '15 at 19:59
  • Please take a look at update part of question, I tried all of options at the page you referenced! – Mona Coder Sep 18 '15 at 20:04
  • Your last attempt in your question is getting really close. You now only need to assign the return value of the functions to the array again. – Rizier123 Sep 18 '15 at 20:09
  • `You now only need to assign the return value of the functions to the array ` you mean in functions or outside? – Mona Coder Sep 18 '15 at 20:17
  • `$allData = func2($allData);` and the same for the first function – Rizier123 Sep 18 '15 at 20:18
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/90068/discussion-between-rizier123-and-mona-coder). – Rizier123 Sep 18 '15 at 20:18