I Make a
echo '<pre>';
echo var_dump($arra开发者_JAVA百科yX);
echo '</pre>'
I got the result:
array(6) {
[0]=>
string(9) "AAA"
[1]=>
string(13) "BBB"
[2]=>
string(8) "CCC"
[3]=>
string(8) "DDD"
[4]=>
string(8) "EEE"
[5]=>
string(13) "FFF"
}
How Can I make It to A New array What I want is to get arrayX in this format :
array('AAA', 'BBB' , 'CCC','DDD', 'EEE', 'FFF');
$myarray = array('AAA', 'BBB' , 'CCC','DDD', 'EEE', 'FFF');
If you want a copy of it then
$a = $arrayX;
However if you have to convert it to some string format then the better way of doing it would be this
$dump = var_export($a,true);
eval('$b = ' . $dump . ';');
Or better yet
$s = serialize($a);
$c = unserialize($s);
If that does not do it then here is how to parse the vardump format in question
function parseValue($value) {
return substr(preg_replace('/\s*[a-z]+\([0-9]+\)\s+"(.*)/','\\1',$value),0,-2);
}
function parseIndex($index) {
return preg_replace('/[^[]*\[([0-9]+)\].*/','\\1',$index);
}
function parseVardump($dump) {
$lines = explode("\n",$dump);
foreach ($lines as $line) {
switch (true) {
case preg_match('/array\([0-9]+\) {/',$line) :
break;
case preg_match('/\[[0-9]+\]=>/',$line) :
// end previous value
if (isset($index)) {
$ar[$index] = parseValue($value);
}
$index = parseIndex($line);
$value = '';
break;
case preg_match('/}$/',$line) :
if (isset($index)) {
$ar[$index] = parseValue($value);
}
break;
default:
$value .= $line . "\n";
break;
}
}
return $ar;
}
$a = array("AAA\n", 'BBB' , 'CCC','DDD', 'EEE', 'FFF');
ob_start();
var_dump($a);
$dump = ob_get_contents();
ob_end_clean();
$ar = parseVardump($dump);
ehm, …? this:
$myarray = $arrayX;
if you want to make it complicated, you could use var_export
I must admit I don't understand your question.
If you want a copy of $arrayX
, then simply type
$myarray = $arrayX;
you could right a simple function to output as the format you wish. like this one:
<?php
function dump_array($ar) {
$output = 'array(';
$lastIndex = count($ar) - 1;
$counter = 0;
foreach($ar as $key => $value) {
$output .= (is_string($value) ? "'{$value}'" : $value) . ( $counter++ < $lastIndex ? ', ' : '' );
}
$output .= ')';
return $output;
}
?>
although remember that the built-in "var_dump()" function recurses into arrays and shows information about objects inside the array. if you need such functionality, you should extend this function to do so.
If I understand it, what you are doing is outputing your array on some website and trying to create it again on another site which fetch the first one. If this is right and you have control over the first site (the one doing the print_r), I think you should use the serialize and unserialize functions.
精彩评论