doukuibi9631 2017-07-10 17:42
浏览 40
已采纳

修改多维数组的当前值

I have three arrays, say multiarray, valsarray, and otherarray. otherarray is a multidimensional array that supplies values to multiarray and valsarray, but besides that it is unimportant here. valsarray takes values from a subarray of each value in otherarray and multiarray takes straight values from otherarray, as demonstrated below:

foreach($otherarray as $other){
    foreach($other as $sub){
        $valsarray[] = $sub
    }
    $multiarray[] = array('Val1' => $other['Val1'], 'Val2' => $other['Val2']);
}

Now what I would like to do is append each key/value pair in valsarray to the current array entry of multiarray, to achieve a result similar to:

$multiarray = array('Val1' => $other['Val1'], 'Val2' => $other['Val2'], 
'VALSARRAY_KEY1' => VALSARRAY_VALUE1, ..., 'VALSARRAY_KEYN' => VALSARRAY_VALUEN)

I have attempted to solve this using current in the following fashion:

foreach($valsarray as $key => $val){
    current($multiarray)[$key] = $val;
}

But the multiarray remained unaltered. I may be misunderstanding how current works, or how to approach this problem, so any help or direction would be appreciated.

EDIT- EXAMPLE

otherarray = array(...prior array entries...,
                   array('Val1' => 'abc',
                         'Val2' => 'cde',
                         'Val3' => 'not important',
                         'Val4' => array(0 => 'subA', 1 => 'subB'),
                   ...next array entries...);

BEFORE MERGE:
multiarray = array(...prior entries...,
                   array('Val1' => 'abc',
                         'Val2' => 'cde'));
valsarray = array(0 => 'subA', 1 => 'subB');

AFTER MERGE:
multiarray = array(...prior entries...,
                   array('Val1' => 'abc',
                         'Val2' => 'cde',
                          0 => 'subA',
                          1 => 'subB'));

So if multiarray was a regular array instead of a multidimensional one, I would do something like:

foreach($valsarray as $key => $val){
    $multiarray[$key] = $val;
}

To achieve the end result.

  • 写回答

1条回答 默认 最新

  • douqu2712 2017-07-10 18:35
    关注

    I am not 100% sure what you are trying to accomplish a Minimal, Complete, and Verifiable example may help if I have misunderstood something.

    It appears that the current() function does not work as you assume. (Or more specifically, the internal pointer.) If you look at the example in the PHP documentation: Current(), you will see that for current($array) to change elements, you need to call next($array) or prev($array). These function move the internal pointer of the array.

    Note that in PHP 5, foreach loops use the internal pointer (and reset it when you start a loop), but in PHP 7, foreach loops do not use the internal pointer.

    Anyway, here is my best guess at what could help you.

    $valsarray_index = 0;
    foreach ($otherarray as $other) {
        $multiarray_value = array('Val1' => $other['Val1'], 'Val2' => $other['Val2']);
        foreach ($other as $sub) {
            $multiarray_value[$valsarray_index] = $sub;
            // $multiarray_value["VALSARRAY_KEY" . $valsarray_index] = $sub;
            $valsarray[] = $sub;
    
            $valsarray_index += 1; // This stays in lockstep with the last index of $valsarray
        }
        $multiarray[] = $multiarray_value;
    }
    

    I am not exactly sure about what you want the final output to look like. If this produces incorrect information, then if would be helpful to provide some specific arrays for input and what you expect as output.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示