1

I want to read data from a data base, and put it in multi-dimensional array. I don't know number of rows that will come from data base, and when i try to add new row to the multi-dimensional array i have the following error

Warning: array_push() [function.array-push]: First argument should be an array in C:\AppServ\www\web\commands\changeservice.php on line 101

and this is the code i have

function preparenewservices()
    {

                $managername = $_SESSION['managername'];

                $sqls = "select s.*,m.* from rm_allowedmanagers m inner join rm_services s on s.srvid = m.srvid where m.managername = '$managername' ";


                $sql = mysql_query($sqls);


                 $newservices =  array();


                    while($row = mysql_fetch_array($sql))
                  {
                        $nsrvid = $row['srvid'];
                        $nsrvname = $row['srvname'];                        
                        $nunitprice = $row['unitprice'];
                        $nunitpricetax = $row['unitpricetax'];

                        $ntotal = $nunitprice + $nunitpricetax;

                        $newservice = array($nsrvid, $nsrvname , $ntotal); 

                        array_push ($newservices[count($newservices)], $newservice);

               }

    }
0

4 Answers 4

1

try this:

array_push ($newservices, $newservice);

instead of:

array_push ($newservices[count($newservices)], $newservice);

because now you pass to the first argument of array_push an integer value not an array

Sign up to request clarification or add additional context in comments.

Comments

0

Thats because $newservices[count($newservices)] is not an array

Read docs here array_push

Just to fix the error you can do this

$newservices[count($newservices)] = array();
array_push ($newservices[count($newservices)], $newservice);

1 Comment

@str agreed. It will clarify user about the error and he can change the code according to his requirement.
0

Try to replace this code

array_push ($newservices[count($newservices)], $newservice);

with

$newservices[count($newservices)] = $newservice;

1 Comment

Arrays are zero-index, there is no need to increment it by one.
0

The simplest way of adding an element/array to another array is this:

$newservices[] = $newservice;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.