Solved

Array_Push - Only variables can be passed by reference

Posted on 2013-05-31
3
854 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 52

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 52

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to count occurrences of each item in an array.
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 …

932 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

12 Experts available now in Live!

Get 1:1 Help Now