Solved

Array_Push - Only variables can be passed by reference

Posted on 2013-05-31
3
876 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 54

Accepted Solution

by:
Julian Hansen earned 500 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 54

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PHP and google maps 13 44
How can I make this form submit to itself? 10 26
Which userID to use for shopping cart application 13 19
How do I fix this UPDATE error? 7 20
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo‚Ķ
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

776 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