doumi1099 2014-01-09 22:01
浏览 20
已采纳

PHP中的动态数组索引

I don't know why I'm having trouble figuring this out, but I'm hoping someone can help.

I want to dynamically add multiple indices to an array. For example, if I have a list of IDs.

$ids = array(1247, 1248);

How can I do this dynamically, depending on the number of ids that are in the array?

$history['transactions'][$id1][$id2]['Thursday'] = 0;
  • 写回答

3条回答 默认 最新

  • douji2283 2014-01-09 22:11
    关注

    If I am unserstanding you correctly, then your code should work. unless you are trying to use the array your created, take a look. I think a simple change could solve your problem

    this would work:

    $ids = array(1247, 1248);
    $history['transactions'][$ids[0]][$ids[1]]['Thursday'] = 0;
    

    So would this:

    $id1 = 1247;
    $id2 = 1248;
    $history['transactions'][$id1][$id2]['Thursday'] = 0;
    

    From the looks of it, you just aren't calling anything. but without more of your code I can't be more help


    As Daedalus points out, to this point the code isn't really dynamic, (I assumed you just needed a snipit in the the middle to help with already dynamic code. but now I will assume the opposite) here is an example of how how to change that:

    say you had the array $ids = array(1247, 1248, 1249, 1250, 1251); you would need to loop through the lot of them and deal with them individually. Probably the easiest way to do that would be something like:

    $ids = array(1247, 1248, 1249, 1250, 1251);
    foreach($ids as $id){
        $history['transactions'][$id]['Thursday'] = 0;
    }
    

    Make sense?


    Third try: Getting a better understanding of whats going on. (sorry, took a long time to get it through my thick skull)

    So if you want an array with all the ids in order of the array $ids = array(1247, 1248, 1249, 1250, 1251); then this is the solution for you:

    $ids = array(1247, 1248, 1249, 1250, 1251);
    $result = array('Thursday' => 0);
    for($i = count($ids) -1; $i >= 0; $i--){
         $result = array($ids[$i] => $result);
    }
    $history = array('transactions' => $result);
    

    var_dump($history) yields:

    array(1) {
      ["transactions"]=>
      array(1) {
        [1247]=>
        array(1) {
          [1248]=>
          array(1) {
            [1249]=>
            array(1) {
              [1250]=>
              array(1) {
                [1251]=>
                array(1) {
                  ["Thursday"]=>
                  int(0)
                }
              }
            }
          }
        }
      }
    }
    

    Which is now what I believe you are looking for

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?