Multidimensional Array PHP Implode

前端 未结 5 754
梦如初夏
梦如初夏 2020-12-02 17:48

In terms of my data structure, I have an array of communications, with each communications_id itself containing three pieces of information: id, score, and content.

5条回答
  •  被撕碎了的回忆
    2020-12-02 18:02

    You can have a look to array_walk_recursive function . This is a working snippet of creating of recursive array to string conversion :

    $array = 
      array(
        "1"    => "PHP code tester Sandbox Online",  
        "foo"  => "bar", 
         5 , 
         5     => 89009, 
        "case" => "Random Stuff", 
        "test" => 
           array(
             "test"  => "test221",
             "test2" => "testitem"
           ),
        "PHP Version" => phpversion()
      );
    
    $string="";
    
    $callback = 
      function ($value, $key) use (&$string) {
         $string .= $key . " = " . $value . "\n";
      };
    
    array_walk_recursive($array, $callback);
    
    echo $string;
    ## 1 = PHP code tester Sandbox Online
    ## foo = bar
    ## 2 = 5
    ## 5 = 89009
    ## case = Random Stuff
    ## test = test221
    ## test2 = testitem
    ## PHP Version = 7.1.3
    

提交回复
热议问题