【问题标题】:How to deal with empty array keys in a foreach that is inside a while loop?如何处理while循环内的foreach中的空数组键?
【发布时间】:2015-08-10 20:39:34
【问题描述】:

我有以下从 msqli 查询生成的数组。

Array
(
    [0] => 21
    [1] => AAAAA
    [2] => BBBBB
    [3] => 1234567
    [4] => aaa
    [5] => bbb
    [6] => ccc
    [7] => ddd
    [8] => 2015-01-19 03:31:33
    [9] => 1
    [10] => 5
    [11] => 5, - not set -;9, - not set -;10, - not set -;11, - not set -;14, - not set -;19, 12;20, mm_cb_on;21, - not set -;27, Noe;28, Pena;62, mm_cb_off
)

Array
(
    [0] => 22
    [1] => AAAAA
    [2] => BBBBB
    [3] => 1234567
    [4] => aaa
    [5] => bbb
    [6] => ccc
    [7] => ddd
    [8] => 2015-01-19 03:31:33
    [9] => 1
    [10] => 6
    [11] => 5, - not set -;9, - not set -;10, - not set -;11, - not set -;14, - not set -;20, mm_cb_on;21, - not set -;
)

Array
(
    [0] => 23
    [1] => AAAAA
    [2] => BBBBB
    [3] => 1234567
    [4] => aaa
    [5] => bbb
    [6] => ccc
    [7] => ddd
    [8] => 2015-01-19 03:31:33
    [9] => 1
    [10] => 7
    [11] => 5, - not set -;11, - not set -;14, - not set -;19, 23;20, mm_cb_on;21, - not set -;27, Noe;28, Pena;62, mm_cb_off
)

我正在像这样循环这些数据:

while ($row = mysqli_fetch_array($data)) {
    echo "<td nowrap>" .$row{'first_name'}. "</td>
          <td nowrap>" .$row{'last_name'}. "</td>
          .......";
}

当我到达数组中的最后一个键时,我正在遍历该数据,并像这样爆炸:

while ($row = mysqli_fetch_array($data)) {
    echo "<td nowrap>" .$row{'first_name'}. "</td>
          <td nowrap>" .$row{'last_name'}. "</td>
          .......";
    $custom_fields = $row[11];
    $fields = explode(";", $custom_fields);
    foreach ($fields as $keys) {
        $key = explode(',', $keys);

        var_dump($key);
    }
}

该 var 转储产生以下数组结构(对于前一个数组中的第一个键):

Array
(
    [0] => 5
    [1] =>  - not set -
)
Array
(
    [0] => 9
    [1] =>  - not set -
)
Array
(
    [0] => 10
    [1] =>  - not set -
)
Array
(
    [0] => 11
    [1] =>  - not set -
)
Array
(
    [0] => 14
    [1] =>  - not set -
)
Array
(
    [0] => 19
    [1] =>  12
)
Array
(
    [0] => 20
    [1] =>  mm_cb_on
)
Array
(
    [0] => 21
    [1] =>  - not set -
)
Array
(
    [0] => 27
    [1] =>  Noe
)
Array
(
    [0] => 28
    [1] =>  Pena
)
Array
(
    [0] => 62
    [1] =>  mm_cb_off
)

我正在尝试创建这样的条件:

if (isset($key[0]) && $key[0] == 19) { // this produce none for every row
    $prov_id = $key[1];
} else {
    $prov_id = 'None';
}

if ($key[0] == 27) {
    $fm2_fname = $key[1];
} else {
    $fm2_fname  = '';
}

if ($key[0] == 28) {
    $fm2_lname = $key[1];
} else {
    $fm2_lname  = '';
}

我遇到的问题是循环中的一些项目没有$key[0] == "19" ...我将如何在 foreach 循环中处理这个问题?非常感谢任何帮助。

【问题讨论】:

  • 没有$key[0] == "19"的物品怎么办?
  • 如果 $key[0] 不存在,那么你的爆炸失败了。所以...isset($key[0])?
  • 谢谢@Marc B。我明白这一点,但 $key[0] 将始终存在于数组中的某个位置,它并不总是等于 19 ...所以isset 永远是正确的。跨度>
  • 那到底是什么问题?
  • 我需要获取$key[1]的值,其中$key[0] == 19,如果没有等于19的$key[0],我需要提供一个空值。

标签: php mysql arrays foreach


【解决方案1】:
if (isset($key[0]) && $key[0] == 19) { // this produce none for every row
    $prov_id = $key[1];
} else {
    $prov_id = 'None';
}

if ($key[0] == 27) {
    $fm2_fname = $key[1];
} else {
    $fm2_fname  = '';
}

if ($key[0] == 28) {
    $fm2_lname = $key[1];
} else {
    $fm2_lname  = '';
}

因为您在每个语句上都有一个 else,该值将在循环时设置,因此在您的情况下,它将变量设置为 'None',然后将其设置为 $key[1]

改为摆脱} else { 并将变量更改为以下内容,以便每一行都有自己的值:

$row['prov_id']
$row['fm2_fname']
$row['fm2_lname']

所以现在最终的代码会是这样的:

if ($key[0] == 19)
    $row['prov_id'] = $key[1];

if ($key[0] == 27) 
    $row['fm2_fname'] = $key[1];

if ($key[0] == 28) 
    $row['fm2_lname'] = $key[1];

在 foreach 循环之外对每个变量执行此操作

if (!isset($row['prov_id'])) $row['prov_id'] = '';

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-14
    • 2017-05-28
    • 2011-10-19
    • 1970-01-01
    • 2018-02-09
    • 2015-02-07
    • 2013-10-01
    • 1970-01-01
    相关资源
    最近更新 更多