1

I have one object and i have to convert into array, i have used json encode and json decode but it is not working properly.

my object

$LearningNodesData = '{
        0:"5df31",
        1:"5df32",
        2:"5df33"
    }';

my code

    $LearningNodesData1 =json_decode(json_encode($LearningNodesData,true),true);
echo "<pre>";
print_r($LearningNodesData1);

my expected output

[
  "5df31",
  "5df32",
  "5df33"
]

my ouput

{
    0:"5df1",
    1:"5df2",
    2:"5df3"
}

Here what is wrong

Updated code section

<?php
$LearningNodesData = '{
    "0":"5df31",
    "1":"5df32",
    "2":"5df33"
}';

echo my_json_decode($LearningNodesData);


function my_json_decode($s) {
    $s = str_replace(
        array('"',  "'"),
        array('\"', '"'),
        $s
    );
    $s = preg_replace('/(\w+):/i', '"\1":', $s);
    return json_decode(sprintf('{%s}', $s));
}
?>
James Z
  • 12,209
  • 10
  • 24
  • 44
  • provided you have an actual valid json string, you're doing it the wrong way, decode it first with the true flag, then encode it, so `encode(decode(string here, true))`, you're doing it the other way around – Kevin Jul 22 '20 at 04:48
  • @kevin, i have used like $LearningNodesData1 = json_encode(json_decode($LearningNodesData, true)); but i am getting `null` – Nallammal T Jul 22 '20 at 04:53
  • If you're expecting an array, you should start with an array. – Brad Jul 22 '20 at 05:12

4 Answers4

3

Your string is not a valid json.

Valid json would be:

$LearningNodesData = '{
    "0":"5df31",
    "1":"5df32",
    "2":"5df33"
}';

you can read this: php json_decode fails without quotes on key maybe you find the solution

Burhan Ibrahimi
  • 367
  • 3
  • 14
  • Can you please check my updated code section, even though not working, nothing is printing – Nallammal T Jul 22 '20 at 05:10
  • `$LearningNodesData = '{ "0":"5df31", "1":"5df32", "2":"5df33" }'; $array_data = json_decode($LearningNodesData, true); return array_values( $array_data ); ` Can you please check this. – Burhan Ibrahimi Jul 22 '20 at 09:19
2

You are trying to encode/decode a dictionary and your expected result is a list. If your desired result is a list then try this!

$LearningNodesData = '["5df31","5df32","5df33"]';

Not this

$LearningNodesData = '{
    0:"5df31",
    1:"5df32",
    2:"5df33"
}';
1

If you are converting an object into array, it will always return with keys

    $LearningNodesData = '{
    "0":"5df31",
    "1":"5df32",
    "2":"5df33"
    }';
   $arr = json_decode($LearningNodesData,true);
   print_r($arr);
  //output
  Array
  (
    [0] => 5df31
    [1] => 5df32
    [2] => 5df33
  )

In the end array without a key or with the key doesn't matter (if it is numeric key). Your desired output is without key but you will access with their index position.

If you don't want it as array you can make it into comma formatted string using imploade() function

echo imploade(',',$arr); //5df31,5df32,5df33
DEEPAK
  • 1,364
  • 10
  • 24
0

If I understand, you want to echo/print array, but no keys? If so:

<?php
$learningNodesData = '{
    "0":"5df31",
    "1":"5df32",
    "2":"5df33"
}';

$decodedLearningNodesData = json_decode($learningNodesData, true);

$noKeysLearningNodesData = json_encode(array_values($decodedLearningNodesData));
print_r($noKeysLearningNodesData);

?>

Will print out:

["5df31","5df32","5df33"]
JureW
  • 641
  • 1
  • 6
  • 15