Traversing the data in PHP is generally used to list,foreach,each in fact, but the following tutorial may not be used, I would like to introduce you to iterate over the array of advanced instances, I hope this method is helpful to everyone.
When you learn a programming language, you always learn for, and then try to use the while to write some exercises for the effects of for and so on.
To see if there is no foreach before, want to have the function of foreach how to write (with while, list, each to achieve).
See in this article: The syntax of the foreach predecessor of PHP
| The code is as follows |
Copy Code |
|
The old notation
Reset ($attributes);
while (list ($key, $value) = each ($attributes)) {
Do something
}
PHP4 version added
foreach ($attributes as $key = = $value) {
Do something
} |
Multidimensional associative array Ordering
PHP provides some functions for sorting arrays, such as sort (), Ksort (), and Asort (), but does not provide a sort of multidimensional associative array.
For example, an array like this:
| The code is as follows |
Copy Code |
|
Array
(
[0] = = Array
(
[Name] = Chess
[Price] = 12.99
)
[1] = = Array
(
[Name] = Checkers
[Price] = 9.99
)
[2] = = Array
(
[Name] = Backgammon
[Price] = 29.99
)
) |
To sort the array in ascending order, you need to write a function to compare the price, and then pass the function as a callback function to the Usort () function to accomplish this:
| The code is as follows |
Copy Code |
|
function Compareprice ($priceA, $priceB) {
return $priceA [' Price ']-$priceB [' Price '];
}
Usort ($games, ' compareprice '); If the program fragment is executed, the array is sorted and the result is as follows:
Array
(
[0] = = Array
(
[Name] = Checkers
[Price] = 9.99
)
[1] = = Array
(
[Name] = Chess
[Price] = 12.99
)
[2] = = Array
(
[Name] = Backgammon
[Price] = 29.99
)
) |
To sort the array in descending order, replace the two minus number in the Compareprice () function with the position.
Iterating through arrays in reverse order
PHP's while loop and for loop are the most common methods of traversing an array. But how do you traverse the array like this one?
| The code is as follows |
Copy Code |
|
Array
(
[0] = = Array
(
[Name] = Board
[Games] = = Array
(
[0] = = Array
(
[Name] = Chess
[Price] = 12.99
)
[1] = = Array
(
[Name] = Checkers
[Price] = 9.99
)
)
)
) |
The PHP standard library has an iterator to the collection Iterators class that can be used not only to traverse some heterogeneous data structures (such as file systems and database query result sets), but also to traverse a number of nested arrays that do not know the size. For example, the traversal of the above array can be done using the Recursivearrayiterator iterator:
| The code is as follows |
Copy Code |
|
$iterator = new Recursivearrayiterator ($games);
Iterator_apply ($iterator, ' Navigatearray ', Array ($iterator));
function Navigatearray ($iterator) {
while ($iterator->valid ()) {
if ($iterator->haschildren ()) {
Navigatearray ($iterator->getchildren ());
} else {
printf ("%s:%s", $iterator->key (), $iterator->current ());
}
$iterator->next ();
}
} |
Executing the code will give the following results:
Name:board
Name:chess
price:12.99
Name:checkers
price:9.99
Filter the results of associative arrays
Suppose you get an array of the following, but you just want to manipulate the element with a price below $11.99:
| The code is as follows |
Copy Code |
|
Array
(
[0] = = Array
(
[Name] = Checkers
[Price] = 9.99
)
[1] = = Array
(
[Name] = Chess
[Price] = 12.99
)
[2] = = Array
(
[Name] = Backgammon
[Price] = 29.99
)
) |
Using the Array_reduce () function can be a simple implementation:
| The code is as follows |
Copy Code |
|
function Filtergames ($game) {
Return ($game [' Price '] < 11.99);
}
$names = Array_filter ($games, ' filtergames '); |
The Array_reduce () function filters out all elements that do not satisfy the callback function, and the callback function for this example is filtergames. Any element with a price below 11.99 will be left and the others will be removed. The result of the code snippet execution:
| The code is as follows |
Copy Code |
Array
(
[1] = = Array
(
[Name] = Checkers
[Price] = 9.99
)
) |
Object conversions to an array
A requirement is to convert an object into an array form, which is much simpler than you think, and just casts it! Example:
| The code is as follows |
Copy Code |
|
Class Game {
Public $name;
Public $price;
}
$game = new Game ();
$game->name = ' chess ';
$game->price = 12.99;
Print_r (Array ($game)); Executing this example will result in the following:
Array
(
[0] = = Game Object
(
[Name] = Chess
[Price] = 12.99
)
) |
There are some unexpected side effects of converting an object to an array. For example, the above code snippet, all member variables are of the public type, but the return result for private variables will become different. Here is another example:
| The code is as follows |
Copy Code |
|
Class Game {
Public $name;
Private $_price;
Public Function Setprice ($price) {
$this->_price = $price;
}
}
$game = new Game ();
$game->name = ' chess ';
$game->setprice (12.99);
Print_r (Array ($game)); Execute the code snippet:
Array
(
[0] = = Game Object
(
[Name] = Chess
[_price:game:private] = 12.99
)
) |
As you can see, the keys to the private variables stored in the array are automatically changed for the sake of distinction.
The "natural sort" of an array
PHP's ordering of "alpha-numeric" strings is indeterminate. For example, suppose you have a lot of picture names stored in an array:
| The code is as follows |
Copy Code |
$arr = Array (
0=> ' Madden2011.png ',
1=> ' Madden2011-1.png ',
2=> ' Madden2011-2.png ',
3=> ' Madden2012.png '
); |
How do you sort this array? If you sort the array with sort (), the result is this:
| The code is as follows |
Copy Code |
Array
(
[0] = Madden2011-1.png
[1] = Madden2011-2.png
[2] = Madden2011.png
[3] = Madden2012.png
) |
Sometimes that's what we want, but what if we want to keep the original subscript? To resolve the problem, you can use the Natsort () function, which sorts the array in a natural way:
| The code is as follows |
Copy Code |
|
$arr = Array (
0=> ' Madden2011.png ',
1=> ' Madden2011-1.png ',
2=> ' Madden2011-2.png ',
3=> ' Madden2012.png '
);
Natsort ($arr);
echo "
"; Print_r ($arr); echo " ";
?>
Operation Result:
Array
(
[1] = Madden2011-1.png
[2] = Madden2011-2.png
[0] = Madden2011.png
[3] = Madden2012.png
) |
http://www.bkjia.com/PHPjc/632897.html www.bkjia.com true http://www.bkjia.com/PHPjc/632897.html techarticle traversing the data in PHP is generally used to list,foreach,each in fact, but the following tutorial may not be used, I would like to introduce you to iterate over the array of advanced instances, I hope ...