Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 13-04-2009
Member
 
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?
Reply With Quote
  #2  
Old 13-04-2009
Member
 
Join Date: May 2008
Posts: 2,293
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, '');

?>
Reply With Quote
  #3  
Old 13-04-2009
Member
 
Join Date: May 2008
Posts: 2,008
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;  
}  
?>
Reply With Quote
  #4  
Old 13-04-2009
Member
 
Join Date: May 2008
Posts: 2,383
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
Reply With Quote
  #5  
Old 13-04-2009
Member
 
Join Date: Feb 2008
Posts: 1,848
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);
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


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


All times are GMT +5.5. The time now is 01:36 PM.