Page 1 of 1

Convert an array to a string

Posted: Thu Feb 05, 2009 11:05 am
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! :)

Re: Convert an array to a string

Posted: Thu Feb 05, 2009 11:22 am
by papa

Code: Select all

 
$slash_separated = implode("/", $array);
echo $slash_separated;
 
Something like that.

Re: Convert an array to a string

Posted: Thu Feb 05, 2009 11:33 am
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;

Re: Convert an array to a string

Posted: Thu Feb 05, 2009 11:52 am
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!

Re: Convert an array to a string

Posted: Thu Feb 05, 2009 11:55 am
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!

Re: Convert an array to a string

Posted: Thu Feb 05, 2009 12:19 pm
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