Solved

Array_Push - Only variables can be passed by reference

Posted on 2013-05-31
3
819 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 51

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 51

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
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.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

705 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now