Search code examples
phparray-push

array_push is couting the value instead of adding


When I insert the value to the array its counting the value instaed of adding.

I have the below array named $staff_group_arr

Array ( [0] => All Rights User [1] => Operation [2] => Supritendent [3] => Admin Department [4] => Accounts [5] => Transportation )

I am donig like this

$glo_staff_array = array_push($staff_group_arr, "Global", "Managers");

print_r($glo_staff_array );

Result is coming as 8

Please let me know where is the fault.


Solution

  • Read the documentation:

    Description

    int array_push ( array &$array , mixed $value1 [, mixed $... ] )

    Return Values

    Returns the new number of elements in the array.

    The ampersand, &, is the reference operator. This means that the function will modify the parameter, as opposed to returning a modified version of the parameter:

    array_push($staff_group_arr, "Global", "Managers");
    print_r($staff_group_arr);
    

    You can also append stuff the simple way:

    $staff_group_arr[] = 'Global';
    $staff_group_arr[] = 'Managers';
    print_r($staff_group_arr);