2016-04-20 148 views
0

我有一个数组与他们不同的数组与价值观。我想循环这些数组以获得所有值,但由于某种原因,它只能遍历第一个数组。PHP循环多维数组与键

以下是阵列的外观: 此阵列的名称是插槽。

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进入下一个数组。

任何人都知道我该如何解决这个问题?

非常感谢提前!

+0

什么是你想要的输出运行? –

+0

我想输出数组的值为52以及 –

+0

但它应该看起来**完全** –

回答

1

您当前的代码会将来自41和52的条目合并到一个数组中,并且您将无法确定哪个数组是哪个数组。

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

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

我得到一个'非法的偏移类型'和无效参数'提供给foreach()'错误 –

+0

忘记一个级别。现在试试。 –

+0

@MarcinOrlowski,我收回了我之前的评论,但是有一些错误:你应该只需要2个循环。现在你只能得到值,没有键,因为$ s不是数组。 – trincot

0

试试这个循环:

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'], 
     ); 

    } 


} 

但目前尚不清楚你想要的输出。

+0

$ outer_arr应该是什么? –

+0

$ outer_arr将包含索引为“41”和“52”的数组,以及一个用于遍历数组的foreach循环。 – Meathanjay

+0

如果你可以添加'数组变量'不'数组输出'我可以测试它。 – Meathanjay

0

试试这个。无论您有什么索引,此循环都会在您的数组内输出任何值。

<?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 
1

您可以使用此:

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

看到它在eval.in