Utiliser la fonction PHP "next" pour parcourir tous les index suivants d'un Array
Bonjour.
J'essaie de scinder le tableau des données des items qui est représentée par la variable $this->children dans la fonction getNextBufferLimit:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
| function nextToString() {
$objectString='';
$items=$this->children;
$bufferLimit=(count($items)==0)?1:$this->getBufferLimit()+1;
do {
$bufferLimit-=1;
$objectString=$this->recurseMenu($this->getNextBufferLimit(),$bufferLimit);
} while(strlen($objectString>160));
$this->index=$bufferLimit;
return $objectString;
}
function getNextBufferLimit() {
$len = count($this->children);
$margin = $len-$this->index;
echo "<pre>";
print_r(array_chunk($this->children, 5, true));
echo "</pre>";
$childrenSplited = array_chunk($this->children, 5, true);
if($margin<5)
return $this->index+$margin;
else
$childSplitedNext = next($childrenSplited);
echo "<pre>";
echo count($this->children) % count($childSplitedNext);
echo "</pre>";
return $childSplitedNext;
}
function getBufferLimit() {
$len = count($this->children);
$margin = $len-$this->index;
if($margin<5)
return $this->index+$margin;
else
return $this->index+5; //Permet de définir le Nombre de Menu à afficher au niveau de l'index "/ussd/receiver.php" dont le Menu est "/ussd/MyTree.php".
}
function recurseMenu($items,$bufferLimit) {
$objectString="<strong>". $this->getTitle() . "</strong>" . PHP_EOL;
$lastMenu=false;
if(count($items)>0) {
foreach ($items as $i => $item) {
if ($i >= $bufferLimit) {
break;
}
$num = $i + 1;
//get node by name
$userSessions = $_SESSION['userSessions'];
$currUserSession = $userSessions[$this->address];
$node = $currUserSession->getNode($item);
$title = $node->getTitle();
$objectString .= PHP_EOL . $num . '. ' . $title;
}
} else {
$objectString=$objectString.PHP_EOL . 'NO DATA AVAILABLE, TRY AGAIN LATER';
}
$lastMenu=$bufferLimit==count($items);
$objectString=$objectString . PHP_EOL . PHP_EOL . "<strong>0. Exit</strong>";
if($this->getParent() != '0'){
$objectString=$objectString . PHP_EOL . "<strong>#. Back</strong>";
}
if($lastMenu===false){
$rem=count($items)-$this->index;
$objectString=$objectString . PHP_EOL . "<strong>99. Next (".$rem.")</strong>";
}
return $objectString;
} |
Le débugue array_chunk($this->children, 5, true) de la ligne 16 à 18 dans la fonction getNextBufferLimit me retourne:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| Array
(
[0] => Array
(
[0] => sendmoney
[1] => airtimedata
[2] => withdrawcash
[3] => paybill
[4] => buygoods
)
[1] => Array
(
[5] => checkbalance
[6] => financialservices
[7] => myaccount
[8] => messages
[9] => messages1
)
[2] => Array
(
[10] => messages2
[11] => messages3
)
) |
Et le débugue du modulo de count($this->children) % count($childSplitedNext); dans la même fonction donne 2. Ce qui veut dire qu'il y a encore des items à afficher pour une exécution prochain du bouton next.
Mon problème: c'est qu'en utilisant la fonction de navigation d'au prochain élément d'un tableau next (https://www.php.net/manual/fr/function.next.php) dans la variable $childSplitedNext = next($childrenSplited); au niveau de la ligne 24 de la fonction getNextBufferLimit, j'arrive à passer une 1ère fois vers le 2ème élément du tableau (de l'index 0 à l'index 1). Mais quand j'essaie une d'exécuter une deuxième fois ce next pour passer de l'index 1 à l'index 2, je reçois l'erreur suivante:
Citation:
Warning: count(): Parameter must be an array or an object that implements Countable in C:\laragon\www\ussd\ussdmenu-server-php\UssdNode.php on line 97
Warning: Invalid argument supplied for foreach() in C:\laragon\www\ussd\ussdmenu-server-php\UssdNode.php on line 98
Warning: count(): Parameter must be an array or an object that implements Countable in C:\laragon\www\ussd\ussdmenu-server-php\UssdNode.php on line 113
Warning: count(): Parameter must be an array or an object that implements Countable in C:\laragon\www\ussd\ussdmenu-server-php\UssdNode.php on line 120
Comment donc appliquer la fonction next (https://www.php.net/manual/fr/function.next.php) pour naviguer à chaque index suivant de tous les éléments du tableau au lieu de naviguer une seule fois de l'index 0 à 1 comme je le fait actuellement sachant qu'il y a deux autres qui sont cachés ???
Merci de m'aider s'il vous plaît.