dream752614590 2019-03-19 10:34
浏览 240
已采纳

如何在php中更改关联数组值

$arr = array(
  0 => array(
        0 => 'one',
        1 => 'two',
        2 => 'three'
        ),
  1 => array(
        0 => 'sun',
        1 => 'mon',
        2 => 'tues'
        ),
  2 => array(
        0 => 'jan',
        1 => 'feb',
        2 => 'march'
        )
  );

This is array, I want to result like this. An array has a sub-array and wants to change the value of subarray.

$arr = array(
  0 => array(
        0 => 'one',
        1 => 'sun',
        2 => 'jan'
        ),
  1 => array(
        0 => 'two',
        1 => 'mon',
        2 => 'feb'
        ),
  2 => array(
        0 => 'three',
        1 => 'tues',
        2 => 'march'
        )
  );

please provide me the solution. Thanks in advance!

  • 写回答

4条回答 默认 最新

  • douping3427 2019-03-19 10:57
    关注

    Try with this loop to handle your requirement

    output

    Array ( [0] => Array ( [0] => one [1] => sun [2] => jan ) [1] => Array ( [0] => two [1] => mon [2] => feb ) [2] => Array ( [0] => three [1] => tues [2] => march ) )

    CODE

    <?php 
    $arr = array(
            0 => array(
                        0 => 'one',
                        1 => 'two',
                        2 => 'three'
                        ),
            1 => array(
                        0 => 'sun',
                        1 => 'mon',
                        2 => 'tues'
                        ),
            2 => array(
                        0 => 'jan',
                        1 => 'feb',
                        2 => 'march'
                        )
            );
        $newarray = [];
        $length = count($arr[0]); 
        foreach ($arr as $key => $value) {
            for ($i=0; $i < $length; $i++) { 
                $newarray[$i][$key] = $value[$i];
    
            }
            //break;
        }
        print_r($newarray);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

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