Php 爆炸阵列

Php 爆炸阵列,php,Php,如果我有字符串: 123+0456+1789+2 我明白我可以做到以下几点: $test = 123+0,456+1,789+2,; $test = explode(",", $test); 这将为“,”之间的每个节创建一个数组 然后如何在区域的每个部分中分解“+”?我如何访问它 我知道这可能是一个非常简单的问题,但我尝试过的一切都失败了 谢谢。为什么不再次使用explode?这次用“+”代替“,”作为测力计: $test = 123+0,456+1,789+2,; $test = explo

如果我有字符串:

123+0456+1789+2

我明白我可以做到以下几点:

$test = 123+0,456+1,789+2,;
$test = explode(",", $test);
这将为“,”之间的每个节创建一个数组

然后如何在区域的每个部分中分解“+”?我如何访问它

我知道这可能是一个非常简单的问题,但我尝试过的一切都失败了


谢谢。

为什么不再次使用explode?这次用“+”代替“,”作为测力计:

$test = 123+0,456+1,789+2,;
$test = explode(",", $test);

foreach($test as $test_element){
    $explodedAgain = explode("+", $test_element);
    var_dump($explodedAgain);
}

为什么不再次使用explode?这次用“+”代替“,”作为测力计:

$test = 123+0,456+1,789+2,;
$test = explode(",", $test);

foreach($test as $test_element){
    $explodedAgain = explode("+", $test_element);
    var_dump($explodedAgain);
}

分解字符串时,返回一个数组。在您的例子中,
$test
是一个数组。因此,您需要循环通过该数组来访问每个部分

foreach($test as $subtest){

}
在上面的循环中,每个部分现在都列为
$subtest
。然后,您可以再次使用
explode
分解
$subtest
以“+”分割字符串,这将再次返回一个包含位的数组。然后您可以使用这些位

一个完整的例子是:

$test = 123+0,456+1,789+2,;
$test = explode(",", $test);

foreach($test as $subtest){
    $bits= explode("+", $subtest);
    print_r($bits);
}

分解字符串时,返回一个数组。在您的例子中,
$test
是一个数组。因此,您需要循环通过该数组来访问每个部分

foreach($test as $subtest){

}
在上面的循环中,每个部分现在都列为
$subtest
。然后,您可以再次使用
explode
分解
$subtest
以“+”分割字符串,这将再次返回一个包含位的数组。然后您可以使用这些位

一个完整的例子是:

$test = 123+0,456+1,789+2,;
$test = explode(",", $test);

foreach($test as $subtest){
    $bits= explode("+", $subtest);
    print_r($bits);
}
这是一个多维阵列,您可以通过以下方式访问它:

$test2[1][0]; // =456
这是一个多维阵列,您可以通过以下方式访问它:

$test2[1][0]; // =456
将此添加到您的代码中:

$newArr = array();
foreach($test as $v)
{
    $newArr[] = explode('+', $v);
}
$newArr
现在是一个包含数字的数组。

将此添加到代码中:

$newArr = array();
foreach($test as $v)
{
    $newArr[] = explode('+', $v);
}
preg_match_all('/((\d+)\+(\d)),+/', $test, $matches);
var_export($matches);

array (
    0 =>
    array (
        0 => '123+0,',
        1 => '456+1,',
        2 => '789+2,',
    ),
    1 =>
    array (
        0 => '123+0',
        1 => '456+1',
        2 => '789+2',
    ),
    2 =>
    array (
        0 => '123',
        1 => '456',
        2 => '789',
    ),
    3 =>
    array (
        0 => '0',
        1 => '1',
        2 => '2',
    ),
)
$newArr
现在是一个包含数字的数组

preg_match_all('/((\d+)\+(\d)),+/', $test, $matches);
var_export($matches);

array (
    0 =>
    array (
        0 => '123+0,',
        1 => '456+1,',
        2 => '789+2,',
    ),
    1 =>
    array (
        0 => '123+0',
        1 => '456+1',
        2 => '789+2',
    ),
    2 =>
    array (
        0 => '123',
        1 => '456',
        2 => '789',
    ),
    3 =>
    array (
        0 => '0',
        1 => '1',
        2 => '2',
    ),
)
主要部分在$matches[1]中(按“,”分隔)-对于键1下的结果,次要分割在$matches[2][1]和$matches[3][1]中

主要部分在$matches[1]中(按“,”分割)-对于键1下的结果,次要分割在$matches[2][1]和$matches[3][1]中