Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

accessing array element by reference not working

Posted on 2013-06-15
12
Medium Priority
?
319 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 30

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
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!

 
LVL 34

Accepted Solution

by:
Slick812 earned 2000 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 30

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
 
LVL 111

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 34

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 35

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 111

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

Industry Leaders: 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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
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.
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.
Suggested Courses

824 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