phparraysmultidimensional-arrayexplodedelimited

Convert string into a 2d array by splitting on two delimiters


I need to separate a string value on @ , then explode those values by ,. enter image description here

$str = '1000,10.00,10000.00@500,5.00,2500.0';


$ex = explode('@',$str);
 //result = Array ( [0] => 1000,10.00,10000.00 [1] => 500,5.00,2500.00 );
 
$ex2 = explode(',',$ex);
//result need Array ( [0] => 1000, [1] => 500, [2] => 2500);

Solution

  • You can use this method:

    <?php
    $str = '1000,10.00,10000.00@500,5.00,2500.0';
    $arrayResult = [];
    $arrayData = explode('@',$str);
    foreach($arrayData as $sing){
        $arrayResult[] = explode(",",$sing);
    }
    
    echo "<pre>";
    print_r($arrayResult);
    echo "</pre>";