2

I Make a

echo '<pre>';
echo var_dump($arrayX);
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');
1
  • I am sorry but I do not understand the question. The output you want in $myarray is as far as I can see similar to the one you already have in $arrayX. Commented Nov 20, 2009 at 8:41

5 Answers 5

3

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);
Sign up to request clarification or add additional context in comments.

2 Comments

This is by no means a generic var_dump parser, it's quite specific
Yeah, maybe someone finds it useful. This is kind of array specific and does only flat arrays, not multidimensional.
1

ehm, …? this:

$myarray = $arrayX;

if you want to make it complicated, you could use var_export

Comments

0

I must admit I don't understand your question.

If you want a copy of $arrayX, then simply type

$myarray = $arrayX;

Comments

0

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.