1

I've generated an array like below.But unable to parse it in json_encode.Am I missing anything here? please let me know.

PHP code :

<?php

$i=0;
$result = shell_exec('scripts/fetch_script.sh');

foreach(preg_split("/((\r?\n)|(\r\n?))/", $result) as $line)
{
    parse_str($line);

}

$stat = "array(";
    $lines = file('parameter.file');
    foreach ($lines as $line) {
        $v = explode('|', strtoupper($line));
        $str = '$v[1]';
        eval("\$str = \"$str\";");
        eval("\$str = \"$str\";");
        $calc = eval("return round(($str),2);");

        $stat .="'$v[0]' => $calc,";


    }
    $stat .=");";
echo json_encode($stat);
?>

But output shows like Below which is not in json format:

"array('QUERY_CACHE_USAGE' => 0,'DISK_TMP_TABLES_USAGE' => 1.52,'QUERY_CACHE_USAGE' => 0,'DISK_TMP_TABLES_USAGE' => 1.52,'QUERY_CACHE_USAGE' => 0,'DISK_TMP_TABLES_USAGE' => 1.52,'QUERY_CACHE_USAGE' => 0,'DISK_TMP_TABLES_USAGE' => 1.52,'QUERY_CACHE_USAGE' => 0,'DISK_TMP_TABLES_USAGE' => 1.52,'QUERY_CACHE_USAGE' => 0,'DISK_TMP_TABLES_USAGE' => 1.52,'QUERY_CACHE_USAGE' => 0,'DISK_TMP_TABLES_USAGE' => 1.52,'QUERY_CACHE_USAGE' => 0,'DISK_TMP_TABLES_USAGE' => 1.52,'QUERY_CACHE_USAGE' => 0,'DISK_TMP_TABLES_USAGE' => 1.52,'QUERY_CACHE_USAGE' => 0,'DISK_TMP_TABLES_USAGE' => 1.52,'QUERY_CACHE_USAGE' => 0,'DISK_TMP_TABLES_USAGE' => 1.52,'QUERY_CACHE_USAGE' => 0,'DISK_TMP_TABLES_USAGE' => 1.52,);"
6
  • 1
    $stat is string it is not array, try with var_dump($stat); and test it Commented Nov 28, 2017 at 7:18
  • Used var_dump in script ,still facing this issue. .. .... var_dump($stat); echo json_encode($stat); below is the output. jpst.it/181m8 Commented Nov 28, 2017 at 8:32
  • json_encode($stat, true), try to add true in parameter json_encode Commented Nov 28, 2017 at 8:48
  • 2
    json_encode works for an array, not string. Change your code to make array instead of string Commented Nov 28, 2017 at 9:05
  • 3
    Why are you using eval() for everything? And why is not "script" not return proper JSON to begin with? Commented Nov 29, 2017 at 23:54

1 Answer 1

1

The array you're trying to convert to JSON has duplicates, which means that it'll only keep the last occurrence of a duplicated key. For example :

var_dump(['foo' => 'bar', 'foo' => 2]); //displays ["foo"] => int(2)

Since you're already using eval you might as well also use it to evaluate the string into an array :

echo json_encode(eval('return ' . $raw), JSON_PRETTY_PRINT);

Do know however that "eval" is one letter away from "evil". Unless you trust the contents of parameter.file, I'd avoid relying on it. I recommend parsing the file manually instead of relying on eval.

Sign up to request clarification or add additional context in comments.

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.