PHP string to associative array?

andrewrutley

How do I create an associative array with the following string?size:3|order_by:date|order:ASC|post_type:post to

Array
(
  "size" => 3,
  "order_by" => "date",
  "order" => "ASC",
  "post_type" => "post"
)
RiggsFolly

Like this

$str = 'size:3|order_by:date|order:ASC|post_type:post';

$arr = explode('|', $str);

foreach ( $arr as $a){
    $t = explode(':', $a);
    $new[$t[0]] = $t[1];
}

print_r($new);

RESULT

Array
(
    [size] => 3
    [order_by] => date
    [order] => ASC
    [post_type] => post
)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related