向用户显示数组结果

时间:2017-02-20 20:16:54

标签: php arrays

我已经看到并尝试了很多关于显示数组结果的例子给用户,但没有一个对我有效。

我想,最接近我要找的是:

How to display array result

再次尝试过,但它没有用。

它不起作用 - 意味着页面空白。

当我尝试varDump(...)和print_r(...)时,我得到了结果,但我似乎无法向用户显示结果。

有谁知道我需要对下面的代码进行哪些修改才能显示sourcename,sourceaddress1和income1的结果?

$rowIDs = $_POST['rowIDs'];
if (is_array($rowIDs) || is_object($rowIDs))
{
foreach ($rowIDs as $id) {
 $sourcename1 = $_POST['sourcename1'. $id];
 $sourceaddress1 = $_POST['sourceaddress1'. $id];
 $income1 = $_POST['income1'. $id];
 echo $id;
 echo $sourcename;
 echo $sourceaddress1;
 echo $income1;
}

[rowIDs] => 1
[sourcename1] => Array
    (
        [0] => Jane Doe
    )

[sourceaddress1] => Array
    (
        [0] => 123 Main Street
    )

[income1] => Array
    (
        [0] => $89,000.00
    )

' //标记,

<script id="row-template" type="text/x-handlebars-template">
<div>
      <!--reseed attribute IDs in case of gap resulting from deletions -->
     <input type="hidden" name="rowIDs[]" value="{{rowNumber}}" />
<div class="form-group">

    <input type="text" name="sourcename1{{rowNumber}}" id="sourcename1{{rowNumber}}" value="" class="required requiredField" />
    <?php if($nameError != '') { ?>
        <span class="error"><?=$nameError;?></span>
    <?php } ?>
</div>
<div class="form-group">
    <input type="text" name="sourceaddress1{{rowNumber}}" id="sourceaddress1{{rowNumber}}" style="width:250px;" class="form-control" value="" class="required requiredField" />
    <?php if($nameError != '') { ?>
        <span class="error"><?=$nameError;?></span>
    <?php } ?>
</div>
<div class="form-group">
    <input type="text" style="width:250px;"  class="form-control" name="income1{{rowNumber}}" id="income1{{rowNumber}}" value="<?php if(isset($_POST['spouseIncome{{rowNumber}}'])) echo $_POST['spouseIncome{{rowNumber}}'];?>" class="required requiredField" />
    <?php if($nameError != '') { ?>
        <span class="error"><?=$nameError;?></span>
    <?php } ?>
    </div>
    <input id="Button{{rowNumber}}" type="button" rel="remove-row" value="Remove" />
</div>
</script>
<div id="addrow">
    <div>
    <!--reseed attribute IDs in case of gap resulting from deletions -->
       <input type="hidden" name="rowIDs[]" value="{{rowNumber}}" />
    <div class="form-group">
    <label for="sourcename">Name</label><br>
            <input type="text" name="sourcename1[]" id="sourcename1" value="" class="required requiredField" />
            <?php if($nameError != '') { ?>
                <span class="error"><?=$nameError;?></span>
            <?php } ?>
        </div>
        <div class="form-group">
            <label for="sourceaddress1">Address</label><br>
            <input type="text" name="sourceaddress1[]" id="sourceaddress1" style="width:250px;" class="form-control" value="" class="required requiredField" />
            <?php if($nameError != '') { ?>
                <span class="error"><?=$nameError;?></span>
            <?php } ?>
        </div>
        <div class="form-group">
          <label for="income1">Income</label><br>
            <input type="text" name="income1[]" id="income1" style="width:250px;"  class="form-control" value="" class="required requiredField" />
            <?php if($nameError != '') { ?>
                <span class="error"><?=$nameError;?></span>
            <?php } ?>
<input type="button" value="Add More" rel="add-row" />
    </div>
</div>
</div><br><br>

2 个答案:

答案 0 :(得分:1)

$_POST['rowids']是一个数字,而不是一个数组,因此foreach ($_POST['rowids'] as $id)是错误的。我猜它包含其他输入数组中的元素数,所以它应该是:

for ($id = 0; $id < $rowIDs; $id++)

您也不应该有if (is_array($postIDs) || is_object($postIDs)),因为当此输入是数字时代码应该运行。

其他输入是数组,因此您需要使用数组索引来访问它们,而不是字符串连接。

$sourcename1 = $_POST['sourcename1'][$id];

和其他输入类似。

答案 1 :(得分:0)

sourcename1,sourceaddress1和income1是包含一个项目的数组。例如,$sourcename[0]将返回Jane Doe

有关数组please read the documentation的更多信息。