Please can anyone suggest books / tutorials that deals very well with advanced multidimensional array programming in PHP. I need to write an algorithm that intelligently produces a number of subsets of an array.
Please note that I have already looked at the PHP manual!
Thanks.
Books/Tutorials on PHP arrays
Moderator: General Moderators
Can't say that I've seen anything (books/tutorials) that deal with Multidimensional Arrays in great detail.. all of the ones I've seen have added them onto the end of the the arrays sections in a sort of "oh by the way you can also use multidimensional arrays" and don't go into much detail.
What sort of thing are you thinking about doing... maybe us lot can help you out? Personally I'm using MD arrays in nearly every script I writing at the moment.
What sort of thing are you thinking about doing... maybe us lot can help you out? Personally I'm using MD arrays in nearly every script I writing at the moment.
Here's a library function which illustrates the use of array_walk(). It won't do what you want but it might give you some ideas.
I'm assuming you've got some programming experience: it should be possible to adjust the iterator to output an array instead of processing existing elements.
For example, an arrayIterator could be a class method. Other methods could define the sorting functions you wish to use as array walk functions (or possibly it might be more efficient to extend a base class with some childs).
I'm assuming you've got some programming experience: it should be possible to adjust the iterator to output an array instead of processing existing elements.
For example, an arrayIterator could be a class method. Other methods could define the sorting functions you wish to use as array walk functions (or possibly it might be more efficient to extend a base class with some childs).
Code: Select all
<?php
// apply $function to all elements in an array; $function can only take a single value as an arg (which will be the current array element) and $function must return a value (replaces the element after processing by the function)
// uses: slashing, searching, replacing, whatever..
function arrayIterator(&$array, $function)
{
foreach ($array as $key=>$value) {
IF (!is_array($value))
{
$array[$key] = $function($value);
} ELSE {
arrayIterator($value, $function);
$array[$key] = $value;
}
}
}
?>
Last edited by McGruff on Wed Aug 10, 2005 2:27 pm, edited 1 time in total.
Thanks Gen-ik and McGruff.
Here is a part of my array..
Basically, the array is contains multiple choce questions with their answers. It came from an XML source.
WHat i need to do is to write a function that can look at content of the array containing say 30 questions and come with say 4 sets (sub arrays)of 5 unique questions. Clearly, i need to iterate through this array, by having access to the questionId key. This is where i am a bit stuck at the moment. Any help will be appreciated.
Here is a part of my array.
Code: Select all
Array ( ї0] => Array ( їitem] => їquestionId] => 1a їquestion] => What is the capital city of England їchoice] => London Cardiff Edinburgh Dublin ) ї1] => Array ( їitem] => їquestionId] => 2a їquestion] => Which of these countries do not belong to the Scandanavia? їchoice] => Sweden Poland Norway Denmark ) ї2] => Array ( їitem] => їquestionId] => 1b їquestion] => London is located in which Country? їchoice] => England Scotland Ireland Isle of Man ) )Basically, the array is contains multiple choce questions with their answers. It came from an XML source.
WHat i need to do is to write a function that can look at content of the array containing say 30 questions and come with say 4 sets (sub arrays)of 5 unique questions. Clearly, i need to iterate through this array, by having access to the questionId key. This is where i am a bit stuck at the moment. Any help will be appreciated.
OK that sounds a lot simpler than I first imagined. The foreach() fn could be used to pick out the question and choices something like:
Code: Select all
<?php
// $quiz = your original array
foreach($quiz as $value)
{
$question = $value['question'];
$choices = $value['choices'];
}
?>