?
Solved

why array_push doesn't work

Posted on 2009-02-20
5
Medium Priority
?
288 Views
Last Modified: 2012-05-06
hello:

why array_push() doesn't work for the following code?

thanks for your reply!
$projLocation=array();	
foreach ($pg[1] as $loc){
$elem= $pg[0][0]." - ".$loc;  // this does print the info. 
$projLocation = array_push($projLocation, $elem);
}
echo "loc=".$projLocation[0]; //this print nothing

Open in new window

0
Comment
Question by:lilyyan
5 Comments
 
LVL 39

Accepted Solution

by:
Roger Baklund earned 1600 total points
ID: 23693195
array_push() changes the array in-place. Instead of this:

$projLocation = array_push($projLocation, $elem);

...just do this:

array_push($projLocation, $elem);

...or this:

$projLocation[] = $elem;

http://php.net/array_push
0
 
LVL 19

Assisted Solution

by:LordOfPorts
LordOfPorts earned 400 total points
ID: 23693223
The array_push function returns the new number of elements in the array, an integer value, so you should not assign it $projLocation as this destroys the array. Line 4 in your code above should be simply:

array_push($projLocation, $elem);
$projLocation=array();  
foreach ($pg[1] as $loc){
    $elem= $pg[0][0]." - ".$loc;  // this does print the info. 
    array_push($projLocation, $elem);
}
echo "loc=".$projLocation[0]; //this print nothing

Open in new window

0
 
LVL 8

Expert Comment

by:agamal
ID: 23693265
try to echo $elem and $projLocation to check value
0
 

Author Comment

by:lilyyan
ID: 23694112
well, only this one worked:(@cxr)
$projLocation[] = $elem;

maybe it's because $projLocation is an empty array?
0
 
LVL 39

Assisted Solution

by:Roger Baklund
Roger Baklund earned 1600 total points
ID: 23695636
It should not matter that $projLocation is empty. This works for me:

$projLocation=array();
$elem = 'test';
array_push($projLocation,$elem);
var_dump($projLocation);   # output: array(1) { [0]=>  string(4) "test" } 

Open in new window

0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
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…
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.
Suggested Courses
Course of the Month15 days, 14 hours left to enroll

850 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