1

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

1 Answer 1

1

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
        )
)
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for your comment. It is correct. But my data comes from mysql output. When putting mysql output into $txt, not working. can you give me any idea?
@ABShaman Please edit your question to show exactly what is output from mysql using var_dump
I solved it by changing one line like this $a = explode(',',MY OUT PUT); Now ok, Thank you very musha @JacobMulquin

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.