Solved

Array_Push - Only variables can be passed by reference

Posted on 2013-05-31
3
1,000 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 58

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 58

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
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.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

632 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