1

I'm trying to read recursively into an array until I'm getting a string. Then I try to explode it and return the newly created array. However, for some reason it does not assign the array:

function go_in($arr) { // $arr is a multi-dimensional array

        if (is_array($arr))
            foreach($arr as & $a)
                $a = go_in($a);
        else
                return explode("\n", $arr);
}

EDIT:

Here's the array definition as printed by print_r:

Array
(
    [products] => Array
        (
            [name] => Arduino Nano Version 3.0 mit ATMEGA328P
            [id] => 10005
        )

    [listings] => Array
        (
            [category] => 
            [title] => This is the first line
This is the second line
            [subtitle] => This is the first subtitle
This is the second subtitle
            [price] => 24.95
            [quantity] => 
            [stock] => 
            [shipping_method] => Slow and cheap
            [condition] => New
            [defects] => 
        )

    [table_count] => 2
    [tables] => Array
        (
            [0] => products
            [1] => listings
        )

)
3
  • 1
    Paste the array definition please. Commented Apr 25, 2013 at 22:36
  • And the expected string at the end would be helpful too. Commented Apr 25, 2013 at 22:43
  • I edited my post. The title and subtitle are the items that contain the "\n" Commented Apr 25, 2013 at 22:44

3 Answers 3

3

I'd use this:

array_walk_recursive($array,function(&$value,$key){
        $value = explode("\n",$value);
});

However, this fixes your function:

function &go_in(&$arr) { // $arr is a multi-dimensional array
        if (is_array($arr)){
                foreach($arr as & $a) $a = go_in($a);
        } else {
                $arr = explode("\n", $arr);
        }
        return $arr;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the answer. Will use it next time.
No, not really. Never use is for speed considerations: PHP will under the hood will only actually make a copy when one of the values are changed: if you perforam $a = $b; or $a = & $b, they will still point to the same memory location, an actual copy of $b into $a when not assiging as a reference will only be created when either $a or $b is modified.
0

When writing nested conditions/loops - always add braces for better readability and to prevent bugs.. Also you should return the go_in function, because it is recursive, it needs to be passed to the calling function instance.

function go_in($arr) { // $arr is a multi-dimensional array

        if (is_array($arr))
        {
            foreach($arr as &$a)
            {
                return go_in($a);
            }
        }
        else
        {
                return ($arr);
        }
}

2 Comments

Return at the first item in an array? What about the rest of them?
@Wrikken: I misread I'm trying to read recursively into an array until I'm getting a string to think the OP wanted to iterate until s/he got the first non-array element. Hence I returned the first non-array item.
0

The original array was not returned in the function:

function go_in($arr) {

    if (is_array($arr))
        foreach($arr as &$a)
            $a = go_in($a);
    else
         if (strpos($arr, "\n") !== false)
            return explode("\n", $arr);

    return $arr;
}

EDIT:

Now, it only really edits the strings that contain a linebreak. Before it would edit every string which meant that every string was returned as an array.

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.