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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?