dow66098 2018-05-14 05:00
浏览 17
已采纳

处理多宗派数组到数组索引作为数组

I have multidimensional array in php this is array format I need to handle this data in loop and want each array value single time with its index.

Array
(
    [name] => Array
        (
            [0] => third.png
            [1] => second.png
            [2] => first.png
        )

    [type] => Array
        (
            [0] => image/png
            [1] => image/png
            [2] => image/png
        )

    [tmp_name] => Array
        (
            [0] => /opt/lampp/temp/phpOPmQcB
            [1] => /opt/lampp/temp/phpk0j1yp
            [2] => /opt/lampp/temp/phpS2mhVd
        )

    [error] => Array
        (
            [0] => 0
            [1] => 0
            [2] => 0
        )

    [size] => Array
        (
            [0] => 179440
            [1] => 102238
            [2] => 75915
        )

)

I want to handle multidimensional array with new index for example this multidimensional array in core php please help me out

array(
    Array
   (
    [name] => third.png
    [type] => image/png
    [tmp_name] => /opt/lampp/temp/phpOPmQcB
    [error] => 0
    [size] => 179440
   )
    Array
  (
    [name] => second.png
    [type] => image/png
    [tmp_name] =>/opt/lampp/temp/phpk0j1yp
    [error] => 0
    [size] => 102238
  )
    Array
  (
    [name] => first.png
    [type] => image/png
    [tmp_name] => /opt/lampp/temp/phpS2mhVd
    [error] => 0
    [size] => 75915
  )
)
  • 写回答

1条回答 默认 最新

  • dongyu5104 2018-05-14 06:08
    关注

    You can get the keys of the array with array_keys.
    Then you need to loop the count of one of the subarrays. I get the count from the first subarray using the $keys variable.

    I nest a for and a foreach to build the array you expect.
    This method is dynamic and will work with a larger input array both on number of subarrays and number of items.

    $keys = array_keys($arr);
    $count = count($arr[$keys[0]]);
    
    for($i = 0; $i<$count; $i++){
        foreach($keys as $val){
            $new[$i][$val] = $arr[$val][$i];
        }
    }
    
    var_dump($new);
    

    Output:

    array(3) {
          [0]=>
          array(5) {
                ["name"] => "third.png"
                ["type"] => "image/png"
                ["tmp_name"] => "/opt/lampp/temp/phpOPmQcB"
                ["error"] => 0 
                ["size"] => 179440
          }
          [1]=>
          array(5) {
                ["name"] => "second.png"
                ["type"] => "image/png"
                ["tmp_name"] => "/opt/lampp/temp/phpk0j1yp"
                ["error"] => 0
                ["size"] => 102238
          }
          [2]=>
          array(5) {
                ["name"] => "first.png"
                ["type"] => "image/png"
                ["tmp_name"] => "/opt/lampp/temp/phpS2mhVd"
                ["error"] => 0
                ["size"] => 75915
          }
    }
    

    https://3v4l.org/4g40N

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

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化