1 | <?php |
||||
2 | /** |
||||
3 | * This file shows five different ways to iterate through the contents of a |
||||
4 | * QueryPath. |
||||
5 | * |
||||
6 | * QueryPath wraps zero or more elements (or, more generally, just objects). |
||||
7 | * Most of the operations available in QueryPath will operate on the entire |
||||
8 | * set of objects. Sometimes, though, it is desirable to go through the results |
||||
9 | * one at a time. These examples exhibit five ways of accomplishing this. |
||||
10 | * |
||||
11 | * Keep in mind that PHP passes objects (more or less) by reference, which means |
||||
12 | * that a change to an item inside of a loop will be reflected in the main QueryPath. |
||||
13 | * In other words, there is no need to put data back into the QueryPath if it is |
||||
14 | * altered during a loop. |
||||
15 | * |
||||
16 | * @see QueryPath |
||||
17 | * |
||||
18 | * @author M Butcher <[email protected]> |
||||
19 | * @license LGPL (The GNU Lesser GPL) or an MIT-like license. |
||||
20 | */ |
||||
21 | |||||
22 | require '../src/QueryPath/QueryPath.php'; |
||||
23 | |||||
24 | $demo = '<?xml version="1.0" ?> |
||||
25 | <data> |
||||
26 | <li>Foo</li> |
||||
27 | <li>Foo</li> |
||||
28 | <li>Foo</li> |
||||
29 | <li>Foo</li> |
||||
30 | <li>Foo</li> |
||||
31 | </data> |
||||
32 | '; |
||||
33 | |||||
34 | $qp = qp($demo, 'data'); |
||||
35 | |||||
36 | // Iterate over elements as DOMNodes: |
||||
37 | foreach ($qp->get() as $li_ele) { |
||||
38 | print $li_ele->tagName . PHP_EOL; // Prints 'li' five times. |
||||
39 | } |
||||
40 | |||||
41 | // Iterate over elements as QueryPath objects |
||||
42 | foreach ($qp as $li_qp) { |
||||
43 | print $li_qp->tag() . PHP_EOL; // Prints 'li' five times |
||||
44 | } |
||||
45 | |||||
46 | function callbackFunction($index, $element) { |
||||
0 ignored issues
–
show
|
|||||
47 | print $element->tagName . PHP_EOL; |
||||
48 | } |
||||
49 | |||||
50 | // Iterate using a callback function |
||||
51 | $qp->each('callbackFunction'); |
||||
52 | |||||
53 | // Iterate using a Lambda-style function |
||||
54 | $qp->eachLambda('return $item->tagName . PHP_EOL;'); |
||||
0 ignored issues
–
show
The function
QueryPath\DOMQuery::eachLambda() has been deprecated.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
55 | |||||
56 | // Loop through by index/count |
||||
57 | for ($i = 0; $i < $qp->size(); ++$i) { |
||||
0 ignored issues
–
show
The function
QueryPath\DOMQuery::size() has been deprecated: QueryPath now implements Countable, so use count().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This function has been deprecated. The supplier of the function has supplied an explanatory message. The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead. ![]() |
|||||
58 | $domElement = $qp->get($i); |
||||
59 | print $domElement->tagName . PHP_EOL; |
||||
60 | } |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.