fork download
  1. <?php
  2.  
  3. echo "<h3>1) array as string</h3>";
  4. $sa = '["item1","item2","item3"]';
  5. var_dump($sa);
  6. echo "<h3>2) array as string decoded</h3>";
  7. $sad = json_decode($sa);
  8. var_dump($sad);
  9. echo "<h3>3) array as string decoded, then encoded again. <small>(Note it's the same as the original string)</small></h3>";
  10. $sade = json_encode($sad);
  11. var_dump($sade);
  12. echo "<h3>4) Unset decoded</h3>";
  13.  
  14. unset($sad[0]);
  15. $sad = array_values($sad);
  16.  
  17. var_dump($sad);
  18. echo "<h3>5) Encode unset array. <small>(Expecting it to look like original string minus the unset value)</small></h3>";
  19. $ns = json_encode($sad);
  20. var_dump($ns);
  21. echo "<h3>6) Decode Encoded unset array</h3>";
Success #stdin #stdout 0.02s 24448KB
stdin
Standard input is empty
stdout
<h3>1) array as string</h3>string(25) "["item1","item2","item3"]"
<h3>2) array as string decoded</h3>array(3) {
  [0]=>
  string(5) "item1"
  [1]=>
  string(5) "item2"
  [2]=>
  string(5) "item3"
}
<h3>3) array as string decoded, then encoded again. <small>(Note it's the same as the original string)</small></h3>string(25) "["item1","item2","item3"]"
<h3>4) Unset decoded</h3>array(2) {
  [0]=>
  string(5) "item2"
  [1]=>
  string(5) "item3"
}
<h3>5) Encode unset array. <small>(Expecting it to look like original string minus the unset value)</small></h3>string(17) "["item2","item3"]"
<h3>6) Decode Encoded unset array</h3>array(2) {
  [0]=>
  string(5) "item2"
  [1]=>
  string(5) "item3"
}