How to convert a string

There is a string like:

text=Mile:year=2010:genre=:country=:

From it you need to get associative arrays (or one multidimensional) of the type:

Array ( [text] => Mile )

That’s how it turned out so far, but you need to stuff it into the loop somehow.

$str = 'text=Mile:year=2010:genre=:country=:';
$search = explode(':', $str);
$search1 = explode('=',$search[0]);
$out[$search1[0]] = $search1[1];
echo print_r($out);

Maybe with regular expressions it will be easier?


Answer 1, authority 100%

Something like this

$array = explode(':','text=Mile:year=2010:genre=:country=:');
$arrayLength = count($array);
$result = array();
for($i=0; $i<$arrayLength; $i++) {
   $tempData = explode('=',$array[$i]);
   if(!empty($tempData[0])) { //      
      $result[$tempData[0]] = !empty($tempData[1]) ? $tempData[1] : ' ';
   }
}

Answer 2, authority 50%

$str = 'text=Mile:year=2010:genre=:country=:';
$search = explode(':', $str);
for($i = 0; $i < count($search); $i++)
{
    $search[$i] = explode('=', $search[$i]);
    if(!empty($search[$i][1]))
    {
        echo $search[$i][0] . ' - ' . $search[$i][1] . '<br />';
    }
}