Filtering the elements of a multi-dimensional array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Geoff Soper

    #1

    Filtering the elements of a multi-dimensional array

    I would like to be able to filter every element of a multi-dimensional array
    using htmlspecialchar s() and to retain the structure of the array. I can't
    see how to do this when I don't know how many dimensions the array has.

    Any suggestions?

    Thanks,
    Geoff

    --
    Remove nospam in email address to reply


  • Andy Hassall

    #2
    Re: Filtering the elements of a multi-dimensional array

    On Thu, 4 Dec 2003 21:02:51 -0000, "Geoff Soper" <news.nospam@al phaworks.co.uk>
    wrote:
    [color=blue]
    >I would like to be able to filter every element of a multi-dimensional array
    >using htmlspecialchar s() and to retain the structure of the array. I can't
    >see how to do this when I don't know how many dimensions the array has.[/color]

    <pre>
    <?php

    $a = array(
    array(
    array('&', '&', '&'),
    '&',
    array('&', '&')
    ),
    '&',
    array('&', '&', '&'),
    );

    print_r($a);

    function htmlspecialchar sArray(&$a) {
    foreach (array_keys($a) as $key)
    if (is_array($a[$key]))
    htmlspecialchar sArray($a[$key]);
    else
    $a[$key] = htmlspecialchar s($a[$key]);
    }

    htmlspecialchar sArray($a);

    print_r($a);

    ?>
    </pre>

    So long as you don't set up any loops in your arrays, of course :-)

    --
    Andy Hassall (andy@andyh.co. uk) icq(5747695) (http://www.andyh.co.uk)
    Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)

    Comment

    Working...