【问题标题】:Associative array Push Shift Items关联数组 Push Shift 项
【发布时间】:2012-04-11 07:25:20
【问题描述】:

我在 PHP 中有一个关联数组。

$myarray = array(
                  "a"=>"News",
                  "b"=>"Articles",
                  "c"=>"images"
                );

我想在“a”键之后插入一些值。这样数组的结构就变成了

$myarray = array(
           "a"=>"News",
           "j"=>"Latest News",  
           "k"=>"Sports News",
           "l"=>"Entertainment",  
           "b"=>"Articles",
           "c"=>"images"
           );

我怎样才能得到这个功能。

【问题讨论】:

    标签: php arrays algorithm


    【解决方案1】:

    这个函数是array_splice,但你将不得不手动做一些工作,因为它不保留密钥。让我们这样做:

    function search_and_insert($input, $afterKey, $newItems) {
        $keys = array_keys($input);
        $insertPosition = array_search($afterKey, $keys);
        if ($insertPosition === false) {
            return false;
        }
        ++$insertPosition;
    
        $newKeys = array_keys($newItems);
        array_splice($keys, $insertPosition, 0, $newKeys);
        array_splice($input, $insertPosition, 0, $newItems);
        return array_combine($keys, $input);
    }
    

    这里的想法是你分别处理键和值,为每个数组拼接一次,然后使用array_combine得到最终结果。另一个好主意是使用相同的技术编写一个可重用的 array_splice_assoc 函数并使用它,而不是让一个只做这个特定的工作。

    用法:

    $myarray = array("a"=>"News", "b"=>"Articles", "c"=>"images");
    $newItems = array("j"=>"Latest News", "k"=>"Sports News", "l"=>"Entertainment");
    $insertAfter = "a";
    
    print_r(search_and_insert($myarray, "a", $newItems));
    

    See it in action.

    【讨论】:

      【解决方案2】:

      使用它在数组中的特定键之后插入新的键/值:

       function array_insert_after($key, array &$array, $new_key, $new_value) {
            if (array_key_exists($key, $array)) {
              $new = array();
              foreach ($array as $k => $value) {
                $new[$k] = $value;
                if ($k === $key) {
                  $new[$new_key] = $new_value;
                }
              }
              return $new;
            }
            return FALSE;
          }
      

      用法:

      $array = array("a"=>"News", "b"=>"Articles", "c"=>"images");
      
      array_insert_after("a", $array, "j", "Latest News");
      

      【讨论】:

        【解决方案3】:

        使用 array_splice 时不保留数组键

        <?php
        $myarray = array(
                          "a"=>"News",
                          "b"=>"Articles",
                          "c"=>"images"
                        );
        
        $newarray = array(
                        "j"=>"Latest News",
                        "k"=>"Sports News",
                        "l"=>"Entertainment");
        
        var_dump($myarray);
        
        array_splice($myarray, 1, 0, $newarray);
        
        var_dump($myarray);
        ?>
        

        输出

        array(3) {
          ["a"]=>
          string(4) "News"
          ["b"]=>
          string(8) "Articles"
          ["c"]=>
          string(6) "images"
        }
        
        array(6) {
          ["a"]=>
          string(4) "News"
          [0]=>
          string(11) "Latest News"
          [1]=>
          string(11) "Sports News"
          [2]=>
          string(13) "Entertainment"
          ["b"]=>
          string(8) "Articles"
          ["c"]=>
          string(6) "images"
        }
        

        【讨论】:

        • 您是否看到此程序的输出与问题中所述的所需输出之间有任何差异?
        • 使用 array_splice 时不会保留数组键,否则这是一个优雅的解决方案,这也是我的第一个想法 :)
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-10-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-24
        相关资源
        最近更新 更多