【问题标题】:PHP Split & join array into separate linesPHP将数组拆分并加入单独的行
【发布时间】:2016-05-07 23:45:04
【问题描述】:

我有以下数组:

Array ( [0] => Array ( [0] => Bob 
                       [1] => Freddy 
                     ) 
        [1] => Array ( [0] => IT Dev 
                       [1] => Programmer 
                     ) 
        [2] => Array ( [0] => 123 
                       [1] => 23423 
                     ) 
      )

我正在尝试将数组连接在一起,所以它看起来像下面这样:

Bob - IT 开发人员 - 123

弗雷迪 - 程序员 - 23423

我一直在搞乱内爆函数,但实际上我不知道如何在 PHP 中实现这一点

任何帮助将不胜感激。

非常感谢

最大

【问题讨论】:

    标签: php arrays string join split


    【解决方案1】:

    如果你的数组被称为$myArray,那么:

    foreach ($myArray as $row)
    {
      $string1 .= $row[0]."-";
      $string2 .= $row[1]."-";
    }
    
    echo $string1."<br>";
    echo $string2."<br>";
    

    【讨论】:

    • 实际上如果有超过 2 个数组,例如[0],[1],[2],[3] 等...有没有可以循环遍历它们?
    • 会不会像 1 个数组中的 3 个数组?或 1 个数组内 1 个数组内 1 个数组?如果你愿意,你可以根据新情况编辑你的答案,然后看看它
    • @MaxThorley 哦,如果这个答案对你有用,请接受是(带有绿色复选标记),以便未来的用户知道它对你有用:)
    【解决方案2】:

    假设这是你的数组:

    $array = array(
        array('Bob', 'Freddy'),
        array('IT Dev', 'Programmer'),
        array('123', '23423')
    );
    

    您必须遍历每个数组,将它们连接成一个句子。

    $result = array();
    foreach($array as $data){
        foreach($data as $index => $value){
            if(!isset($result[$index]))
                $result[$index] = $value;
            else
                $result[$index] .= " - " . $value;
        }
    }
    

    现在如果你var_dump$result:

    array(2) {
        [0]=>
            string(18) "Bob - IT Dev - 123"
        [1]=>
            string(27) "Freddy - Programmer - 23423"
    }
    

    如果你想访问一个单独的句子,你可以这样做:

    echo $result[0]; // Bob - IT Dev - 123
    

    【讨论】:

      【解决方案3】:

      只需使用 array_column 和 implode 即可获得输出。

      数组:

      $arr = array(
          array('Bob', 'Freddy'),
          array('IT Dev', 'Programmer'),
          array('123', '23423')
      );
      

      机制:

      echo implode(" - ", array_column($arr, 0)); //Bob - IT Dev - 123
      echo implode(" - ", array_column($arr, 1)); //Freddy - Programmer - 23423
      

      【讨论】:

        【解决方案4】:

        我有两个可靠的方法给你。第一个是“可变方法”(... splat operator),第二个是利用 array_column() 的经典 foreach 循环。

        代码:(Demo)

        $array=[
            ['Bob','Freddy'],
            ['IT Dev','Programmer'],
            [123,23423]
        ];
        
        // Use this to see that both methods are flexible and produce the same result,
        // so long as all subarrays have equal length.
        // If there are differences in length, depending where the hole is, the results
        // between the two methods will vary slightly.
        // (The 1st method best preserves the intended output structure.)
        /*$array=[
            ['Bob','Freddy','Jane'],
            ['IT Dev','Programmer','Slacker'],
            [123,23423,0],
            ['more1','more2','more3']
        ];*/
        
        // this one requires php5.6 or better
        $imploded_columns=array_map(function(){return implode(' - ',func_get_args());},...$array);
        var_export($imploded_columns);
        
        echo "\n\n";
        unset($imploded_columns);
        
        // or you can go with a sub-php5.6 version:
        foreach($array[0] as $k=>$v){
            $imploded_columns[]=implode(' - ',array_column($array,$k));
        }
        var_export($imploded_columns);
        

        两种方法的输出:

        // variadic method
        array (
          0 => 'Bob - IT Dev - 123',
          1 => 'Freddy - Programmer - 23423',
        )
        
        // foreach method
        array (
          0 => 'Bob - IT Dev - 123',
          1 => 'Freddy - Programmer - 23423',
        )
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2022-01-14
          • 1970-01-01
          • 1970-01-01
          • 2021-07-23
          • 1970-01-01
          • 2015-03-11
          • 2018-11-24
          • 1970-01-01
          相关资源
          最近更新 更多