Books/Tutorials on PHP arrays

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jbatty
Forum Commoner
Posts: 35
Joined: Tue Sep 23, 2003 2:42 pm

Books/Tutorials on PHP arrays

Post 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.
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post 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.
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post 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; 
        }
    }
}
?>
Last edited by McGruff on Wed Aug 10, 2005 2:27 pm, edited 1 time in total.
jbatty
Forum Commoner
Posts: 35
Joined: Tue Sep 23, 2003 2:42 pm

Post by jbatty »

Thanks Gen-ik and McGruff.
Here is a part of my array.

Code: Select all

Array ( &#1111;0] => Array ( &#1111;item] => &#1111;questionId] => 1a &#1111;question] => What is the capital city of England &#1111;choice] => London Cardiff Edinburgh Dublin ) &#1111;1] => Array ( &#1111;item] => &#1111;questionId] => 2a &#1111;question] => Which of these countries do not belong to the Scandanavia? &#1111;choice] => Sweden Poland Norway Denmark ) &#1111;2] => Array ( &#1111;item] => &#1111;questionId] => 1b &#1111;question] => London is located in which Country? &#1111;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.
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post 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'];
}

?>
Post Reply