Hier tritt habe ich eine verschachtelte Array, den Grund Kommentar WerteWie ein Array Element neben einem anderen Array-Element hinzuzufügen, wenn ein bestimmte Wert
//cid is comment id and pid is parent id
$comments = array(
array(
'cid' = '47',
'pid' = '0',
'comment' = 'This is comment'
),
array(
'cid' = '48',
'pid' = '0',
'comment' = 'This is another comment'
),
array(
'cid' = '49',
'pid' = '47',
'comment' = 'This is child comment to parentID 47'
),
);
das Endergebnis enthält Was ich will, ist jeder Kommentar Array Element, das hat Eine parentID sollte sich nach der commentID, die der Eltern-ID entspricht, neu positionieren. Um es zu visualisieren, ist das Kommentar-Array mit einem übergeordneten Element eine Antwort auf den ursprünglichen Kommentar. Bisher habe ich es geschafft, so viel
//main loop
foreach ($comments as $index => $val) {
$cids[] = $val['cid'];
if($val['pid'] > 0) {
foreach ($cids as $cidindex => $cid) {
if($val['pid'] == $cid) {
$results[$index] = $val;
$results[$index + 1] = _comment_load($cid, $array);
}
}
}
else {
$results_ano[$index] = $val;
}
}//end of main loop
//loads comment
function _comment_load($cid, $array) {
foreach ($array as $Key => $item) {
if($item['cid'] == $cid) {
return $item;
}
}
}
So nach $results
Zusammenführung und $results_ano
ich auf print_r
Array
(
[2] => Array
(
[pid] => 47
[cid] => 49
)
[3] => Array
(
[pid] => 0
[cid] => 47
)
[0] => Array
(
[pid] => 0
[cid] => 47
)
[1] => Array
(
[pid] => 0
[cid] => 48
)
)
Ich habe es geschafft, zu bekommen, was ich wollte etwas, aber jetzt die commentId = 47
wiederholt und ich möchte, dass die Wiederholen von cid
Elementarray, das aus dem resultierenden Array entfernt werden soll.
Gibt es irgendetwas Bestimmtes, das zwingt, dass das Array in dieser bestimmten Reihenfolge benötigt wird, was nicht durch mehrere Arrays erreicht werden kann? – m4grio
Danke, Ein Array-Element, das als eine Eltern-ID eine Antwort auf eine Kommentar-ID ist, die es viel einfacher macht, Vorlage als auch später. –