2

I am trying to create a function that works on both simple arrays and nested arrays. So far, the function looks like this:

function fnPrepareDataForBrowser($values)
  {
    $values = array_map('htmlspecialchars', $values);   
    return $values;
  }

It works fine for simple arrays - for example:

Array
(
    [Item ID] => 25469
    [Item Desc] => spiral nails, 1"
    [Standard Item] => yes
    [Entry UOM] => lb
)

But it fails with the message "Warning: htmlspecialchars() expects parameter 1 to be string, array given..." for nested arrays - for example:

Array
(
[0] => Array
    (
        [Item ID] => 25469
        [Item Description] => spiral nails, 1"
        [Standard Item] => yes
        [Entry UOM] => lb
    )

[1] => Array
    (
        [Item ID] => 25470
        [Item Description] => finishing screws, 2.5"
        [Standard Item] => no
        [Entry UOM] => lb
    )

[2] => Array
    (
        [Item ID] => 25576
        [Item Description] => paint brush, 3"
        [Standard Item] => no
        [Entry UOM] => each
    )
)

What modifications should be made to the function so it works for both simple arrays and nested arrays?

4 Answers 4

2

Try this:

<?php
    /**
     * Applies callback function recursively to every element of the given array.
     *
     * If the array contains inner arrays or objects, the callback is also applied
     * to these.
     *
     * Caution: If $arrayOfObject is an object, only public members are processed.
     *
     * @param callback     $func
     * @param array|object $array
     *
     * @return array
     */
    public static function array_map_recursive($func, $arrayOrObject)
    {
            $array = is_array($arrayOrObject) ? $arrayOrObject : get_object_vars($arrayOrObject);
            foreach ($array as $key => $val) {
                    $array[$key] = is_array($val) || is_object($val)
                            ? self::array_map_recursive($func, $val)
                            : call_user_func($func, $val);
            }
            return $array;
    }
?>
Sign up to request clarification or add additional context in comments.

Comments

1
function fnPrepareDataForBrowser(& $values)
{
    return is_array($values) ? 
           array_map('fnPrepareDataForBrowser', $values) : 
           htmlspecialchars($values);   

}

$array = fnPrepareDataForBrowser( $your_array );

Comments

1

You can use this array_map_recursive function instead. I stole it from the manual.

function array_map_recursive($callback, $array) {
    foreach ($array as $key => $value) {
        if (is_array($array[$key])) {
            $array[$key] = array_map_recursive($callback, $array[$key]);
        }
        else {
            $array[$key] = call_user_func($callback, $array[$key]);
        }
    }
    return $array;
}

Comments

0

Does this suit your needs?

<?php

function fnPrepareDataForBrowser(&$values)
{
    $result = array();

    foreach ( $values as $k => $v )
    {
        if ( is_array( $v ) ) {

            array_push( $result, array_map('htmlspecialchars', $v) );
        }
    }

    return $result;
}


$tst = array(
array (
        'Item ID' => 25469,
        'Item Description' => "spiral nails & 1",
        'Standard Item' => 'yes&',
        'Entry UOM' => 'lb>'
    ),

array (
        'Item ID' => 25470,
        'Item Description' => "finishing screws & 2.5",
        'Standard Item' => 'no&',
        'Entry UOM' => 'lb<'
    ),

array (
        'Item ID' => 25576,
        'Item Description' => "paint brush & 3",
        'Standard Item' => 'no&',
        'Entry UOM' => 'each&'
    )
);

$result = fnPrepareDataForBrowser($tst);

print_r( $result );

Produces these results

Array
(
    [0] => Array
        (
            [Item ID] => 25469
            [Item Description] => spiral nails &amp; 1
            [Standard Item] => yes&amp;
            [Entry UOM] => lb&gt;
        )

    [1] => Array
        (
            [Item ID] => 25470
            [Item Description] => finishing screws &amp; 2.5
            [Standard Item] => no&amp;
            [Entry UOM] => lb&lt;
        )

    [2] => Array
        (
            [Item ID] => 25576
            [Item Description] => paint brush &amp; 3
            [Standard Item] => no&amp;
            [Entry UOM] => each&amp;
        )

)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.