Home > Software engineering >  how to make a multidimensional array correctly from string
how to make a multidimensional array correctly from string

Time:04-17

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
        )
)

CodePudding user response:

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
        )
)
  • Related