本帖最后由 edisonli 于 2012-09-06 11:35:14 编辑

解决方案 »

  1.   

    <?php
    $arr1=array(
        array(
            'id'=>1,
            'pos'=>1
        ),
        array(
            'id'=>2,
            'pos'=>2
        ),
        array(
            'id'=>3,
            'pos'=>3
        ),
        array(
            'id'=>4,
            'pos'=>4
        ),
        array(
            'id'=>5,
            'pos'=>5
        ),
        array(
            'id'=>6,
            'pos'=>6
        ),
    );$arr2=array(
        'id'=>7,
        'pos'=>4
    );function array_insert($arr1, $arr2)
    {
        $index = 1;
        $insert = false;
        foreach($arr1 as $v)
        {
            if ($v['pos'] == $arr2['pos']) {
                if ($v['id'] < $arr2['id']) {
                    array_splice($arr1, $index - 1, 0, array($arr2));
                    break;
                } else {
                    $insert = true;
                }
            } else if ($insert) {
                break;
            }
            $index ++;
        }
        if ($insert) {
            array_splice($arr1, $index - 1, 0, array($arr2));
        }
        return $arr1;
    }
    $arr1 = array_insert($arr1, $arr2);
    print_r($arr1);$arr2=array(
        'id'=>3,
        'pos'=>4
    );
    $arr1 = array_insert($arr1, $arr2);
    print_r($arr1);$arr2=array(
        'id'=>5,
        'pos'=>6
    );
    $arr1 = array_insert($arr1, $arr2);
    print_r($arr1);
    ?>
      

  2.   

    看来还是得掌握php的函数啊,array_splice这个都没用过,哎~
      

  3.   

    当$arr1=array(
        array(
            'id'=>1,
            'pos'=>1
        ),
        array(
            'id'=>2,
            'pos'=>2
        ),
        array(
            'id'=>3,
            'pos'=>3
        ),
        array(
            'id'=>4,
            'pos'=>5
        ),
        array(
            'id'=>5,
            'pos'=>5
        ),
        array(
            'id'=>6,
            'pos'=>6
        ),
    );$arr2=array(
        'id'=>7,
        'pos'=>4
    );时$arr1 = array_insert($arr1, $arr2);
    print_r($arr1);
    得到Array
    (
        [0] => Array
            (
                [id] => 1
                [pos] => 1
            )    [1] => Array
            (
                [id] => 2
                [pos] => 2
            )    [2] => Array
            (
                [id] => 3
                [pos] => 3
            )    [3] => Array
            (
                [id] => 4
                [pos] => 5
            )    [4] => Array
            (
                [id] => 5
                [pos] => 5
            )    [5] => Array
            (
                [id] => 6
                [pos] => 6
            ))插不进去哟
      

  4.   

     $arrays = array_chunk($arr1, 1);
     
     $result = array(array());
     
     $merge = array($arr2);
     
     foreach ($arrays as $array) {
     
      for($i = 0; $i < count($array); $i++) {
      if (($array[$i]['pos'] < $arr2['pos']) || ($array[$i]['pos'] > $arr2['pos'])) {
     
     
      $result = array_merge($result, $array);
     
      }elseif ($array[$i]['id'] < $arr2['id']) {
     
      $result = array_merge($result, $merge);
     
      $result = array_merge($result, $array);
      } else {
      $result = array_merge($result, $array);
      $result = array_merge($result, $merge);
      }
      }
     
     }
     
      $result = array_slice($result, 1);
      

  5.   

    矩阵数组交换一下行列的key值,用起来就很方便,而且其他情况例如求和都很有用,要善用这办法交换后arr结构为 array([id][整数],[pos][整数])这样的结构用array[pos]和arr2用“值”求交集(保留对应键),得到 arr3
    用array[id]和arr3用“整数键”求交集,得到arr4合并arr3和arr4,再排序就是你想要的了操作过程中注意不要干扰整数key的顺序就行
    代码略