0

I am new to php ,facing some issues between array_push and array_merge

I used two array in input $a(Blank array) and $b(Associative array with some value)

In case of array_merge

when i used array_merge i got same result in both cases ($c and $f) but in put i used an associative array with key "212" and value "aa" but in output i get an array with key "0" and value "aa"

now in case of

array_push i get 2 different outputs (int 1 and int 2)for two different cases for 2 same inputs

I tried this

$a=array();
$b= array(212=>"aa");
$c=array_merge($a,$b);
$f=array_merge($b,$a);
$d=array_push($b,$a);
$e=array_push($a,$b);

now i dumped these array using this

echo '<pre>';
var_dump($c);
var_dump($f);
var_dump($d);
var_dump($e);

and i get these result for every array

array(1) {
  [0]=>
  string(2) "aa"
}
array(1) {
  [0]=>
  string(2) "aa"
}
int(2)
int(1)
Rahul Saini
  • 111
  • 2
  • 11
  • array_push modifies its first parameter and returns the number of new elements. so your experiment is somewhat flawed ;o) – Jakumi Jul 07 '18 at 10:56
  • 1
    @Jakumi what about in case of array_merge() . – Rahul Saini Jul 07 '18 at 11:02
  • look at answer 1 and/or read the manuals carefully, because I believe almost everything is answered already. sorry for the rtfm, but really. – Jakumi Jul 07 '18 at 11:07
  • array_push returns the new number of elements in the array. `var_dump($b);` after your push to see what happened. And likewise for $a after your second push. – Progrock Jul 07 '18 at 13:52
  • array_merge merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. It returns the resulting array. ( From the manual.) When looking in the manual, do check the explanation regarding the return of a function. – Progrock Jul 07 '18 at 13:57

0 Answers0