Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用foreach循环外部数组中的变量_Php_Html - Fatal编程技术网

Php 使用foreach循环外部数组中的变量

Php 使用foreach循环外部数组中的变量,php,html,Php,Html,我试图显示数组中的变量。我使用的是foreach循环,但是我需要在循环之前显示$order['campaign\u name',以便它只显示一次。我该怎么做?如果我将其更改为$orders['campaign_name'],我会得到一个未定义的索引错误 <div class="table-responsive"> <table class="table" id="component-table"> <?php

我试图显示数组中的变量。我使用的是foreach循环,但是我需要在循环之前显示$order['campaign\u name',以便它只显示一次。我该怎么做?如果我将其更改为$orders['campaign_name'],我会得到一个未定义的索引错误

  <div class="table-responsive">
            <table class="table" id="component-table">
              <?php if ($orders) { ?>
              <?php foreach ($orders as $order) { ?>
              <thead>
                <tr>
                  <td colspan=100%><h3><?php echo $order['campaign_name']; ?></h3></td>
                </tr>
              </thead>

              <tbody>
                <tr class="campaign-list" id="campaign-list">
                  <td><?php echo $order['component_name']; ?></td>
                  <td><?php echo $order['component_owner']; ?></td>
                  <td><?php echo $order['component_date']; ?></td>
                  <td><?php echo $order['campaign_code']; ?></td>
                </tr>
                <?php } ?>
                <?php } else { ?>
                <tr>
                  <td class="text-center" colspan="8"><?php echo $text_no_results; ?></td>
                </tr>
                <?php } ?>
              </tbody>
            </table>
          </div>

您需要知道要显示数组的哪个索引,但如果要显示数组的第一个索引,可以使用
$orders[0]['component\u name']

  <div class="table-responsive">
            <table class="table" id="component-table">
              <?php if ($orders) { ?>
              <thead>
                <tr>
                  <td colspan=100%><h3><?php echo $orders[0]['campaign_name']; ?></h3></td>
                </tr>
              </thead>
              <tbody>
              <?php foreach ($orders as $order) { ?>    
                <tr class="campaign-list" id="campaign-list">
                  <td><?php echo $order['component_name']; ?></td>
                  <td><?php echo $order['component_owner']; ?></td>
                  <td><?php echo $order['component_date']; ?></td>
                  <td><?php echo $order['campaign_code']; ?></td>
                </tr>
                <?php } ?>
                </tbody>
                <?php } else { ?>
                <tbody>
                <tr>
                  <td class="text-center" colspan="8"><?php echo $text_no_results; ?></td>
                </tr>
                </tbody>
                <?php } ?>
            </table>
          </div>

您的活动名称继续呼应,因为它位于您的foreach循环中;如果希望它只显示一次,请确保将其放置在foreach循环上方

这里有一个不太混乱的例子可以学习:

<table>
...
<?php
if ($orders): 
    echo $order['campaign_name'];
    foreach($orders as $order):
        echo '...'; // other components
    endforeach;
else:
    echo $text_no_results;
endif;
?>
...
</table>

...
...

您试图获取循环之前不存在的值。您正在直接调用VALUE。 轻松地将索引值放在数组后面 echo$订单[0]['campaign_name']
它将打印您的值。

变量超出范围,因此您无法引用它。您可以在循环上方,
echo$orders[0]['campaign\u name']
将其放置在
foreach
中,而不是将其放置在
中?