Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 聪明的爆炸或者别的什么_Php - Fatal编程技术网

Php 聪明的爆炸或者别的什么

Php 聪明的爆炸或者别的什么,php,Php,我有一个数组: $array = [ 'aaa 3', 'bbb 15', 'ccccc 3A', 'dddd 2412', 'eee fff 15', 'ggg 612', 'hhh iiiii 23B', ]; 我希望收到: $name = 'aaa'; $number = '3'; $name = 'bbb'; $number = '15'; $name = 'ccccc'; $number = '3A'; $name = 'ddd

我有一个数组:

$array = [
    'aaa 3',
    'bbb 15',
    'ccccc 3A',
    'dddd 2412',
    'eee fff 15',
    'ggg 612',
    'hhh iiiii 23B',
];
我希望收到:

$name = 'aaa'; $number = '3';
$name = 'bbb'; $number = '15';
$name = 'ccccc'; $number = '3A';
$name = 'dddd'; $number = '2412';
$name = 'eee'; $number = '15';
$name = 'ggg'; $number = '612';
$name = 'hhh iiiii'; $number = '23B';
因此,我:

foreach ($array as $item) {
    $expl = explode(' ', $item);
    $name = $expl[0];
    $number = $expl[1];
}
但这不适用于
EEFFF15
hhhIIIIIII23b
,因为这些名称有两部分。如何更好地实现这一点?

使用正则表达式:

<?php

$array = [
    'aaa 3',
    'bbb 15',
    'ccccc 3A',
    'dddd 2412',
    'eee fff 15',
    'ggg 612',
    'hhh iiiii 23B',
];

$regex = '#(?<word>.+)\s(?<number>.+)#';
$results = [];

foreach ($array as $line) {
    preg_match($regex, $line, $matches);
    $results[$matches['word']] = $matches['number'];
}

var_dump($results);

查看这里的工作情况:

您需要查看数组的大小,只选择最后一个作为数字,其余的作为名称

foreach ($array as $item) {
    $expl = explode(' ', $item);
    $name = implode(" ", array_slice($expl,0,-1));
    $number = $expl[count($expl)-1];

    echo $name."=". $number.PHP_EOL;
}
试试这个

foreach ($array as $item) {
    $expl = explode(' ', $item);
    $division=array_pop($expl);
    $name = $expl[0].' '.$expl[1];
    $number = $division;
}

参加聚会有点晚了,但我想提出我的建议。 如果所需的
$number
始终位于数组的末尾,则无论
$name
有多大

$newArray = array();
foreach ($array as $item) {
    $expl = explode(' ', $item);
    //get the size of the array
    $last= count($expl)-1;

    //get the final element in the array
    $number = $expl[$last];

    //remove the last element from array
    unset($expl[$last]);

    //glue it back together
    $name = implode(" ",$expl);

    //set the name + number in our array
    $newArray[]=array('name' => $name,'number'=>$number);

}


print_r($newArray);

//Output:
/* Array ( 
        [0] => Array ( 
            [name] => aaa 
            [number] => 3 
        )
        [1] => Array ( 
            [name] => bbb 
            [number] => 15 
        ) 
        [2] => Array ( 
            [name] => ccccc 
            [number] => 3A 
        ) 
        [3] => Array (
            [name] => dddd 
            [number] => 2412 
        ) 
        [4] => Array ( 
            [name] => eee fff 
            [number] => 15 
        ) 
        [5] => Array ( 
            [name] => ggg 
            [number] => 612 
        ) 
        [6] => Array ( 
            [name] => hhh iiiii 
            [number] => 23B 
        )       
    ) 
*/

如果数组大小超过2-内爆项。使用预期的输出,您只需覆盖您的值。为什么
'eeeff15',
=>$name='eee'$数字='15';`但是
'hhh iiii 23B',=>$name='hhh iiii'$数字='23B'?你不认为它应该是:——
'hhhiiiiiiiiiii23b',=>$name='hhh'$数字='23B'取决于数据-但如果您确实有“2aaa 1B”或“aaaa 1 1B”,则会出现问题,但在他的用例中没有提到这些问题,因此我没有将其作为要求。不过,对正则表达式进行调整是非常简单的。人们经常会立即使用他们必须掌握的示例,但在两个小时后,当它开始在野外出错时,他们突然改变了他们的要求。这就是为什么有时你不得不尝试预测这种变化的原因,为什么要编写有人为限制的代码?
$newArray = array();
foreach ($array as $item) {
    $expl = explode(' ', $item);
    //get the size of the array
    $last= count($expl)-1;

    //get the final element in the array
    $number = $expl[$last];

    //remove the last element from array
    unset($expl[$last]);

    //glue it back together
    $name = implode(" ",$expl);

    //set the name + number in our array
    $newArray[]=array('name' => $name,'number'=>$number);

}


print_r($newArray);

//Output:
/* Array ( 
        [0] => Array ( 
            [name] => aaa 
            [number] => 3 
        )
        [1] => Array ( 
            [name] => bbb 
            [number] => 15 
        ) 
        [2] => Array ( 
            [name] => ccccc 
            [number] => 3A 
        ) 
        [3] => Array (
            [name] => dddd 
            [number] => 2412 
        ) 
        [4] => Array ( 
            [name] => eee fff 
            [number] => 15 
        ) 
        [5] => Array ( 
            [name] => ggg 
            [number] => 612 
        ) 
        [6] => Array ( 
            [name] => hhh iiiii 
            [number] => 23B 
        )       
    ) 
*/