【问题标题】:PHP looping over multidimensional array with keyPHP用键循环遍历多维数组
【发布时间】:2016-04-20 07:24:18
【问题描述】:

我有一个数组,其中包含不同的数组和值。我想在内部循环这些数组以获取所有值,但由于某种原因它只遍历第一个数组。

数组如下所示: 这个数组的名字是slots。

Array
(
    [41] => Array
        (
            [0] => Array
                (
                    [attractie] => attractie1
                    [start] => 0930
                    [end] => 1200
                    [personen] => 
                    [catering] => 1
                    [bedrijfsnaam] => attractie1
                    [link] => http:
                    [color] => dd0330
                )

            [1] => Array
                (
                    [attractie] => attractie1
                    [start] => 1000
                    [end] => 1230
                    [personen] => 
                    [catering] => 1
                    [bedrijfsnaam] => Bedrijf2
                    [link] => http:
                    [color] => e49fca
                )
        )

    [52] => Array
        (
            [0] => Array
                (
                    [attractie] => attractie2
                    [start] => 0930
                    [end] => 1030
                    [personen] => 
                    [catering] => 1
                    [bedrijfsnaam] => Bedrijf4
                    [link] => http:
                    [color] => f7e300
                )

            [1] => Array
                (
                    [attractie] => attractie2
                    [start] => 0930
                    [end] => 1030
                    [personen] => 
                    [catering] => 0
                    [bedrijfsnaam] => bedrijf5
                    [link] => http:
                    [color] => f78f1e
                )

        )

)

这就是我的循环的样子:

$i=0;
foreach($slots[$attractieIDs[$i]] as $s){

        $myOrders[] = array( 'attractie' => $s['attractie'],
                             'name' => $s['bedrijfsnaam'],
                             'start' => $s['start'],
                             'end' => $s['end'],
                             'link' => $s['link'], 
                             'personen' => $s['personen'],
                             'catering' => $s['catering'],
                             'color' => $s['color'],
                           ); 
         $i++;
}

attractieID 是一个数组,其中 id 为(41 和 52)。

当我打印出 $myOrders 时,我只能看到 id 为 41 的数组的值,它不会转到具有新 id 的下一个数组。

有人知道我该如何解决这个问题吗?

非常感谢!

【问题讨论】:

  • 你想要的输出是什么?
  • 我也想输出 id 为 52 的数组的值
  • 但是它应该看起来究竟是什么
  • 只是我的大 $slots 数组中所有数组中每个项目的所有值(名称、吸引力等)
  • 你为什么不在你的问题中加入想要的数组结构呢?

标签: php arrays loops multidimensional-array


【解决方案1】:

您当前的代码会将 41 和 52 中的条目合并到一个数组中,您将无法分辨哪个是哪个。

$sourceArray = .... your source array here :)
$attractieIDs = array(41, 52);

foreach($attractieIDs as $id) {
   foreach($sourceArray[$id] as $attr) {
     $myOrders[] = $attr;
   }
}

【讨论】:

  • 我收到 Illegal offset type 和无效参数 supplied for foreach() 错误
  • 忘记了一级。立即尝试。
  • @MarcinOrlowski,我撤回了我之前的评论,但是有一些问题:你应该只需要 2 个循环。现在你只会得到值,没有键,因为 $s 不是一个数组。
  • 这似乎是错误的(除非 OP 在表达上非常不清楚),因为这并没有给你键“attractie”、“bedrijfsnaam”、“start”......只有它们的值是在一个大数组中返回。我把你的代码here.
  • 也不会因为$attractieIDs = array(41, 52);而受到限制吗?如果会有很多其他索引会发生什么?有多少索引将被硬编码?
【解决方案2】:

你可以用这个:

foreach ($attractieIDs as $id) {
    foreach ($slots[$id] as $s) {
        $myOrders[] = $s;
    }
}

看到它在eval.in上运行

【讨论】:

    【解决方案3】:

    试试这个循环:

    foreach($slots as $outer_arr){
    
        foreach($outer_arr as $s) {
            $myOrders[] = array( 'attractie' => $s['attractie'],
                  'name' => $s['bedrijfsnaam'],
                  'start' => $s['start'],
                  'end' => $s['end'],
                  'link' => $s['link'], 
                  'personen' => $s['personen'],
                  'catering' => $s['catering'],
                  'color' => $s['color'],
            ); 
    
        }
    
    
    }
    

    但仍不清楚您想要的输出。

    【讨论】:

    • $outer_arr 应该是什么?
    • $outer_arr 将包含索引为4152 的数组以及一个用于迭代数组的foreach 循环。
    • 如果可以加array variable而不是array output我可以测试一下。
    【解决方案4】:

    试试这个。不管你有什么索引,这个循环都会输出你数组中的任何值。

    <?php
    $test = array(
        '41' => array( 
            array('attractie' => 'attractie1',
                 'start' => '0930',
                 'end' => '1200',
                 'personen' => NULL,
                 'catering' => '1',
                 'bedrijfsnaam' => 'attractie1',
                 'link' => 'http:',
                 'color' =>'dd0330'),
            array('attractie' => 'attractie1',
                 'start' => '1000',
                 'end' => '1230',
                 'personen' => NULL,
                 'catering' => '1',
                 'bedrijfsnaam' => 'Bedrijf2',
                 'link' => 'http:',
                 'color' =>'e49fca'),
                ),
        '51' => array( 
            array('attractie' => 'attractie2',
                 'start' => '0930',
                 'end' => '1030',
                 'personen' => NULL,
                 'catering' => '1',
                 'bedrijfsnaam' => 'Bedrijf4',
                 'link' => 'http:',
                 'color' =>'f7e300'),
            array('attractie' => 'attractie2',
                 'start' => '0930',
                 'end' => '1030',
                 'personen' => NULL,
                 'catering' => '0',
                 'bedrijfsnaam' => 'bedrijf5',
                 'link' => 'http:',
                 'color' =>'f78f1e'),
                )
        );
    
    foreach ($test as $a => $val) {
        echo "<b>Index $a</b><br><br>";
        foreach ($val as $b) {
            foreach ($b as $key => $value) {
                echo "<b>$key</b> - $value<br>";
            }
        }
        echo "<br><br>";
    }
    ?>
    

    输出:

    Index 41
    
    attractie - attractie1
    start - 0930
    end - 1200
    personen - 
    catering - 1
    bedrijfsnaam - attractie1
    link - http:
    color - dd0330
    attractie - attractie1
    start - 1000
    end - 1230
    personen - 
    catering - 1
    bedrijfsnaam - Bedrijf2
    link - http:
    color - e49fca
    
    
    Index 51
    
    attractie - attractie2
    start - 0930
    end - 1030
    personen - 
    catering - 1
    bedrijfsnaam - Bedrijf4
    link - http:
    color - f7e300
    attractie - attractie2
    start - 0930
    end - 1030
    personen - 
    catering - 0
    bedrijfsnaam - bedrijf5
    link - http:
    color - f78f1e
    

    【讨论】:

      猜你喜欢
      • 2012-04-21
      • 2011-01-02
      • 2016-02-16
      • 2021-09-17
      • 1970-01-01
      • 2010-10-24
      相关资源
      最近更新 更多