Convert an array to a string

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
g_p
Forum Newbie
Posts: 13
Joined: Sun Jan 25, 2009 9:49 am

Convert an array to a string

Post by g_p »

Hello,

i have an array, and when i do

echo "<pre>";
print_r($MyArray);
echo "</pre>";

i get

( [0] => Array ( [0] => i [1] => d ) [1] => Array ( [0] => g [1] => a ) [2] => Array ( [0] => h [1] => f ) [3] => Array ( [0] => a [1] => b [2] => h ) )

How am i supposed to convert that array in a string?
I would like to take as output idgahfabh and inside the different arrays i would like to add a "/"
like id/ga/hf/abh
Output should be id/ga/hf/abh.


Thanks in advance! :)
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: Convert an array to a string

Post by papa »

Code: Select all

 
$slash_separated = implode("/", $array);
echo $slash_separated;
 
Something like that.
mintedjo
Forum Contributor
Posts: 153
Joined: Wed Nov 19, 2008 6:23 am

Re: Convert an array to a string

Post by mintedjo »

This does it. But I don't know if its a good way to go about it

Code: Select all

    //some array
    $array = array(array("a","b"),array("c","d"),array("e","f"));
    //An array to hold strings
    $array_strings = array();
    //concatenate the inner arrays and stick them into the new array
    foreach($array as $bray){array_push($array_strings, implode($bray));}
    //concatenate the string array with / characters 
    $slash_separated = implode("/", $array_strings);
    echo $slash_separated;
g_p
Forum Newbie
Posts: 13
Joined: Sun Jan 25, 2009 9:49 am

Re: Convert an array to a string

Post by g_p »

papa wrote:

Code: Select all

 
$slash_separated = implode("/", $array);
echo $slash_separated;
 
Something like that.
Well i did that and i got Array/Array/Array/Array but i don't why it outputs that, then i tried to write sthg else

Code: Select all

 
 
for( $la = 0 ; $la < count($MyArray) ; $la++)
        print implode('/',$MyArray[$la]);
 
i got i/dg/ah/fa/b/h , it concatenates in the wrong place!
g_p
Forum Newbie
Posts: 13
Joined: Sun Jan 25, 2009 9:49 am

Re: Convert an array to a string

Post by g_p »

mintedjo wrote:This does it. But I don't know if its a good way to go about it

Code: Select all

    //some array
    $array = array(array("a","b"),array("c","d"),array("e","f"));
    //An array to hold strings
    $array_strings = array();
    //concatenate the inner arrays and stick them into the new array
    foreach($array as $bray){array_push($array_strings, implode($bray));}
    //concatenate the string array with / characters 
    $slash_separated = implode("/", $array_strings);
    echo $slash_separated;

Hm..Well i tried this code and it seems it's working!!!
Let's see, i'm gonna test it more!
g_p
Forum Newbie
Posts: 13
Joined: Sun Jan 25, 2009 9:49 am

Re: Convert an array to a string

Post by g_p »

g_p wrote:
mintedjo wrote:This does it. But I don't know if its a good way to go about it

Code: Select all

    //some array
    $array = array(array("a","b"),array("c","d"),array("e","f"));
    //An array to hold strings
    $array_strings = array();
    //concatenate the inner arrays and stick them into the new array
    foreach($array as $bray){array_push($array_strings, implode($bray));}
    //concatenate the string array with / characters 
    $slash_separated = implode("/", $array_strings);
    echo $slash_separated;

Hm..Well i tried this code and it seems it's working!!!
Let's see, i'm gonna test it more!
Great!!!It worked!!!Thank you all!!! :) :D
Post Reply