Results 1 to 5 of 5

Thread: PHP: array to string?

  1. #1
    Join Date
    Dec 2008
    Posts
    37

    PHP: array to string?

    hi,
    Lets say I want to get the contents of a simple array:

    $arr = array(“p”, “q”, “r”, “s”);

    Into a string, with each element seperated by a "+", preferably with no trailing "+".is there any function that converts the values of an array to string?Better yet, is there a function for sorting strings?

  2. #2
    Join Date
    May 2008
    Posts
    2,297

    Re: PHP: array to string?

    This simple function converts a PHP array to a string containing a PHP array declaration. It can be used to put an array into a database field. It can then be retrieved and eval()'d in another script.
    Code:
    <?php
    
    $arr = array('h','e','l','l','o');
    $string = implode($arr, '');
    
    ?>

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    Re: PHP: array to string?

    The function below facilitates the process of converting an array to a string. Very useful for building SQL insert queries with arrays of data.
    Code:
    <?php  
    function array2str($array, $pre = '', $pad = '', $sep = ', ')  
    {  
        $str = '';  
        if(is_array($array)) {  
            if(count($array)) {  
                foreach($array as $v) {  
                    $str .= $pre.$v.$pad.$sep;  
                }  
                $str = substr($str, 0, -strlen($sep));  
            }  
        } else {  
            $str .= $pre.$array.$pad;  
        }  
      
        return $str;  
    }  
    ?>

  4. #4
    Join Date
    May 2008
    Posts
    2,389

    Re: PHP: array to string?

    I’ve been working a great deal with vBulletin, our forums system, and in doing so have had to brush on my PHP. Through the course of preparing the system for the summer I’ve come across a few PHP functions that I was not aware. Here’s a few that you may not know about:

    implode: takes an array of strings and concatenates them, using $glue as a separator.

    Example: string implode (string $glue, array $pieces)
    $before = array(’one’, ‘two’, ‘three’);
    $after = implode(”,”, $before);
    echo $after;

    Produces:one,two,three

  5. #5
    Join Date
    Feb 2008
    Posts
    1,852

    Re: PHP: array to string?

    Arrays are not automatically passed by reference in PHP (unlike C, for instance). As with other variables, however, you can write your functions to accept arrays as pass-by-reference arguments. Of course, this applies only to PHP 4.

    Code:
    Create an array of user information with defaults, then fill it with information. */
    $user_info = array('lastname' => '',
                       'initial' => '',
                       'firstname' => '',
                       'homedir' => '');
    $user_info['lastname'] = 'User';
    $user_info['initial'] = 'L.';
    $user_info['firstname'] = 'Joe';
    $user_info['homedir'] = '/home/joeluser';
    
    /* Filling an indexed array with the letters of the English alphabet. */
    $arr = array();
    for ($i = 97; $i < 123; $i++) {
        $arr[] = chr($i);
    }
    print_r($arr);
    
    /* Do the same thing, but make an array containing two arrays, one of uppercase
     * letters and one of lowercase. */
    $arr = array();
    for ($i = 97; $i < 123; $i++) {
        $arr['lowercase'][] = chr($i);
        $arr['uppercase'][] = chr($i - 32);
    }
    print_r($arr);

Similar Threads

  1. String is not occurring in array.
    By rUChIRr in forum Software Development
    Replies: 6
    Last Post: 17-06-2011, 10:20 PM
  2. Splitting string into array with delimiters
    By Paul$Terry in forum Software Development
    Replies: 6
    Last Post: 10-06-2011, 07:09 AM
  3. Problem in creating array from string values
    By Cedric in forum Software Development
    Replies: 4
    Last Post: 19-01-2010, 05:48 PM
  4. JavaScript to convert an Array to String
    By KAMANA in forum Software Development
    Replies: 3
    Last Post: 09-12-2009, 05:30 AM
  5. NullPointerException when using array of string in Java
    By Sean J in forum Software Development
    Replies: 2
    Last Post: 25-04-2009, 08:14 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,857,049.77867 seconds with 16 queries