PHP - 将一个字符串的每2个部分组合在一个数组中

时间:2015-11-25 15:19:59

标签: php arrays

我有一个像这样构造的长字符串:

randomstring number     randomstring number     randomstring number

我需要将这些随机字符串和数字组合在一起,以便得到这样的数组:

array = [[randomstring, number], [[randomstring, number], [randomstring, number]]

我不知道字符串和数字之间的空格量。有什么建议吗?

更新

自Edwin Moller回答以来,我现在已经离开了这个阵列:

Array (46) {

[0] => 
array(2) {
  [0]=>
  string(20) "string"
  [1]=>
  string(7) "number"
}
[1]=>
array(2) {
  [0]=>
  string(5) ""
  [1]=>
  string(7) ""
}

[2] => 
array(2) {
  [0]=>
  string(10) ""
  [1]=>
  string(11) ""
}

[3] => 
array(2) {
  [0]=>
  string(10) ""
  [1]=>
  string(11) ""
}

[4] => 
array(2) {
  [0]=>
  string(10) ""
  [1]=>
  string(11) ""
}

需要删除具有2个空元素的数组元素。

我会留下这个解决方案。它根本不优雅,但我不知道这些“空”字符串是什么。它不响应空格,空格,任何字符测试,所以我使用了strlen()函数:

$str = preg_replace('!\s+!', ' ', $longstring);
$parts = explode(" ", $str);
$nr = count($parts);

for ($i = 0; $i < $nr; $i = $i + 2) {
    if(strlen($parts[$i]) > 20) { // ugly, but it works for now..
        $tmp[] = [$parts[$i], $parts[$i + 1]];
    }
}

// unsetting these elements because they are longer than 30
unset($tmp[0]);
unset($tmp[1]);
unset($tmp[2]);

5 个答案:

答案 0 :(得分:1)

$longstring = "randomstring1 1001     randomstring2 205 randomstring3 58";
// First, take care of the multiple spaces.
$str = preg_replace('/\s+/', ' ', $longstring);

// split in parts on space
$parts = explode(" ",$str);
$nr = count($parts);

$tmp = array();
for ($i=0; $i<$nr; $i=$i+2){
  $tmp[] = array($parts[$i], $parts[$i+1]);
}


echo "<pre>";
print_r($tmp);
echo "</pre>";

您可能希望确保它是偶数。 (检查$ nr)。

编辑:OP说你在数组$ parts中有一些空元素。

我不知道导致这种情况的原因,可能是某些编码问题,如果没有原始材料(字符串)则不确定。 猜测:尝试utf8_decode原始字符串,然后执行preg_replace,然后执行print_r。 像这样:

$longstring = "randomstring1 1001     randomstring2 205 randomstring3 58";
$longstring = utf8_decode($longstring);
$str = preg_replace('/\s+/', ' ', $longstring);
$parts = explode(" ",$str);
echo "<pre>";
print_r($parts);
echo "</pre>";

答案 1 :(得分:1)

爆炸字符串,然后像这样检查

Online Demo

last

答案 2 :(得分:0)

尝试以下

server_name site1.org www.site1.org old.site1domain.org;

评论应该很好地解释这里发生的事情,它将为您留下以下内容:

//Our string
$string = "randomstring 11     randomstring 22     randomstring 33";

//Split them up if they have MORE THAN ONE space
$firstSplit = preg_split('/\s\s+/', $string);

//Set up a new array to contain them
$newArray = array();

//For each of the ones like "randomstring 11"
foreach($firstSplit as $key => $split){

    //Split them if they have ONE OR MORE space
    $secondSplit = preg_split('/\s+/', $split);

    //Add them to the array
    //Note: change this part to structure the array however you'd like
    $newArray[$key]["string"] = $secondSplit[0];
    $newArray[$key]["integer"] = $secondSplit[1];
}

答案 3 :(得分:0)

我不知道这是否是最短的方式但我会说你的问题可以包括这些步骤

1-将多个空格转换为一个空格

2-将大字符串分解为一个数组,其中包含所有子字符串

通过循环遍历大数组

对元素进行分组
<?php
$str = 'randomstring1 number1     randomstring2 number2     randomstring3 number3';
//remove the spaces and put all the element in one array
$arr  = explode(' ', $str);
$space = array('');
$arr = array_values(array_diff($arr, $space));

//now loop over the array and group elements
$result = $temArr = array();
foreach($arr as $index => $element){
  if($index % 2){//second element (number)
    $temArr[] =  $element;
    $result[] = $temArr;
    $temArr = array();
  }else{//first element (randomstring)
    $temArr[] =  $element;
  }
}

//print the result
var_dump($result);die;

?>  

答案 4 :(得分:0)

首先拆分2个或更多空格,然后用单个空格拆分这些组。 试试这个:

$input  = 'randomstring1 number1     randomstring2 number2     randomstring3 number3';
$groups = preg_split("/[\s]{2,}/", $input );
$result = array_map(function($i){
    return explode(' ', $i);
}, $groups);

这让我得到了这个结果:

array(3) {
    [0] = array(2) {
        [0] = string(13) "randomstring1"
        [1] = string(7) "number1"
    }
    [1] = array(2) {
        [0] = string(13) "randomstring2"
        [1] = string(7) "number2"
    }
    [2] = array(2) {
        [0] = string(13) "randomstring3"
        [1] = string(7) "number3"
    }
}