php - 在 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) }。我需要索引消失,只需要数组。我用谷歌搜索了这个广告,但没有成功。有任何想法吗?
解决方案
我意识到我已经创建了一个多维数组......这让我很震惊。我相信在这一点上,需要对多维数组进行更多研究,如果我需要进一步的帮助,我会重新提出这个问题。
推荐阅读
- swift - XCode/Swift:将 Bash 输出解析为 Textwindow 的函数
- node.js - 服务器启动时的 Mongoose ValidationError
- angular - Angular CLI 生成组件问题
- python - 将多个参数传递给异步 - Python 多处理
- module - 是否可以 require() 使用 luaL_loadstring() 加载的脚本?
- python - 在 python 请求中为 mailjet 添加变量
- javascript - 通过键盘输入 2 个数字并使用 Math 显示最大/最小和功率等级?
- javascript - vue-cli 3.0 - 输出库代码拆分时未定义 jsonpArray.push
- ruby-on-rails - ruby 的 link_to 方法不适用于 shopify 嵌入式应用程序
- sql-server - 删除几个表的外键上的 ON DELETE CASCADE