duanlao1552 2011-05-17 19:26
浏览 23
已采纳

如何使用另一个数组操作数组?

Hi I have a problem to manipulate an array with another array like the sample seen below.

Sample scenario

Array one contains elements A, B, C, D, E ordering is strict

B's position is constant, it will not change in any manipulation

Array two contain X, Y

I need to merge array two with array one, merging result array will be:

Result array: X, B, Y, A, C D E

  • A changed its position with X, because A's position isnot constant
  • B's positions cannot changed, because B's position is constant
  • C changed its position with Y, because C's position isnot constant
  • A and C is appended after changes

Note: More than one element's positiob can be constant

What do you suggest for this kind of problem?

tHanks

Edit:

Thanks to @mkilmanas, I changed his code it works in the scenario described above

$arrayOne   =   array(
        0   =>  array('val' =>  'A','is_const'  =>  0),
        1   =>  array('val' =>  'B','is_const'  =>  1),
        2   =>  array('val' =>  'C','is_const'  =>  0),
        3   =>  array('val' =>  'D','is_const'  =>  0),
        4   =>  array('val' =>  'E','is_const'  =>  0)
);

$arrayTwo   =   array(
        0   =>  array('val' =>  'X','is_const'  =>  0),
        1   =>  array('val' =>  'Y','is_const'  =>  0)
);

//Clone arrayOne
$loose = $arrayOne;

//Collect Fixed Elements
$fixed = array();
foreach ($arrayOne as $idx=>$val){
    if ($val['is_const'] == 1){
        $fixed[] = $idx;
    }
}


//Now remove fixed elements
//PHP doesn't reindex the array, so it is safe to use foreach here
foreach($fixed as $idx) { unset($loose[$idx]); } 


//since they are numeric-indexed, they will be concatenated
//and the order is inverted, so that $arrayOne is appended to the end of $arrayTwo
$combined = array_merge($arrayTwo, $loose); 

// And now we insert the fixed elements at their fixed positions
foreach($fixed as $idx) {
   array_splice($combined, $idx, 0, array($arrayOne[$idx]));        
}

Result of $combined is X B Y A C D E which is correct

tHank you very much

  • 写回答

3条回答 默认 最新

  • dsj2014 2011-05-17 19:47
    关注

    I would use a combination of array_merge() and array_splice(). Lets assume that $fixed contains an array of fixed element's keys (from $arrayOne).

    $loose = $arrayOne; // Copy initial array
    
    // Now remove fixed elements
    // PHP doesn't reindex the array, so it is safe to use foreach here
    foreach($fixed as $idx) { unset($loose[$idx]); } 
    
    // since they are numeric-indexed, they will be concatenated
    // and the order is inverted, so that $arrayOne is appended to the end of $arrayTwo
    $combined = array_merge($arrayTwo, $arrayOne); 
    
    // And now we insert the fixed elements at their fixed positions
    foreach($fixed as $idx) {
        array_splice($combined, $idx, 0, array($arrayOne[$idx]));        
    }
    // voila - $combined should now have the desired order
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误