dongqiang2358 2009-06-22 20:12
浏览 15
已采纳

在多维数组中移动键顺序

How can I "reorder" the keys in a multi-array? For example, I have:

$arr["abc"][0] = "val1";
$arr["abc"][1] = "val2";
$arr["abc"][2] = "val3";
$arr["xyz"][0] = "val4";
$arr["xyz"][1] = "val5";
$arr["xyz"][2] = "val6";

And I want it to be:

$arr[0]["abc"] = "val1";
$arr[0]["xyz"] = "val4";
$arr[1]["abc"] = "val2";
$arr[1]["xyz"] = "val5";
$arr[2]["abc"] = "val3";
$arr[2]["xyz"] = "val6";

My current effort is:

foreach ($arr as $param => $num) foreach ($num as $val) $newArr[$num][$param] = $val;

but it doesn't seem to be working. Any help would be appreciated.

EDIT: Specifically, I'm trying to loop through all elements submitted on $_FILES, as they all need the same thing done to them. By default, when there is more than one <input type="file" name="filedata[]" /> in a form, they go to $_FILES["filedata"]["name"][$index]. ("name" or any other parameter), so I cant just loop through every $_FILES["filedata"] to get at everything; thus, I need the keys of $_FILES["filedata"] reversed.

  • 写回答

3条回答 默认 最新

  • dongqiaochi2711 2009-06-22 20:19
    关注

    I think you need to still grab the keys in the second foreach. Try something like:

    foreach($arr as $k1 => $v1) {
      foreach ($v1 as $k2 => $v2) {
        $newArray[$k2][$k1] = $v2
      }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥60 pb数据库修改或者求完整pb库存系统,需为pb自带数据库
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路