PHP:将数组插入另一个数组

时间:2015-07-02 09:09:11

标签: php arrays arraylist insert array-merge

我试图规范化一串以逗号分隔的数字和一个范围。为了证明:

数组:

$array = ["1","2","5-10","15-20"];

应该成为:

$array = [1,2,5,6,7,8,9,10,15,16,17,18,19,20];

我正在研究的算法是:

//get the array values with a range in it :
$rangeArray = preg_grep('[-]',$array);

这将包含["5-10", "16-20"];然后:

foreach($rangeArray as $index=>$value){
    $rangeVal = explode('-',$value);
    $convertedArray = range($rangeVal[0],$rangeVal[1]);
}

转换后的数组现在将包含 [" 5"," 6"," 7"," 8",& #34; 9"" 10"]

我现在面临的问题是,如何弹出价值" 5-10"在原始数组中,并在 $ convertedArray 中插入值,以便我将获得值:

  

$ array = [" 1"," 2"," 5"," 6"," 7"" 8"" 9"" 10" 下," 16-20"];

那么,如何将数组插入数组?或者有更清洁的方法来解决这类问题吗?将数字和范围值的数组转换为正确排序的数字数组?

8 个答案:

答案 0 :(得分:5)

你走了。 我尽可能地尽量减少代码。

考虑下面的初始数组,

$array

如果您要创建一个新数组而不是$array,请将$array = call_user_func_array('array_merge', array_map(function($value) { if(1 == count($explode = explode('-', $value, 2))) { return [(int)$value]; } return range((int)$explode[0], (int)$explode[1]); }, $array)); 的第一次出现更改为您想要的任何名称,

$array

现在,$array = [1,2,5,6,7,8,9,10,15,16,17,18,19,20]; 变为,

15-20-25

注意:

  • 将每个转换成员转换为整数
  • 如果提供了15-20,请考虑15a-20b并忽略其余
  • 如果提供了15-20,将其视为-,则这是使用15a展开后套管为整数的结果,15变为0
  • <?php $array = ["1","2","5-10","15-20"]; $newdata = array(); foreach($array as $data){ if(strpos($data,'-')){ $range = explode('-', $data); for($i=$range[0];$i<=$range[1];$i++){ array_push($newdata, $i); } } else{ array_push($newdata, (int)$data); } } echo "<pre>"; print_r($array); echo "</pre>"; echo "<pre>"; print_r($newdata); echo "</pre>";
  • 开始,将数组键转换为数字升序
  • 仅当给定数组按单个成员和范围成员的升序排列
  • 时,才会对新数组进行排序

答案 1 :(得分:5)

试试这个:

Array
(
    [0] => 1
    [1] => 2
    [2] => 5-10
    [3] => 15-20
)
Array
(
    [0] => 1
    [1] => 2
    [2] => 5
    [3] => 6
    [4] => 7
    [5] => 8
    [6] => 9
    [7] => 10
    [8] => 15
    [9] => 16
    [10] => 17
    [11] => 18
    [12] => 19
    [13] => 20
)

<强>结果:

var responsePromise = $http.get("./php/getdata.php");

responsePromise.success(function(data, status, headers, config) {
                   //Assign resultJSON to desired variable
                });

问题解决了!

答案 2 :(得分:3)

更简单,更简短的答案。

<强> See in Ideone

$new_array = array();  

foreach($array as $number){
  if(strpos($number,'-')){
     $range = explode('-', $number);  
     $new_array = array_merge($new_array, range($range[0],$range[1]));
  } 
  else{
     $new_array[] = (int) $number;
  }
}

var_dump($new_array);

答案 3 :(得分:3)

使用range和array_merge处理非数字值:

$array = ["1","2","5-10","15-20"];

$newArray = [];
array_walk(
    $array,
    function($value) use (&$newArray) {
        if (is_numeric($value)) {
            $newArray[] = intval($value);
        } else {
            $newArray = array_merge(
                $newArray,
                call_user_func_array('range', explode('-', $value))
            );
        }
    }
);

var_dump($newArray);

答案 4 :(得分:3)

更容易找出最小值和最大值并使用它们创建数组。这是一个例子:

$in = ["1","2","5-10","15-20"];
$out = normalizeArray($in);
var_dump($out);

function normalizeArray($in)
{
    if(is_array($in) && sizeof($in) != 0)
    {
        $min = null;
        $max = null;
        foreach($in as $k => $elem)
        {
            $vals = explode('-', $elem);
            foreach($vals as $i => $val)
            {
                $val = intval($val);
                if($min == null || $val < $min)
                {
                    $min = $val;
                }
                if($max == null || $val > $max)
                {
                    $max = $val;
                }
            }
        }

        $out = array();
        for($i = $min; $i <= $max; $i++)
        {
            $out[] = $i;
        }

        return $out;
    }
    else
    {
        return array();
    }
}

答案 5 :(得分:3)

在这里你去交配。

<?php
$array = ["1","2","5-10","15-20"];
$newArr = array();
foreach($array as $item){
    if(strpos($item, "-")){
        $temp = explode("-", $item);
        $first = (int) $temp[0];
        $last = (int) $temp[1];
        for($i = $first; $i<=$last; $i++){
            array_push($newArr, $i);
        }
    }
    else
        array_push($newArr, $item);
}
print_r($newArr);
?>

答案 6 :(得分:2)

试试这个:

$array = ["1","2","5-10","15-20"];
$result = [];

foreach ($array as $a) {
    if (strpos($a,"-")!== false){
        $tmp = explode("-",$a);
        for ($i = $tmp[0]; $i<= $tmp[1]; $i++) $result[] = $i;
    } else {
        $result[] = $a;
    }
}

var_dump($result);

答案 7 :(得分:1)

你没有完成一点

127.0.0.1