Showing posts with label array. Show all posts
Showing posts with label array. Show all posts

PHP: Recursive Handling of Multidimensional Arrays

Saturday, March 14, 2009

There will be times when you find yourself with a multidimensional array and needing to perform some task across all values in it. It would be cumbersome (and in some cases impossible) to actually perform the task manually on all the elements. This is where recursion can help you. By using a recursive function, you can craft a simple function that performs the final task for you. Find the Maximum Value in a Multidimensional Array

<?php
function recursive_array_max($a) {
    foreach ($a as $value) {
        if (is_array($value)) {
            $value = recursive_array_max($value);
        }
        if (!(isset($max))) {
            $max = $value;
        } else {
            $max = $value > $max ? $value : $max;
        }
    }
    return $max;
}

$dimensional = array(
    57,
    array(3, 35),
    array(235, 534, 73, array(3, 4, 956), 6),
    14,
    2,
    array(5, 74, 73)
    );

$max = recursive_array_max($dimensional);

echo "The maximum value was: {$max}";
?>


In this case a function is created that finds the maximum value within the array. Like the built-in function max(), however, this recursive function dives deep into the array. Note the use of is_array() to detect whether each entry happens to be an array itself. If so, the function is called recursively on that array to find its maximum value. Eventually every element has been individually compared, or each array has been collapsed to its single highest value. Then the final answer is known.

Hope it helps.

Turning Array into NonArray Variable and Restore It Back

Thursday, February 5, 2009

Array is useful, but it's not silver bullet. There are some conditions that we can't use array. Saving an array into a session or cookie directly, for instance. XML and MySQL can't handle native PHP array type either.

That's where the function serialize come in handy. And here's a script that shows how this function works:

<?php
$drinks = array("lemon squash",
                  "capuccino",
                  "soda");
$dinners = array(
    "salad" => "spinach",
    "main" => "tenderloine",
    "drink" => $drinks);

$dinners_s = serialize($dinners); 

echo $dinners_s; 

?>
The outputs:
PEa:3:{s:5:"salad";s:6:"spinach";s:4:"main";s:11:"tenderloine";s:5:"drink";a:3:{i:0;s:12:"lemon squash";i:1;s:9:"capuccino";i:2;s:4:"soda";}}

THe serialized variable $dinners_s can be stored in almost anywhere -database, hiddem input variable, cookies, session, and so on. And You only need a line of code to restore it back to array

$dinners = deserialize($dinners_s)

But keep in mind, serialized arrays can be read easily by people so you might need to encrypt them so that nobody can't sniff your data. Secondly, don't pass serialized arrays through GET method because URL's length have finite length. And last one, don't overuse it, when you start to get and store serialized arrays consistently, maybe it's time for you to redesign your storage mechanism or database structure.

Hope it helps.