0

I am working with an array such as the following and I'd like to set all the "price" keys, that have no value, to 0.

How can I achieve this, if the depth of the array is infinite?

Big thanks!

Array
(
    [0] => Array
    (
        [random_key0] => Array
            (
                [name] => Foo
                [price] => 25
            )

        [random_key1] => Array
            (
                [name] => Bar
                [price] => 
            )
    [1] => Array
    (
        [name] => 125
        [price] =>
    )
    [2] => Array
    (
        [another_key0] => Array
            (
                [name] => Foo
                [options] => Options here
                [special0] => Array
                    (
                        [name] => Special Name
                        [price] =>
                    )

                [special1] => Array
                    (
                        [name] => Special 2
                        [price] => 120
                    )

              )
        )
  )
2
  • I can recommend you instead of looping it to cast prices to int/float when you use it. Empty will become 0.. Commented Apr 6, 2015 at 12:20
  • Where are you getting the source data from? This would be much easier to do there. Or even on display if that would do the job. Commented Apr 6, 2015 at 12:22

2 Answers 2

1

You would do this with a "walking" function, that calls itself until all elements are worked through:

<?php
$test = array(
    array(
        "random_key0" => array("name"=>"foo","price"=>25), 
        "random_key1" => array("name"=>"Bar","price"=>"")
    ),
    array("name"=>125,"price"=>""), 
    array("another_key0" => array(
        "name" => "foo",
        "options" => "Options here",
        "special0" => array("name"=>"Special Name","price"=>""),
        "special1" => array("name"=>"Special 2","price"=>120),
    ))
);

function test_alter(&$item, $key)
{
    if ($key=="price" && empty($item))
        $item = 0;
}

function test_print($item2, $key)
{
    echo "$key. $item2<br>\n";
}

echo "Before ...:\n";
array_walk_recursive($test, 'test_print');

// now actually modify values
array_walk_recursive($test, 'test_alter');

echo "... and afterwards:\n";
array_walk_recursive($test, 'test_print');
?>

Actually i saw i was too slow, but here you got sample for non-modifying recursive function as well :)

Sign up to request clarification or add additional context in comments.

Comments

0

You can use array_walk_recursive, for example like this:

<?php
function update_price(&$item, $key) {
    if ($key == 'price' && !$item) {
        $item = 0;
    }
}
$test = array('key1' => array('price' => null, 'test' => 'abc', 'sub' => array('price' => 123), 'sub2' => array('price' => null)));
array_walk_recursive($test, 'update_price');
print_r($test);

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.