Convert the first element of an array to a string in PHP

后端 未结 13 1702
盖世英雄少女心
盖世英雄少女心 2020-12-05 02:00

I have a PHP array and want to convert it to a string.

I know I can use join or implode, but in my case array has only one item. Why do I

相关标签:
13条回答
  • 2020-12-05 02:38

    Convert an array to a string in PHP:

    1. Use the PHP join function like this:

      $my_array = array(4, 1, 8);
      
      print_r($my_array);
      Array
      (
          [0] => 4
          [1] => 1
          [2] => 8
      )
      
      $result_string = join(',', $my_array);
      echo $result_string;
      

      Which delimits the items in the array by comma into a string:

      4,1,8
      
    2. Or use the PHP implode function like this:

      $my_array = array(4, 1, 8);
      echo implode($my_array);
      

      Which prints:

      418
      
    3. Here is what happens if you join or implode key value pairs in a PHP array

      php> $keyvalues = array();
      
      php> $keyvalues['foo'] = "bar";
      
      php> $keyvalues['pyramid'] = "power";
      
      php> print_r($keyvalues);
      Array
      (
          [foo] => bar
          [pyramid] => power
      )
      
      php> echo join(',', $keyvalues);
      bar,power
      php> echo implode($keyvalues);
      barpower
      php>
      
    0 讨论(0)
  • 2020-12-05 02:40

    A For() loop is very useful. You can add your array's value to another variable like this:

    <?php
        $dizi=array('mother','father','child'); //This is array
    
        $sayi=count($dizi);
        for ($i=0; $i<$sayi; $i++) {
            $dizin.=("'$dizi[$i]',"); //Now it is string...
        }
             echo substr($dizin,0,-1); //Write it like string :D
    ?>
    

    In this code we added $dizi's values and comma to $dizin:

    $dizin.=("'$dizi[$i]',");

    Now

    $dizin = 'mother', 'father', 'child',
    

    It's a string, but it has an extra comma :)

    And then we deleted the last comma, substr($dizin, 0, -1);

    Output:

    'mother','father','child'

    0 讨论(0)
  • 2020-12-05 02:41

    Use the inbuilt function in PHP, implode(array, separator):

    <?php
        $ar = array("parth","raja","nikhar");
        echo implode($ar,"/");
    ?>
    

    Result: parth/raja/nikhar

    0 讨论(0)
  • 2020-12-05 02:44

    You can use the reset() function, it will return the first array member.

    0 讨论(0)
  • 2020-12-05 02:44

    You could use print_r and html interpret it to convert it into a string with newlines like this:

    $arraystring = print_r($your_array, true); 
    
    $arraystring = '<pre>'.print_r($your_array, true).'</pre>';
    

    Or you could mix many arrays and vars if you do this

    ob_start();
    print_r($var1);
    print_r($arr1);
    echo "blah blah";
    print_r($var2);
    print_r($var1);
    $your_string_var = ob_get_clean();
    
    0 讨论(0)
  • 2020-12-05 02:45

    Is there any other way to convert that array into string ?

    Yes there is. serialize(). It can convert various data types (including object and arrays) into a string representation which you can unserialize() at a later time. Serializing an associative array such as Array(18 => 'Somthing') will preserve both keys and values:

    <?php
    $a = array(18 => 'Something');
    echo serialize($a);                                   // a:1:{i:18;s:9:"Something";}
    var_dump(unserialize('a:1:{i:18;s:9:"Something";}')); // array(1) {
                                                          //   [18]=>
                                                          //   string(9) "Something"
                                                          // }
    
    0 讨论(0)
提交回复
热议问题