Solved

accessing array element by reference not working

Posted on 2013-06-15
12
290 Views
Last Modified: 2013-07-08
Hi

I am not a php programmer but am writing a website im php and having a bit of a struggle. I have this code which does not work on my web server. It echoes 0

$booking_stage = 'your_details';

$images = array();
$images['your_vehicle'] = "your_vehicle";
$images['choose_date'] = "choose_date";
$images['your_details'] = "your_details";
$images['confirm'] = "confirm";
$images[$booking_stage] = $images[$booking_stage] . "_active";


foreach ($images as &$img) {
	
    $img = '<img src="img/' . $img . '.jpg"/>';
	
	
}


$menu = implode( "<br>", $images);
echo $menu;

Open in new window


But when i run it on this website it runs fine: http://writecodeonline.com/php/

The issues seems to be with iterating over the array by reference as if i comment out that bit and echo back one of the array elements it is fine. Please could you suggest what I need to look at in the code or the web server

thanks a lot
0
Comment
Question by:andieje
  • 3
  • 2
  • 2
  • +3
12 Comments
 
LVL 29

Expert Comment

by:Randy Downs
ID: 39250461
Do you have php enabled on your server? Try this

<?php


phpinfo();

?>

Open in new window

0
 

Author Comment

by:andieje
ID: 39250503
yes it is enabled. the rest of the website is working
0
 

Author Comment

by:andieje
ID: 39250512
This worked fine but i dont understand why the reference wouldnt work

foreach ($images as $key => $value) {
      $images[$key] = '<img src="img/' . $images[$key] . '.jpg"/>';      
}
0
 
LVL 33

Accepted Solution

by:
Slick812 earned 500 total points
ID: 39250534
I have used the PHP foreach( )  with a & reference and changed all of the "values" in an array without a problem. Since you say that your code works on http://writecodeonline.com/php/   , then the problem is your server version or settings for PHP. I believe that some early versions of PHP can not do the reference thing in foreach, but I have no direct knowledge of that.

not that it matters, but this line is incorrect -
$images[$booking_stage] = $images[$booking_stage] . "_active";

the $images[$booking_stage]  is NOT SET and has No value in it, so $images[$booking_stage] will end up with only  "_active"  in it.
0
 
LVL 29

Expert Comment

by:Randy Downs
ID: 39250548
Check the phpinfo() for your version.
0
 
LVL 58

Expert Comment

by:Gary
ID: 39250811
Is that your real code? You have an & before the $img - it shouldn't be there.

foreach ($images as &$img) {
                    ^ look here

Open in new window

0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39251346
@slick... First line of code snippet: $booking_stage = 'your_details';

There is nothing wrong with PHP.  PHP references are quirky, especially if you're an experienced programmer who uses other languages.

Don't iterate over the array by reference!  Just remove the & from &$img and the code will probably work.  PHP will make a copy of the array and use that.  However if you're using objects, PHP will address the object by reference, not by copy, whether or not you're using the & notation.  Sorry - it's a PHP "tradition" that is an artifact of the way PHP was written long ago and we can't change it now.

If you want some good learning resources for PHP, this article lists some trustworthy places.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html
0
 
LVL 58

Expert Comment

by:Gary
ID: 39251362
Once you've removed the & then the question is what are you doing here
foreach ($images as &$img) {
    $img = '<img src="img/' . $img . '.jpg"/>';
}


All that is happening is you are assigning $img to each element of the array but on each pass you are changing it to the current element - the previous value is removed
If you want to append the images to $img then you use
$img .= '<img src="img/' . $img . '.jpg"/>';

Notice the .= this appends to the variables

Additionally
$images[$booking_stage]
does not exist so can not add it to a string

Also you are using assigning $img as the temp var to hold the array elements then you are using it in your loop to hold the img string.
0
 
LVL 58

Expert Comment

by:Gary
ID: 39251375
Here's your code with all errors fixed except $images[$booking_stage] as I don't know what you are trying to do here.

<?php
$booking_stage = 'your_details';

$images = array();
$images['your_vehicle'] = "your_vehicle";
$images['choose_date'] = "choose_date";
$images['your_details'] = "your_details";
$images['confirm'] = "confirm";

// Problem here.
//$images[$booking_stage] = $images[$booking_stage] . "_active";

$imgTemp ="";
foreach ($images as $img) {
    $imgTemp .= '<img src="img/'.$img.'.jpg">';
}

echo $imgTemp;

Open in new window

0
 
LVL 33

Expert Comment

by:Slick812
ID: 39251495
@  GaryC123 , good reference and Fix code, however you forgot the "<br>".

@  andieje,  I do not agree wid Ray at all about his - "Don't iterate over the array by reference!  Just remove the & from &$img and the code will probably work".
I use references wid the  & all the  time, even in foreach loops, and they work for me, and are extremely useful to me.
Since you do not really ask about how to fix your code, as you found a solution yourself, but WHY the reference in foreach does not work. consider,  if your PHP version is newer than 5.0 , then it should work, if your PHP version is a 4 , then very many things in the current manual may not work. No one here has offered a SERVER php setting change to fix your problem, and there may not be one. So if you can change your web host to one with a betteror more recent PHP, that would fix the foreach reference problem.
0
 
LVL 34

Expert Comment

by:gr8gonzo
ID: 39251569
I don't have a lot of time to go into details, but when I update arrays, I just use the index:

$booking_stage = 'your_details';

$images = array();
$images['your_vehicle'] = "your_vehicle";
$images['choose_date'] = "choose_date";
$images['your_details'] = "your_details";
$images['confirm'] = "confirm";
$images[$booking_stage] = $images[$booking_stage] . "_active";


foreach ($images as $index => $img) {
	
    $images[$index] = '<img src="img/' . $img . '.jpg"/>';
	
	
}


$menu = implode( "<br>", $images);
echo $menu;

Open in new window

0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39251687
And when you're not sure what your variables contain (as is often the case when you're not sure what the programming means) there is var_dump()
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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…

757 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

22 Experts available now in Live!

Get 1:1 Help Now