首页 > 解决方案 > 在 PHP 中访问关联数组的内部数组

问题描述

我从 MySQL 数据库创建了一个非常简单的关联 ID 数组。

数组的内容有点像这样:

[0]=> array(1) { ["id"]=> int(2) } 
[1]=> array(1) { ["id"]=> int(3) } 
[2]=> array(1) { ["id"]=> int(4) } 
[3]=> array(1) { ["id"]=> int(15) } 
[4]=> array(1) { ["id"]=> int(17) } 
[5]=> array(1) { ["id"]=> int(18) } 
[6]=> array(1) { ["id"]=> int(3) } etc.

但是,出于使用数组的目的,我需要访问 ID 整数,以便它们可以用于标记父表中的行。但是,我创建的脚本似乎没有使用["id"]=> int(2),因此代码失败。

我的代码如下:

mysql代码:

  public function unused()
    {
        $query = "select person.id from person, thing, person_thing
WHERE flag = 1 AND person.id = person_thing.person_id AND person_thing.thing_id = thing.id and thing.name != 'used'
AND person.name NOT IN (select person.name from person, thing, person_thing WHERE person.id = person_thing.person_id
AND person_thing.thing_id = thing.id and thing.name = 'used')";
        $stmt = $this->Conn->prepare($query);
        $stmt->execute(array());
        return 
        $stmt->fetchAll(PDO::FETCH_ASSOC);

功能代码:

{
        $things = $thing->unused();
        $smarty->assign('thing_used', $things);
        for ($i = 0; $i <= count($things); $i++) {
            $gameView->iterateThings($things[$i]);
        }

迭代方法代码

Public function iterateThings($id)
    {
        $changeFlag = "UPDATE thingview SET flag = false WHERE  id= :id";
        $stmt = $this->Conn->prepare($changeFlag);
        $stmt->execute(array(':id' => $id));

    }

我有一个名为 setup 的视图,它的布尔标志全部设置为 true。我正在尝试使用此代码更改关联数组 $things 中所有 person.ids 的标志设置。天真地,我认为这已经足够了,但是每次我运行代码时,标志都保持不变。看起来数组的内容是:array(1) { ["id"]=> int(2) }我需要的是{ ["id"]=> int(2) }。我需要索引消失,只需要数组。我用谷歌搜索了这个广告,但没有成功。有任何想法吗?

标签: phpmysqlsmartyassociative-array

解决方案


我意识到我已经创建了一个多维数组......这让我很震惊。我相信在这一点上,需要对多维数组进行更多研究,如果我需要进一步的帮助,我会重新提出这个问题。


推荐阅读