Page 1 of 1
Books/Tutorials on PHP arrays
Posted: Fri Oct 17, 2003 5:49 am
by jbatty
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.
Posted: Fri Oct 17, 2003 1:47 pm
by Gen-ik
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.
Posted: Fri Oct 17, 2003 6:28 pm
by McGruff
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).
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;
}
}
}
?>
Posted: Sun Oct 19, 2003 9:46 am
by jbatty
Thanks Gen-ik and McGruff.
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.
Posted: Sun Oct 19, 2003 4:37 pm
by McGruff
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'];
}
?>