Merge array items into string

后端 未结 10 1863
遇见更好的自我
遇见更好的自我 2020-11-30 12:33

How do I merge all the array items into a single string?

相关标签:
10条回答
  • 2020-11-30 13:05

    Try this from the PHP manual (implode):

    <?php
        $array = array('lastname', 'email', 'phone');
        $comma_separated = implode(",", $array);
    
        echo $comma_separated; // lastname, email, and phone
    
        // Empty string when using an empty array:
        var_dump(implode('hello', array())); // string(0) ""
    ?>
    
    0 讨论(0)
  • 2020-11-30 13:05
    foreach($array as $key => $value) {
    
    $string .= $value .' ';
    
    }
    
    0 讨论(0)
  • 2020-11-30 13:08

    You can use join. It's an alias for implode, and in my opinion more readable:

    $fruits = array('apples', 'pears', 'bananas');
    echo join(',', $fruits);
    
    0 讨论(0)
  • 2020-11-30 13:11
    join(" -- ", Array("a", "b")) == "a -- b"
    

    actually join is an alias for the implode function.

    0 讨论(0)
  • 2020-11-30 13:12
    $array1 = array(
        "one",
        "two",
    )
    $array2 = array(
        "three",
        "four",
    )
    $finalarr = array_merge($array1, $array2);
    $finalarr = implode(",", $finalarr);
    

    will produce this one,two,three,four

    0 讨论(0)
  • 2020-11-30 13:19
    $array= array( "Alfred Hitchcock", "Stanley Kubrick", "Martin Scorsese", "Fritz Lang" );
    $string="";
    
    foreach ( $tempas $array) {
      $string=$string.",".$temp;
    }
    
    0 讨论(0)
提交回复
热议问题