【问题标题】:foreach - Get specific value from second or third arrayforeach - 从第二个或第三个数组中获取特定值
【发布时间】:2023-03-20 23:10:01
【问题描述】:

大家好,感谢您帮助我... 我同时有 1 个数据库和 3 个查询。每个查询都选择不同的年份并从该列中获取数据。

这是我的代码

$items = array();

      // while we still have rows from the db, display them
while ($row = mysql_fetch_array($result1)) {

    $items[] = $row;
}

while ($row = mysql_fetch_array($result2)) {

    $items[] = $row;
}

while ($row = mysql_fetch_array($result3)) {

    $items[] = $row;
}


mysql_close($connect);

?>

<?php foreach($items as $key => $item) : ?>

    <?php print "I DONT KNOW WHAT TO DO"; ?>

<?php endforeach; ?>

当我输入时:

<?php echo print_r($keys); ?>

我可以看到我有一个由 0、1 和 2 组成的数组。我很想从 first(1) 或 second(2) 数组中获取特定的 ROW。

我正在努力解决,但我无法...

非常感谢大家!

编辑:

完整代码:

    <?php
    include ('db.php'); // for db details
    include ('functions.php');

    $connect = @mysql_connect($host,$username,$password) or die('<p class="error">Unable to connect to the database server at this time.</p>');
    if (!$connect) {
        die('Could not connect: ' . mysql_error());
    }



    @mysql_select_db($database,$connect) or die('<p class="error">Unable to connect to the database at this time.</p>');

    $query1 = "SELECT * FROM godina2015 WHERE mjesec='8' AND godina='2015'";
    $query2 = "SELECT * FROM godina2015 WHERE mjesec='7' AND godina='2015'";
    $query3 = "SELECT * FROM godina2015 WHERE mjesec='6' AND godina='2015'";

    $result1 = @mysql_query("$query1") or die('<p class="error">There was an unexpected error grabbing news from the database.</p>');
    $result2 = @mysql_query("$query2") or die('<p class="error">There was an unexpected error grabbing news from the database.</p>');
    $result3 = @mysql_query("$query3") or die('<p class="error">There was an unexpected error grabbing news from the database.</p>');


    $items = array();

          // while we still have rows from the db, display them
    while ($row = mysql_fetch_array($result1)) {

        $items[] = $row;
    }

    while ($row = mysql_fetch_array($result2)) {

        $items[] = $row;
    }

    while ($row = mysql_fetch_array($result3)) {

        $items[] = $row;
    }


    mysql_close($connect);

    ?>

    <?php foreach($items as $key => $item) : ?>

        <?php print_r ($item); ?>

    <?php endforeach; ?>

编辑#2

    <?php
    include ('db.php'); // for db details
    include ('functions.php');

    $connect = mysql_connect($host,$username,$password) or die('<p class="error">Unable to connect to the database server at this time.</p>');
    if (!$connect) {
        die('Could not connect: ' . mysql_error());
    }



    mysql_select_db($database,$connect) or die('<p class="error">Unable to connect to the database at this time.</p>');

    $query = "SELECT * FROM godina2015 WHERE mjesec IN(8,7,6) AND godina='2015'";

    $result = mysql_query("$query") or die('<p class="error">There was an unexpected error grabbing news from the database.</p>');

    $items = array();

          // while we still have rows from the db, display them
    while ($row = mysql_fetch_array($result)) {

        $items[] = $row;
    }

    mysql_close($connect);

    ?>

    <?php foreach($items as $key => $item) : ?>

        <?php echo $items[1]['AWAsistCTR2']; ?>

    <?php endforeach; ?>

【问题讨论】:

  • 如果您在 foreach 循环中执行 print_r($item); 会怎样?
  • 显示var_dump($items)。您需要准确显示/解释您的数据结构是什么样的。但考虑到您构建数组的方式,您的第一个查询结果将是 $items[0]...$items[n],第二个查询结果将是 $items[n+1] ... $items[n+m],等等......
  • @VincentG 我得到了所有数组的列表:[0] => 82 [id] => 82 [1] => 8 [mjesec] => 8 [2] => 2015.. ..等等
  • 您可能希望首先向我们展示您正在运行的实际查询。谁知道也许你在他们身上做了一些愚蠢的事情

标签: php arrays foreach


【解决方案1】:

在循环内部执行 var_dump($item)。

您将在浏览器中看到数组/对象的结构。

查找包含所需数据的字段(它可能与数据库中的列同名)。

您可以通过在循环中使用 $item['field'] 来访问它。或 $items[此处的数组编号(0,1,2)]['field'].

而且我相信当您说 ROW 时,您的意思是 COLUMN。

【讨论】:

【解决方案2】:

我的第一个答案……太激动了。 我有点知道您要做什么,因为有时当我使用代码“弄清楚”不同(动态)查询响应如何相互关联时,我必须做同样的事情。我通常嵌套结果,以便我有一些可以逻辑解析的关系。

while ($row = mysql_fetch_array($result1)) {
    $items[0][] = $row;
}

while ($row = mysql_fetch_array($result2)) {
    $items[1][] = $row;
}

while ($row = mysql_fetch_array($result3)) {
    $items[2][] = $row;
}

这将区分不同的记录集,以便您可以以任何您想要的方式解析数组,甚至在需要时递归。

(array)$newArray = parseArray($items);

从那里...

function parseArray($inArray) {
    (array)$outArray = Array();

    foreach($inArray as $element) {
        if('some condition with $element') {        //maybe $element['date']=10?
            $outArray[] = parseArray($element);
        } else {
            $outArray[] = $element;
        }
    }
    return $outArray();
}

在不知道数据是什么样子的情况下很难回答(和测试),但也许有一些想法。我现在准备迎接我的第一次投反对票。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-08-14
    • 2022-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-15
    • 2017-03-11
    相关资源
    最近更新 更多