tags:

views:

124

answers:

4

Possible Duplicate:
Convert PHP array string into an array

Which function can be used to convert an array into a string ,maintaining your ability to return the string into an array

+6  A: 

The serialize function turns any value into a string.

Gumbo
+1  A: 

You can use the implode function to convert an array into a string:

$array = implode(" ", $string); //space as glue

If you want to convert it back to an array you can use the explode function:

$string = explode(" ", $array); //space as delimiter
Stevens
And if $array[2] == "Some value"?
Matthew Scharley
I don't know the complexity of his problem. This is just a simple/quick solution...
Stevens
A: 
function makestring($array)
  {
  $outval = '';
  foreach($array as $key=>$value)
    {
    if(is_array($value))
      {
      $outval .= makestring($value);
      }
    else
      {
      $outval .= $value;
      }
    }
  return $outval;
  }
joe
+2  A: 

Just to add, there's also the var_export function. I've found this useful for certain situations. From the manual:

var_export — Outputs or returns a parsable string representation of a variable

Example:

<?php
$a = array (1, 2, array ("a", "b", "c"));
var_export($a);
?>

Returns this output (which can then be converted back to an array using eval()):

array (
  0 => 1,
  1 => 2,
  2 => 
  array (
    0 => 'a',
    1 => 'b',
    2 => 'c',
  ),
)
karim79