Re: Thinking out loud - a continuation...

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]



On 12-03-26 07:05 PM, Jay Blanchard wrote:
[snip]
On Mar 26, 2012, at 5:58 PM, Robert Cummings wrote:

On 12-03-26 06:52 PM, Jay Blanchard wrote:
[snip]
Did you end up with a satisfactory output? It's not overly difficult to generate an array instead of an object.
[/snip]

I did for all but this one instance. Are you saying that it would be easy to make of the children arrays? I thought they were already - am I missing something?

They are arrays... but JSON_encode is creating objects. You can create arrays by traversing the array structure recursively and outputing your own JavaScript code to build a JavaScript array. I don't know if that would serve the purpose, but you would end up with an array.
[/snip]

I'm listening - so could this be added to the code that you just wrote? Or do I need to recurse the output from json_encode()?

I think you need two things... the recursive post processor that removes the string indexes for the children. And then a function that creates a JavaScript array expression from an object or array. The question I have for you... is given the following array structure that might be generated from my previous code:

<?php

array
(
    'exec-001' => array
    (
        'name' => 'exec-001',
        'children' => array
        (
            'sub-exec-011' => array
            (
                'name' => 'sub-exec-011',
                'children' => array
                (
                    'sub-sub-exec-111' => array
                    (
                        'name' => 'sub-sub-exec-111',
                        'children' => array()
                    ),
                    'sub-sub-exec-112' => array
                    (
                        'name' => 'sub-sub-exec-112',
                        'children' => array()
                    )
                )
            ),
            'sub-exec-012' => array
            (
                'name' => 'sub-exec-012',
                'children' => array
                (
                    'sub-sub-exec-121' => array
                    (
                        'name' => 'sub-sub-exec-121',
                        'children' => array()
                    ),
                    'sub-sub-exec-122' => array
                    (
                        'name' => 'sub-sub-exec-122',
                        'children' => array()
                    )
                )
            )
        )
    ),
    'exec-002' => array
    (
        'name' => 'exec-002',
        'children' => array
        (
            'sub-exec-021' => array
            (
                'name' => 'sub-exec-021',
                'children' => array
                (
                    'sub-sub-exec-211' => array
                    (
                        'name' => 'sub-sub-exec-211',
                        'children' => array()
                    ),
                    'sub-sub-exec-212' => array
                    (
                        'name' => 'sub-sub-exec-212',
                        'children' => array()
                    )
                )
            ),
            'sub-exec-022' => array
            (
                'name' => 'sub-exec-022',
                'children' => array
                (
                    'sub-sub-exec-221' => array
                    (
                        'name' => 'sub-sub-exec-221',
                        'children' => array()
                    ),
                    'sub-sub-exec-222' => array
                    (
                        'name' => 'sub-sub-exec-222',
                        'children' => array()
                    )
                )
            )
        )
    )
);

?>

On first blush, I think you want the following structure (from your recent posts):

<?php

array
(
    0 => array
    (
        'name' => 'exec-001',
        'children' => array
        (
            0 => array
            (
                'name' => 'sub-exec-011',
                'children' => array
                (
                    0 => array
                    (
                        'name' => 'sub-sub-exec-111',
                        'children' => array()
                    ),
                    1 => array
                    (
                        'name' => 'sub-sub-exec-112',
                        'children' => array()
                    )
                )
            ),
            1 => array
            (
                'name' => 'sub-exec-012',
                'children' => array
                (
                    0 => array
                    (
                        'name' => 'sub-sub-exec-121',
                        'children' => array()
                    ),
                    1 => array
                    (
                        'name' => 'sub-sub-exec-122',
                        'children' => array()
                    )
                )
            )
        )
    ),
    1 => array
    (
        'name' => 'exec-002',
        'children' => array
        (
            0 => array
            (
                'name' => 'sub-exec-021',
                'children' => array
                (
                    0 => array
                    (
                        'name' => 'sub-sub-exec-211',
                        'children' => array()
                    ),
                    1 => array
                    (
                        'name' => 'sub-sub-exec-212',
                        'children' => array()
                    )
                )
            ),
            1 => array
            (
                'name' => 'sub-exec-022',
                'children' => array
                (
                    0 => array
                    (
                        'name' => 'sub-sub-exec-221',
                        'children' => array()
                    ),
                    1 => array
                    (
                        'name' => 'sub-sub-exec-222',
                        'children' => array()
                    )
                )
            )
        )
    )
);

?>

Essentially, entries at the root and entries for the children are just auto indexed array items but the actual entries in those arrays retain the associative index structure for retrieval of the specific information. let me know and I can probably whip you up something.

Cheers,
Rob.
--
E-Mail Disclaimer: Information contained in this message and any
attached documents is considered confidential and legally protected.
This message is intended solely for the addressee(s). Disclosure,
copying, and distribution are prohibited unless authorized.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]
  Powered by Linux