php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #71557 Foreach and pointers with multidimensional arrays
Submitted: 2016-02-09 08:42 UTC Modified: 2016-02-09 09:58 UTC
From: zjoclips at gmail dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: Irrelevant OS: Linux
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: zjoclips at gmail dot com
New email:
PHP Version: OS:

 

 [2016-02-09 08:42 UTC] zjoclips at gmail dot com
Description:
------------
Hi, I opened a wrong thread last time, I didn't see the real bug. These bug happens with foreach and pointer in to declaration of this foreach.
The type of last element of an array was modified from "array" into "&array" and this create the problem when i use another iteraction in this array. I'm not good to explain me but the code can do what I cannot.

Ps. You can see the difference on the last element in the two var_dumps: before and after the foreach with the pointer.

Test script:
---------------
<?php

$array = array(
	array("field1" => "content1.1", "field2" => "content1.2", "field3" => "content1.3", ),
	array( "field1" => "content2.1", "field2" => "content2.2", "field3" => "content2.3", ),
	array( "field1" => "content3.1", "field2" => "content3.2", "field3" => "content3.3", ),
	array( "field1" => "content4.1", "field2" => "content4.2", "field3" => "content4.3", )
);
echo "<pre>";
var_dump($array);
echo "\n";
foreach($array as &$element){
	$element["field2"] = "content modified";
}
var_dump($array);
echo "\n";
foreach($array as $element){
	foreach($element as $cell){
		echo $cell."\t";
	}
	echo "\n";
}
echo "</pre>";

Expected result:
----------------
array(4) {
  [0]=>
  array(3) {
    ["field1"]=>
    string(10) "content1.1"
    ["field2"]=>
    string(10) "content1.2"
    ["field3"]=>
    string(10) "content1.3"
  }
  [1]=>
  array(3) {
    ["field1"]=>
    string(10) "content2.1"
    ["field2"]=>
    string(10) "content2.2"
    ["field3"]=>
    string(10) "content2.3"
  }
  [2]=>
  array(3) {
    ["field1"]=>
    string(10) "content3.1"
    ["field2"]=>
    string(10) "content3.2"
    ["field3"]=>
    string(10) "content3.3"
  }
  [3]=>
  array(3) {
    ["field1"]=>
    string(10) "content4.1"
    ["field2"]=>
    string(10) "content4.2"
    ["field3"]=>
    string(10) "content4.3"
  }
}

array(4) {
  [0]=>
  array(3) {
    ["field1"]=>
    string(10) "content1.1"
    ["field2"]=>
    string(16) "content modified"
    ["field3"]=>
    string(10) "content1.3"
  }
  [1]=>
  array(3) {
    ["field1"]=>
    string(10) "content2.1"
    ["field2"]=>
    string(16) "content modified"
    ["field3"]=>
    string(10) "content2.3"
  }
  [2]=>
  array(3) {
    ["field1"]=>
    string(10) "content3.1"
    ["field2"]=>
    string(16) "content modified"
    ["field3"]=>
    string(10) "content3.3"
  }
  [3]=>
  array(3) {
    ["field1"]=>
    string(10) "content4.1"
    ["field2"]=>
    string(16) "content modified"
    ["field3"]=>
    string(10) "content4.3"
  }
}

content1.1	content modified	content1.3	
content2.1	content modified	content2.3	
content3.1	content modified	content3.3	
content4.1	content modified	content4.3	

Actual result:
--------------
array(4) {
  [0]=>
  array(3) {
    ["field1"]=>
    string(10) "content1.1"
    ["field2"]=>
    string(10) "content1.2"
    ["field3"]=>
    string(10) "content1.3"
  }
  [1]=>
  array(3) {
    ["field1"]=>
    string(10) "content2.1"
    ["field2"]=>
    string(10) "content2.2"
    ["field3"]=>
    string(10) "content2.3"
  }
  [2]=>
  array(3) {
    ["field1"]=>
    string(10) "content3.1"
    ["field2"]=>
    string(10) "content3.2"
    ["field3"]=>
    string(10) "content3.3"
  }
  [3]=>
  array(3) {
    ["field1"]=>
    string(10) "content4.1"
    ["field2"]=>
    string(10) "content4.2"
    ["field3"]=>
    string(10) "content4.3"
  }
}

array(4) {
  [0]=>
  array(3) {
    ["field1"]=>
    string(10) "content1.1"
    ["field2"]=>
    string(16) "content modified"
    ["field3"]=>
    string(10) "content1.3"
  }
  [1]=>
  array(3) {
    ["field1"]=>
    string(10) "content2.1"
    ["field2"]=>
    string(16) "content modified"
    ["field3"]=>
    string(10) "content2.3"
  }
  [2]=>
  array(3) {
    ["field1"]=>
    string(10) "content3.1"
    ["field2"]=>
    string(16) "content modified"
    ["field3"]=>
    string(10) "content3.3"
  }
  [3]=>
  &array(3) {
    ["field1"]=>
    string(10) "content4.1"
    ["field2"]=>
    string(16) "content modified"
    ["field3"]=>
    string(10) "content4.3"
  }
}

content1.1	content modified	content1.3	
content2.1	content modified	content2.3	
content3.1	content modified	content3.3	
content3.1	content modified	content3.3	

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-02-09 08:58 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2016-02-09 08:58 UTC] requinix@php.net
See bug #71458
 [2016-02-09 09:58 UTC] zjoclips at gmail dot com
Thanks, I was in fault again. Anyway good job with this posts
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Jul 02 14:01:36 2025 UTC