如何多重爆炸?

时间:2020-06-08 09:19:15

标签: php arrays

我需要分隔第一个@和第二个,这个变量 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);

2 个答案:

答案 0 :(得分:1)

您可以使用此方法:

<?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>";

答案 1 :(得分:0)

explode()返回一个数组,因此$ex将是您需要遍历/遍历的数组:

<?php
// Init array to hold exploded values
$exploded = [];
$str = '1000,10.00,10000.00@500,5.00,2500.0';

$ex = explode('@',$str);

// Iterate over the $ex-exploded items
foreach ( $ex as $exItem ) {
  // Add items to the $exploded array
  array_push($exploded, explode(',', $exItem));    
}

print_r($exploded);

将输出

Array
(
    [0] => Array
        (
            [0] => 1000
            [1] => 10.00
            [2] => 10000.00
        )

    [1] => Array
        (
            [0] => 500
            [1] => 5.00
            [2] => 2500.0
        )

)

编辑: 如果您希望所有值都在一个数组中,则可以

$exploded = array_merge($exploded[0], $exploded[1]);
print_r($exploded);

将输出

Array
(
    [0] => 1000
    [1] => 10.00
    [2] => 10000.00
    [3] => 500
    [4] => 5.00
    [5] => 2500.0
)