[英]How can I give each array in a multidimensional array a unique identifier?
I have some paths inside my database 我的数据库中有一些路径
mylist/folder1/horse/fred
mylist/folder1/cat/john
mylist/folder2/cat/sam
mylist/folder2/cat/cat/john
I am creating an array with a tree structure: 我正在创建一个带有树结构的数组:
function explodeTree($array, $delimiter = "_", $baseval = false) {
if(!is_array($array)) return false;
$splitRE = "/" . preg_quote($delimiter, "/") . "/";
$returnArr = array();
foreach ($array as $key => $val) {
$parts = preg_split($splitRE, $key, -1, PREG_SPLIT_NO_EMPTY);
$leafPart = array_pop($parts);
$parentArr = &$returnArr;
foreach ($parts as $part) {
if (!isset($parentArr[$part])) {
$parentArr[$part] = array();
} elseif (!is_array($parentArr[$part])) {
if ($baseval) {
$parentArr[$part] = array("__base_val" => $parentArr[$part]);
} else {
$parentArr[$part] = array();
}
}
$parentArr = &$parentArr[$part];
}
if (empty($parentArr[$leafPart])) {
$parentArr[$leafPart] = $val;
} elseif ($baseval && is_array($parentArr[$leafPart])) {
$parentArr[$leafPart]["__base_val"] = $val;
}
}
return $returnArr;
}
$sql = "SELECT path, path FROM mylist WHERE id = ?";
$q = $pdo->prepare($sql);
$q->execute([$id]);
$array = $q->fetchAll(PDO::FETCH_KEY_PAIR);
$tree = explodeTree($array, "/");
The result is 结果是
array(1) {
["mylist"]=>
array(2) {
["folder1"]=>
array(2) {
["horse"]=>
array(1) {
["fred"]=>
string(30) "mylist/folder1/horse/fred"
}
["cat"]=>
array(1) {
["john"]=>
string(28) "mylist/folder1/cat/john"
}
}
["folder2"]=>
array(1) {
["cat"]=>
array(2) {
["sam"]=>
string(27) "mylist/folder2/cat/sam"
["cat"]=>
array(1) {
["john"]=>
string(32) "mylist/folder2/cat/cat/john"
}
}
}
}
}
Is it possible to give each array a unique identifier? 是否可以给每个数组一个唯一的标识符? It is important because later I need to look for example for the specific array cat
. 这很重要,因为稍后我需要查找特定数组cat
示例。 But cat
exists multiple times, and right now I cannot identity which cat
I am looking for. 但是cat
存在多次,现在我无法确定我要寻找的cat
。
You have to put petID
as index into the array after the sql_query. 您必须将petID
作为索引放入sql_query之后的数组中。
while ($row = $this->stmt->fetch()) {
$pet_array[$row[petID]] = $row;
}
For just 2 selected columns, you could use array_column()
instead of the while
loop on a already completely fetched $pet_array
. 对于仅2个选定的列,可以在已经完全$pet_array
上使用array_column()
代替while
循环。
http://de2.php.net/manual/en/function.array-column.php http://de2.php.net/manual/zh/function.array-column.php
$pet_array = array_column($pet_array, 'cat', 'petID'); // this will only hold $arr[petID] = 'Fred'; so no paths, etc.
function explodeTree($array, $delimiter = "_", $baseval = false) {
if(empty($array)) return false; // checks for false, null, 0, "0", and empty array
$splitRE = "/" . preg_quote($delimiter, "/") . "/";
$returnArr = array();
foreach ($array as $key => $val) {
$parts = preg_split($splitRE, $val['path'], -1, PREG_SPLIT_NO_EMPTY);
$returnArr[$val['id']] = $parts;
//$returnArr[$val['id']] = ARRAY('mylist'=>$parts[0], 'folder'=>$parts[1], 'pet'=>$parts[2], 'name'=>$parts[3]); // does only work for exactly for items (of same category)
}
return $returnArr;
}
So now, if you want to get the details of eg pet
with petID=4
, you can do this like: 因此,现在,如果您想获取petID=4
的例如pet
的详细信息,可以执行以下操作:
$sql = "SELECT id, path FROM mylist WHERE id = ?";
$q = $pdo->prepare($sql);
$q->execute([$id]);
$array = $q->fetchAll(PDO::FETCH_KEY_PAIR);
$tree = explodeTree($array, "/");
echo 'I am a '.$tree[4]['pet'].', and my name is '.$tree[4]['name'];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.