How to convert a string to an array?

There is a string like:

({"result": "silver":0,"red":1,"blue":0,"green":0,"orange":0,"white":0,"black":0,"gray":0}})

How can I turn it into something like an array so that it is convenient to navigate through the data. For example:

foreach ($array as $color => $state) {
  if ($state) {
    .....
  } else {
    .....
  }
}

Answer 1, authority 100%

Perhaps json_decode

will help you

<?php
$a = '({"result":{"silver":0,"red":1,"blue":0,"green":0,"orange":0,"white":0,"black":0,"gray":0}})';
$b = json_decode(preg_replace('/^\(/','',preg_replace('/\)$/','',$a)));
foreach ($b["status"] as $color => $state) {
    //...
}

Answer 2

this is not a string but a hash, and there is not enough “{” after ‘result’:
so dig you towards hashes in php.
in javascript something like this

var hash = {"result": {"silver":0,"red":1,"blue":0,"green":0,"orange":0,"white":0,"black":0,"gray":0}};
hash.result.silver; // => 0
for(var i in hash.result) 
 hash.result[i]; // => 0,1,0,0,0,0,0,0