AB Shaman
AB Shaman

Reputation: 21

how to make a multidimensional array correctly from string

I try to make a multidimensional array in php from my sting data. But I couldn't make it. Please help me.

My sting data is 3=>11040,2=>11160,1=>23400

Want output,

Array
(
    [0] => Array
        (
            [1] => 23400
            [2] => 11160
            [3] => 11040
        )
)

Upvotes: 1

Views: 85

Answers (1)

Serghei Leonenco
Serghei Leonenco

Reputation: 3517

You can do that this way:

<?php
$txt = "3=>11040,2=>11160,1=>23400";
$a = explode(',', $txt);
$b = [];
foreach($a as $v){
    $text = explode('=>', $v);
    $b[$text[0]] = $text[1];
}
ksort($b);
$c = array($b);
var_dump($c);
?>

Output:

Array
(
    [0] => Array
        (
            [1] => 23400
            [2] => 11160
            [3] => 11040
        )
)

Upvotes: 1

Related Questions