将值插入链接列表php中的特定索引

时间:2016-09-30 09:04:43

标签: php linked-list add

我想在php中的链接列表($ wordsposition)中的特定索引处插入值。 它让我看到了OutOfRangeException的异常,所以我检查了我的链表中的索引,看起来很好。 这是我第一次在php中使用链接列表,所以我有点做错了但是我真的不知道它是什么...我很感激你的帮助!

这是我的代码:

 $wordsposition = new SplDoublyLinkedList(); 
        foreach ($searchwords as $key => $value) {
            $wordsposition -> push(array($value=>' '));
        }
        print_r($wordsposition);
        echo "<br>";
        foreach ($searchwords as $key => $value) {
            $sql = "SELECT * FROM words WHERE word='$value'";
            $res = $conn->query($sql);

            if ($res->num_rows > 0) {//check if find matching 
                while ($row = $res->fetch_assoc()) {
                   $wordsposition->add($value,$row['num']);
                     echo " my index is: ".$value."<br>";
                     echo " my file is: ".$row['num']."<br>";
                }
            }
        }

这是我的回声:

SplDoublyLinkedList Object ( [flags:SplDoublyLinkedList:private] => 0 [dllist:SplDoublyLinkedList:private] => Array ( [0] => Array ( [alright] => ) [1] => Array ( [always] => ) [2] => Array ( [around] => ) [3] => Array ( [and] => ) ) ) my index is: alright
my file is: 0
my index is: always
my file is: 2
my index is: always
my file is: 3
my index is: always
my file is: 4
my index is: around
my file is: 0
my index is: around
my file is: 4
my index is: and
my file is: 0
my index is: and
my file is: 1
my index is: and
my file is: 2
my index is: and
my file is: 3
my index is: and
my file is: 4

0 个答案:

没有答案