时间:2021-07-01 10:21:17 帮助过:3人阅读
$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 )
array( 'id'=>4, 'pos'=>4 ),
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); ?>
------解决方案--------------------
当
$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 );