簡體   English   中英

如何使用array_walk更改元素的值?

[英]How to change the value of an element with array_walk?

如何使用array_walk更改元素的

例如,這是我的陣列,

$items = array(
    0 => array( 
        "id" => "1",
        "title" => "parent 1",
       "children" => array()
        ),

    1 => array( 
        "id" => "2",
        "title" => "parent 2",
        "children" => array (
           0 => array( 
            "id" => "4",
            "title" => "children 1"
            ),
           1 => array( 
            "id" => "5",
            "title" => "children 2"
            ) 
        ),
   )
);

我可以用下面這個改變它,

function myfunction(&$item,$key)
{
    if($item['id'] === '1')
    {
        $item['title'] = 'hello world en';
    }   
}


array_walk($items,"myfunction");

print_r($items);

但我有一個嵌套的孩子,我也想改變它的價值,如果我這樣做,我會得到錯誤,

function myfunction(&$item,$key)
{
    if($item['id'] === '1')
    {
        $item['title'] = 'hello world en';
    }

    if($item['id'] === '4')
    {
        $item['title'] = 'hello world en';
    }


    foreach($item as $key => $value)
    {

        if(is_array($value))
        {
            myfunction($value,$key);
        }
    }

}

錯誤,

注意:未定義的索引:在xx行的... index.php中的id

如果數組中有嵌套子項,我該怎么辦?

您可以通過遞歸調用回調函數來實現。 我已經實現了帶閉包的樣本,例如:

//replacement array:
$replace = [
  '1' => 'foo',
  '2' => 'bar',
  '5' => 'baz'
];

array_walk($items, $f=function(&$value, $key) use (&$f, $replace)
{
   if(isset($replace[$value['id']]))
   {
      $value['title'] = $replace[$value['id']];
   }
   if(isset($value['children']))
   {
      //the loop which is failing in question:
      foreach($value['children'] as $k=>&$child)
      {
         $f($child, $k);
      }
      //Proper usage would be - to take advantage of $f
      //array_walk($value['children'], $f);
   }
});

正如您所看到的 - 您需要的只是通過引用傳遞值並在回調內迭代它作為foreach的引用。

當你添加一行如if (!isSet($item['id'])) var_dump($item); 你會明白為什么你得到未定義的索引。

雖然我不確定你為什么要這樣做(你如何利用array_walk() ?)來解決這個問題,你可以使用類似下面的內容:

function myfunction(&$item,$key)
{
    if ($item['id'] === '1')
    {
        $item['title'] = 'hello world en';
    }

    if ($item['id'] === '4')
    {
        $item['title'] = 'hello world en';
    }


    if (isSet($item['children']) && is_array($item['children']))
        array_walk($item['children'], __FUNCTION__);
}

哪個將適用於給出的示例。

foreach($item as $key => $value)
{

    if(is_array($value))
    {
        myfunction($value,$key);
    }
}

您遍歷$ item(id,title,children)中的每個鍵。 但我想你想要的是通過價值['兒童']的每個元素(價值['兒童'] [0],價值['兒童'] [1]),對嗎? 所以它可能是這樣的:

if(is_array($value)){
   foreach($item['children'] as $key => $value){
      myfunction($value,$key);
   }
}

問題是你傳遞的是整個孩子,而不是每個孩子。 看看這個eval的外觀。 這是代碼:

<?php
$items = array(
    0 => array( 
        "id" => "1",
        "title" => "parent 1",
        "children" => array()
    ),

    1 => array( 
        "id" => "2",
        "title" => "parent 2",
        "children" => array (
           0 => array( 
            "id" => "4",
            "title" => "children 1"
            ),
           1 => array( 
            "id" => "5",
            "title" => "children 2"
            ) 
        ),
   )
);

function myfunction(&$item) {
  if($item['id'] == '1' || $item['id'] == '4') {
    $item['title'] = 'hello world en';
  }
  if( ! empty($item['children'])) {
    array_walk($item['children'], "myfunction");
  }
}

array_walk($items, "myfunction");

var_dump($items);

在您發布的代碼中,您沒有通過foreach傳遞引用。 這應該與您發布的代碼一起使用。

foreach($item as $key => &$value)
{
    if(is_array($value)) {
        myfunction($value,$key);
    }
}

並且您沒有看到未定義的索引,只需在比較值之前檢查是否已設置:

if(isset($item['id'])){
    if($item['id'] === '1'){
      ...
    }
}

在線示例

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM