Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Array_Push - Only variables can be passed by reference

Posted on 2013-05-31
3
Medium Priority
?
1,112 Views
Last Modified: 2013-05-31
Im trying to add an array to an existing array using the following code:-
array_push((array)$parameter, array("Type"=>"S", "alias"=>$val->name, "ID"=>$val->orgname, "Table"=>$val->table, "MaxLength"=>$val->length));

Open in new window


But Im getting:-
Fatal error: Only variables can be passed by reference

Initially $parameter is null, and it threw an issue by saying
Warning: array_push() [function.array-push]: First argument should be an array
However this was fixed by putting:-
(array)$parameter

Open in new window


I have tried splitting it into another array and adding it:-
            $tmpArr = array("Type"=>"S", "alias"=>$val->name, "ID"=>$val->orgname, "Table"=>$val->table, "MaxLength"=>$val->length);
            array_push((array)$parameter, $tmpArr);

Open in new window


However this still throws the error:-
Fatal error: Only variables can be passed by reference

I'm stuck for ideas now, anyone else got any clue what Im doing wrong? :-(
0
Comment
Question by:tonelm54
  • 2
3 Comments
 
LVL 60

Accepted Solution

by:
Julian Hansen earned 2000 total points
ID: 39210272
Why are you passing $parameter = null to the array_push

Why not
$parameter = array();
$tmpArr = array("Type"=>"S", "alias"=>$val->name, "ID"=>$val->orgname, "Table"=>$val->table, "MaxLength"=>$val->length);
array_push($parameter, $tmpArr);

Open in new window


The error is quite clear - only variables can be passed by reference - you are casting a var to an array which breaks this.
0
 

Author Comment

by:tonelm54
ID: 39210317
Well doing that gives me the first error again:-
Warning: array_push() [function.array-push]: First argument should be an array

My complete code is:-
<?php
$test = new postForm;
$test->readSQL($mysqli,"SELECT * FROM  `users` LIMIT 0,1;");

class postForm {
  private $parameter;

  function __construct() {
       $parameter = array();
   }
  public function readSQL(&$mysqli, $sqlSelect) {
    $tmpArray = array("Type"=>"S", "alias"=>$val->name, "ID"=>$val->orgname, "Table"=>$val->table, "MaxLength"=>$val->length);
    array_push($parameter, $tmpArray);
  }
}
?>

Open in new window

0
 
LVL 60

Expert Comment

by:Julian Hansen
ID: 39210646
Ok but that is because you have a scoping issue

you have to refer to $parameter like so

$this->parameter otherwise you are essentially creating a new variable called $parameter which is totally separate from the property.
class postForm {
  private $parameter;

  function __construct() {
       $parameter = array();
   }
  public function readSQL(&$mysqli, $sqlSelect) {
    $tmpArray = array("Type"=>"S", "alias"=>$val->name, "ID"=>$val->orgname, "Table"=>$val->table, "MaxLength"=>$val->length);
    array_push($this->parameter, $tmpArray); // Add $this-> to $parameter
  }
}
?>

Open in new window

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question