foreach - 从第二个或第三个数组中获取特定值

时间:2016-04-26 15:47:11

标签: php arrays foreach

大家好,谢谢你帮我... 我同时有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的数组。我希望从第一个(1)或第二个(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; ?>

2 个答案:

答案 0 :(得分:0)

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

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

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

您可以使用循环内的$ item [&#39;字段&#39;]来访问它。或$ items [此处的数组编号(0,1,2)] [&#39;字段&#39;]。

我相信当你说ROW时你意味着COLUMN。

答案 1 :(得分:0)

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

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();
}

很难回答(和测试),不知道什么数据看起来像,但也许一些想法。我现在支持我的第一次投票。