phparray-push

Why does `array_push` not return the array it modified?


I am facing a strange problem in array_push function of php.

Lets see my code:

$sets_collection=array();
foreach($result['ques'] as $val){
    $sets_collection=array_push($sets_collection,$val['set']);
}

It gives me the error:

Message: array_push() expects parameter 1 to be array, integer given

But when I do this, it works fine:

$sets_collection=array();
$i=0;
foreach($result['ques'] as $val)
{
    $sets_collection[$i]=$val['set']; 
    $i++;
}

Why does this happen? Is it necessary that there should be a index of an array than we can perform the push operation? Because in my first case the array $set_collection does not have an index.


Solution

  • Try this

    $sets_collection=array();
    
    foreach($result['ques'] as $val){
        array_push($sets_collection,$val['set']); 
    }