这个数组的所有元素都是一样的,但为什么呢?

时间:2014-08-01 22:33:37

标签: php arrays

我有一些循环遍历数组的代码,以及该数组中的数组,并创建所有项的列表。输出的列表将用于制作菜单。

数据在添加到$new_items之前,回显为:

Lane
Financial Literacy
IDA Required

但是,在foreach结束时,如果我转储$new_items,我预计它会是相同的。但事实并非如此:

IDA Required
IDA Required
IDA Required

似乎$new_items数组正在添加每个项目,但是这些值被覆盖了最后一个要添加的元素。

变量在添加到$new_items之前显示正确但它们在哪里更改?

这是我的代码:

$new_items = array();

foreach($classes as $loc) {
  // Clone the original item, change the given values so they suit our class
  $loc_item = $menu_items[$key];

  $loc_item->ID = new_class_menu_id();
  $loc_item->url = site_url( sprintf('/?post_type=product&class-location=%s', urlencode($loc['term']->slug)) );
  $loc_item->title = $loc['term']->name . ' <span class="class-count">'. $loc['class_count'] .'</span>';
  // $loc_item->menu_item_parent // Keep the original menu parent for locations.

  echo "Location Name: {$loc_item->title}\n"; // #### DEBUG #####
  $new_items[] = $loc_item;

  foreach($loc['categories'] as $cat) {
    $cat_item = $menu_items[$key];

    $cat_item->ID = new_class_menu_id();
    $cat_item->url = site_url( sprintf('/?post_type=product&class-location=%s&product_cat=%s', urlencode($loc['term']->slug), urlencode($cat['term']->slug)) );
    $cat_item->title = $cat['term']->name . ' <span class="class-count">'. $cat['class_count'] .'</span>';
    $cat_item->menu_item_parent = $loc_item->ID; // Categories should be nested under their location

    echo "Category Name: {$cat_item->title}\n"; // #### DEBUG #####
    $new_items[] = $cat_item;
  }

  echo "New_Items: \n"; // #### DEBUG #####
  foreach($new_items as $i => $item) echo "Item {$i}: {$item->title}\n"; // #### DEBUG #####
  echo "\n\n\n"; // #### DEBUG #####
}
function new_class_menu_id() {
  static $i = 1000;
  $i++;
  return $i;
}

调试输出:

Location Name: Lane <span class="class-count">1</span>
Category Name: Financial Literacy <span class="class-count">1</span>
Category Name: IDA Required <span class="class-count">1</span>
New_Items: 
Item 0: IDA Required <span class="class-count">1</span>
Item 1: IDA Required <span class="class-count">1</span>
Item 2: IDA Required <span class="class-count">1</span>

预期产出:

Item 0: Lane <span class="class-count">1</span>
Item 1: Financial Literacy <span class="class-count">1</span>
Item 2: IDA Required <span class="class-count">1</span>

2 个答案:

答案 0 :(得分:1)

答案很简单 - 因为你只处理一个对象($ menu_items [$ key])

看一下我的例子来理解:

 $items = array();
 $object = new stdClass();

 $object->title = 'test1';

 $items[] = $object;
 var_dump($items);  // title is 'test1'

 $object->title = 'test2';
 var_dump($items); // now title is 'test2'

答案 1 :(得分:1)

您没有克隆您的对象,您正在传递引用并覆盖类成员。例如,如果您尝试此代码:

<?
class SomeClass {
 public $ID;
 public $url;
 public $title;

 function __construct() {
  $this->ID = 1;
  $this->url = "www.stackoverflow.com";
  $this->title = "MyTitle";
 }
}

$key=0;
$menu_items[$key] = new SomeClass();

// As the object has been constructed, the ID=1
var_dump($menu_items[$key]);

$loc_item = $menu_items[$key];
$loc_item->ID = 2;

// changed in loc_item var, that refers to the same instance
var_dump($menu_items[$key]);

?>

你会看到$ menu_items [$ key]在第一个var_dump中ID = 1,在$ loc_item中更改后ID = 2。

如果您想要遵循这种方法,也许您应该实现克隆方法,如下所述:http://php.net/manual/en/language.oop5.cloning.php