Php 带数组搜索的多维数组循环
我意识到有很多关于多维数组和foreach循环的问题,我花了几个小时通读它们,试图让我自己的循环工作——但没有成功。如果解决方案是重复的,我将删除我的问题(或者链接到另一个问题,如果这是首选) 现在,挑战是:Php 带数组搜索的多维数组循环,php,mysql,arrays,multidimensional-array,Php,Mysql,Arrays,Multidimensional Array,我意识到有很多关于多维数组和foreach循环的问题,我花了几个小时通读它们,试图让我自己的循环工作——但没有成功。如果解决方案是重复的,我将删除我的问题(或者链接到另一个问题,如果这是首选) 现在,挑战是: 使用返回的MYSQL结果数组。结果来自关联数组中的多个联接表。现在我需要将它转换为我需要的多维数组 我已经完成了大部分工作,但我的问题是循环并将新项目添加到数组中的正确位置 下面是一些代码: //example of how array is setup in the way I wa
- 使用返回的MYSQL结果数组。结果来自关联数组中的多个联接表。现在我需要将它转换为我需要的多维数组
- 我已经完成了大部分工作,但我的问题是循环并将新项目添加到数组中的正确位置
//example of how array is setup in the way I want, this part works.
foreach($results as $i => $a):
//some other code is here, see below.
$items[$i] = [
"id" => $a['id'],
"itemid" => $a['itemid'],
"name" => $a['name'],
"def" => $a['def'],
"class" => $a['class'],
"timeline" => $a['timeline'],
"files" => [
[0] => [
"id" => $a['fileid'],
"name" => $a['filename'],
"path" => $a['filepath'],
"type" => $a['filetype']
]
],
"tags" => [
[0] => [
"id" => $a['tagid'],
"name" => $a['tagname']
]
]
];
endforeach;
然后,我尝试了许多方法来循环,以便仅在项目“id”与上一个相同时添加到“tags”或“files”。以下是我的编辑器中的当前代码,不起作用:
//inside foreach loop, before above code
if($items[$i-1]['id'] == $a['id']):
//it is the same item, works to here.
if(in_array($a['filename'], $items[$i-1], FALSE)):
//add to files array for last item
$items[$i-1]['files'][] = [
"id" => $a['fileid'],
"name" => $a['filename'],
"path" => $a['filepath'],
"type" => $a['filetype']
];
elseif(in_array($a['tagname'], $items[$i-1], FALSE)):
//add to tags array for last item
$items[$i-1]['tags'][] = [
"id" => $a['tagid'],
"name" => $a['tagname']
];
endif;
else:// else it does the code above
如您所见,我最近的一次尝试是使用in_数组,我现在意识到它不适用于多维数组。我的问题是,我不知道如何确定它是同一项目的新文件还是新标记
最后,我想要一个包含多个“文件”和“标记”的“项目”数组。我将对json_进行编码,然后将其与JS一起使用
任何关于如何使其工作或优化的建议,将不胜感激
另外,正如我上面提到的,我知道以前有人问过这个问题——尽管我无法让他们的解决方案对我起作用。如果解决方案是重复的,我将删除此问题(如中所示,它对其他人没有真正的帮助)。谢谢你的帮助,非常感谢 不要使用“自动递增”数组索引,因为它们很容易出错。使用您的数据库id,因为它已经存在:
//example of how array is setup in the way I want, this part works.
foreach($results as $i => $a):
$items[$a['id']] = [ // THIS CHANGED.
"id" => $a['id'],
"itemid" => $a['itemid'],
...
现在,通过进一步的结果,您可以轻松检查阵列中是否已存在id:
if (isset($items[$a['id']])) {
// We had this id before, just add files/tags to it.
// Check first, if files subarray exists, if not: create it.
if (!isset($items[$a['id']]['files'])) {
$items[$a['id']]['files'] = array();
}
$items[$a['id']]['files'][] = array(...); // add the new file.
// Repeat for tags.
}
如果您的结果可以多次返回同一个文件作为id,您可以使用搜索功能检查文件名是否已经存在:
$filename = $a['filename'];
if (!searchFilename($filename, $items[$a['id']]['files']) {
// Filename is not in there, yet.
// Add file info.
}
function searchFilename($id, $array) {
foreach ($array as $key => $val) {
if ($val['filename'] === $id) {
return true;
}
}
return false;
}
同样的方法也适用于标签
最后,如果您不需要索引$items
的ID,只需调用:
$items = array_values($items);
不要使用“自动递增”数组索引,因为它们很容易出错。使用您的数据库id,因为它已经存在:
//example of how array is setup in the way I want, this part works.
foreach($results as $i => $a):
$items[$a['id']] = [ // THIS CHANGED.
"id" => $a['id'],
"itemid" => $a['itemid'],
...
现在,通过进一步的结果,您可以轻松检查阵列中是否已存在id:
if (isset($items[$a['id']])) {
// We had this id before, just add files/tags to it.
// Check first, if files subarray exists, if not: create it.
if (!isset($items[$a['id']]['files'])) {
$items[$a['id']]['files'] = array();
}
$items[$a['id']]['files'][] = array(...); // add the new file.
// Repeat for tags.
}
如果您的结果可以多次返回同一个文件作为id,您可以使用搜索功能检查文件名是否已经存在:
$filename = $a['filename'];
if (!searchFilename($filename, $items[$a['id']]['files']) {
// Filename is not in there, yet.
// Add file info.
}
function searchFilename($id, $array) {
foreach ($array as $key => $val) {
if ($val['filename'] === $id) {
return true;
}
}
return false;
}
同样的方法也适用于标签
最后,如果您不需要索引$items
的ID,只需调用:
$items = array_values($items);
你的PHP版本是什么?在PHP>=5.5上,您可以使用:
在数组中($a['filename',array\u列($items[$i-1]['files','name'))
谢谢您的提示@fusion3k。我将阅读有关该函数的文档并尝试一下。您的PHP版本是什么?在PHP>=5.5上,您可以使用:在数组中($a['filename',array\u列($items[$i-1]['files','name'))
谢谢您的提示@fusion3k。我将阅读该函数的文档并尝试一下。我想我知道我现在哪里出了问题——使用索引。如果有超过1个额外的文件/标记,它将查看数组中的错误索引。谢谢你的解释和时间!我很肯定你的解决办法对我有用。一旦我在代码中得到解决方案,我会确认/标记它:P(P.S.我应该删除这个问题,还是留下它?缺陷在我的逻辑中=P请随意留下意见作为注释。)确切地说,因为$I
是递增的,而不管$a['id']
之前是否处理过。我做了一些修改,但你的解决方案正是我所需要的。我完全改变了数组的设置方式,使用文件/标记ID作为索引。再次感谢保罗,你真厉害!我想我知道我现在错在哪里了——使用索引。如果有超过1个额外的文件/标记,它将查看数组中的错误索引。谢谢你的解释和时间!我很肯定你的解决办法对我有用。一旦我在代码中得到解决方案,我会确认/标记它:P(P.S.我应该删除这个问题,还是留下它?缺陷在我的逻辑中=P请随意留下意见作为注释。)确切地说,因为$I
是递增的,而不管$a['id']
之前是否处理过。我做了一些修改,但你的解决方案正是我所需要的。我完全改变了数组的设置方式,使用文件/标记ID作为索引。再次感谢保罗,你真厉害!