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.
array_push, see its return value and read the examples.$sets_collection[]=$val['set']is same asarray_push($sets_collection,$val['set']),and array_push returns new array count. After 1st iteration you will see the error in case 1.