?
avatar koutja
koutja
20 Sep 2018

Steps to reproduce the issue

I have

$data = array(
    'user' => array(
        'email'   => 'user@example.com',
        'name'    => 'Super User',
        'address' => array(
            'billing' => 'Street 1',
            'delivery' => 'Street 2'
        )
    ),
    'post' => 'Hello, World!'
);

I invoke method

$result = \Joomla\Utilities\ArrayHelper::flatten($data);

Expected result

$result = array(
    'user.email' => 'user@example.com',
    'user.name'  => 'Super User',
    'user.address.billing'  => 'Street 1',
    'user.address.delivery' => 'Street 2',
    'post'       => 'Hello, World!'
);

Actual result

Array
(
    [user] => Array
        (
            [email] => user@example.com
            [name] => Super User
            [address] => Array
                (
                    [billing] => Street 1
                    [delivery] => Street 2
                )

        )

    [post] => Hello, World!
    [email] => user@example.com
    [name] => Super User
    [address] => Array
        (
            [billing] => Street 1
            [delivery] => Street 2
        )

    [user.email] => user@example.com
    [user.name] => Super User
    [billing] => Street 1
    [delivery] => Street 2
    [user.address.billing] => Street 1
    [user.address.delivery] => Street 2
)

System information (as much as possible)

Php 7.2

Additional comments

This can be fixed as follows

    public static function flatten($array, $separator = '.', $prefix = '')
    {
        $result = [];
        if ($array instanceof \Traversable) {
            $array = iterator_to_array($array);
        } elseif (is_object($array)) {
            $array = get_object_vars($array);
        }

        foreach ($array as $k => $v) {
            $key = $prefix ? $prefix . $separator . $k : $k;

            if (is_object($v) || is_array($v)) {
                $result = array_merge($result, static::flatten($v, $separator, $key));
            } else {
                $result[$key] = $v;
            }
        }

        return $result;
    }
avatar koutja koutja - open - 20 Sep 2018
avatar joomla-cms-bot joomla-cms-bot - change - 20 Sep 2018
Labels Added: ?
avatar joomla-cms-bot joomla-cms-bot - labeled - 20 Sep 2018
avatar koutja koutja - change - 20 Sep 2018
The description was changed
avatar koutja koutja - edited - 20 Sep 2018
avatar koutja koutja - change - 20 Sep 2018
The description was changed
avatar koutja koutja - edited - 20 Sep 2018
avatar mbabker
mbabker - comment - 20 Sep 2018

Please submit this change as a pull request to https://github.com/joomla-framework/utilities (if I'm not mistaken the bug is that the flattened result gets assigned to the injected $array parameter and your fix is to assign the result to a new $result parameter, right?).

avatar franz-wohlkoenig franz-wohlkoenig - change - 20 Sep 2018
Status New Information Required
avatar franz-wohlkoenig franz-wohlkoenig - change - 20 Sep 2018
Category Repository
avatar mbabker mbabker - change - 17 Oct 2018
Status Information Required Closed
Closed_Date 0000-00-00 00:00:00 2018-10-17 23:07:56
Closed_By mbabker
avatar mbabker
mbabker - comment - 17 Oct 2018

This is already fixed in the 2.0 branch of the Utilities package, to be included in Joomla 4. Because fixing this results in a B/C break in the returned data, we're going to leave it this way. The test on the 2.0 branch for this method has been improved to test the full return result instead of only validating one key is correctly flattened.

avatar mbabker mbabker - close - 17 Oct 2018

Add a Comment

Login with GitHub to post a comment