php-general Digest 25 Mar 2012 02:18:27 -0000 Issue 7743

Topics (messages 317246 through 317256):

Re: Thinking out loud - a continuation...
        317246 by: Jay Blanchard
        317247 by: tamouse mailing lists
        317250 by: Robert Cummings
        317251 by: Jay Blanchard
        317252 by: Robert Cummings
        317253 by: Jay Blanchard
        317254 by: Jay Blanchard
        317255 by: Jay Blanchard
        317256 by: Robert Cummings

Re: foreach weirdness
        317248 by: Al
        317249 by: Robert Cummings

Administrivia:

To subscribe to the digest, e-mail:
        php-general-digest-subscr...@lists.php.net

To unsubscribe from the digest, e-mail:
        php-general-digest-unsubscr...@lists.php.net

To post to the list, e-mail:
        php-gene...@lists.php.net


----------------------------------------------------------------------
--- Begin Message ---
On Mar 23, 2012, at 11:24 PM, Robert Cummings wrote:

> On 12-03-23 05:41 PM, Jay Blanchard wrote:
>> [-- DELETED GARBAGE --]  :)
> 
> I just realized... I've been stuck in a thinking rut. I latched onto one 
> solution that works well in some case but didn't fully examine the nuances of 
> your own scenario. Given the way you are creating your hierarchy you will 
> ultimately retrieve all rows. As such the following simple solution will do 
> what you need:
> 
> <?php
> 
>    $company = 1;
> 
>    $query =
>        "SELECT DISTINCT "
>       ."   * "
>       ."FROM "
>       ."   tiers "
>       ."WHERE "
>       ."   company = {$company} ";
> 
>    $root = array();
>    if( $db->query( $query ) )
>    {
>        while( ($row = $db->fetchRow()) )
>        {
>            $focus = &$root;
>            for( $i = 1; $i <= 14; $i++ )
>            {
>                $name = $row['tier'.$i];
> 
>                if( !isset( $focus[$name] ) )
>                {
>                    $focus[$name] = array();
>                }
> 
>                $focus = &$focus[$name];
>            }
>        }
>    }
> 
>    $json = JSON_encode( $root );
> 
> ?>
> 
> Cheers,
> Rob.
> -- 

At first blush I'm not sure how this would work - but I haven't had any coffee 
yet either. I'll give this a shot in a little while. Seems almost too easy.

--- End Message ---
--- Begin Message ---
On Sat, Mar 24, 2012 at 7:41 AM, Jay Blanchard
<jay.blanch...@sigmaphinothing.org> wrote:
>
> On Mar 23, 2012, at 11:24 PM, Robert Cummings wrote:
>
>> On 12-03-23 05:41 PM, Jay Blanchard wrote:
>>> [-- DELETED GARBAGE --]  :)
>>
>> I just realized... I've been stuck in a thinking rut. I latched onto one 
>> solution that works well in some case but didn't fully examine the nuances 
>> of your own scenario. Given the way you are creating your hierarchy you will 
>> ultimately retrieve all rows. As such the following simple solution will do 
>> what you need:
>>
>> <?php
>>
>>    $company = 1;
>>
>>    $query =
>>        "SELECT DISTINCT "
>>       ."   * "
>>       ."FROM "
>>       ."   tiers "
>>       ."WHERE "
>>       ."   company = {$company} ";
>>
>>    $root = array();
>>    if( $db->query( $query ) )
>>    {
>>        while( ($row = $db->fetchRow()) )
>>        {
>>            $focus = &$root;
>>            for( $i = 1; $i <= 14; $i++ )
>>            {
>>                $name = $row['tier'.$i];
>>
>>                if( !isset( $focus[$name] ) )
>>                {
>>                    $focus[$name] = array();
>>                }
>>
>>                $focus = &$focus[$name];
>>            }
>>        }
>>    }
>>
>>    $json = JSON_encode( $root );
>>
>> ?>
>>
>> Cheers,
>> Rob.
>> --
>
> At first blush I'm not sure how this would work - but I haven't had any 
> coffee yet either. I'll give this a shot in a little while. Seems almost too 
> easy.
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>

This has been fascinating to read. I hope you get it figured out, Jay!

--- End Message ---
--- Begin Message ---
On 12-03-24 08:41 AM, Jay Blanchard wrote:

On Mar 23, 2012, at 11:24 PM, Robert Cummings wrote:

On 12-03-23 05:41 PM, Jay Blanchard wrote:
[-- DELETED GARBAGE --]  :)

I just realized... I've been stuck in a thinking rut. I latched onto one 
solution that works well in some case but didn't fully examine the nuances of 
your own scenario. Given the way you are creating your hierarchy you will 
ultimately retrieve all rows. As such the following simple solution will do 
what you need:

<?php

    $company = 1;

    $query =
        "SELECT DISTINCT "
       ."   * "
       ."FROM "
       ."   tiers "
       ."WHERE "
       ."   company = {$company} ";

    $root = array();
    if( $db->query( $query ) )
    {
        while( ($row = $db->fetchRow()) )
        {
            $focus =&$root;
            for( $i = 1; $i<= 14; $i++ )
            {
                $name = $row['tier'.$i];

                if( !isset( $focus[$name] ) )
                {
                    $focus[$name] = array();
                }

                $focus =&$focus[$name];
            }
        }
    }

    $json = JSON_encode( $root );

?>

The crux of it is:

    $focus = &$focus[$name];

because it facilitates moving deeper and deeper into the array.

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.

--- End Message ---
--- Begin Message ---
[snip]The crux of it is:
> 
>    $focus = &$focus[$name];
> [/snip]

It works as I expect so far. All I have to do is figure out how to make the 
element a name: element in the JSON. for instance an element would look like 
this

{
   name: "Bob",
   children: []
}

So the PHP array would have to look like this - 

$json = array (
        name => "Bob",
        children => array (
            name => "Angie"
        )
)

and so on. This is for one of the services that will consume the JSON.


--- End Message ---
--- Begin Message ---
On 12-03-24 01:09 PM, Jay Blanchard wrote:
[snip]The crux of it is:

    $focus =&$focus[$name];
[/snip]

It works as I expect so far. All I have to do is figure out how to make the 
element a name: element in the JSON. for instance an element would look like 
this

{
    name: "Bob",
    children: []
}

So the PHP array would have to look like this -

$json = array (
        name =>  "Bob",
        children =>  array (
            name =>  "Angie"
        )
)

and so on. This is for one of the services that will consume the JSON.

A little tweak here... a little tweak there:

<?php

    //
    // Establish the root.
    //

    $company = 1;

    $query =
        "SELECT DISTINCT "
       ."   * "
       ."FROM "
       ."   tiers "
       ."WHERE "
       ."   company = {$company} ";

    $root = array();
    if( $db->query( $query ) )
    {
        while( ($row = $db->fetchRow()) )
        {
            $focus = &$root;
            for( $i = 1; $i <= 14; $i++ )
            {
                $name = $row['tier'.$i];

                if( !isset( $focus[$name] ) )
                {
                    $focus[$name] = array
                    (
                        'name' => $name,
                        'children' => array(),
                    );
                }

                $focus = &$focus[$name]['children'];
            }
        }
    }

    $json = JSON_encode( $root );
?>
--
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.

--- End Message ---
--- Begin Message ---
[snip]
> A little tweak here... a little tweak there:
[/snip]
> 
> 
>                if( !isset( $focus[$name] ) )
>                {
>                    $focus[$name] = array
>                    (
>                        'name' => $name,
>                        'children' => array(),
>                    );
>                }
> 
>                $focus = &$focus[$name]['children'];
>            }
>        }
>    }
[/snip]

Bingo! We have a winner! I had already started on the $focus[$name] array as 
you have shown, I just hadn't figured out the &$focus[$name]['children'] part. 
Thank you so much Robert - you're a real asset to the PHP community!



--- End Message ---
--- Begin Message ---
[snip]
> A little tweak here... a little tweak there:
[/snip]

One more little tweak may be required. The JSON looks like this

{"Executives and Management":{"name":"Executives and Management","children":{

The first part {"Executives and Management": needs to be removed. I think I 
know what to do.



--- End Message ---
--- Begin Message ---
[snip]
> One more little tweak may be required. The JSON looks like this
> 
> {"Executives and Management":{"name":"Executives and Management","children":{
> 
> The first part {"Executives and Management": needs to be removed. I think I 
> know what to do.
[/snip]

It has become painfully obvious that I do not know what to do. Ready to call it 
a day. Besides I just burned my finger setting up the smoker for the ribs.

--- End Message ---
--- Begin Message ---
On 12-03-24 04:11 PM, Jay Blanchard wrote:
[snip]
One more little tweak may be required. The JSON looks like this

{"Executives and Management":{"name":"Executives and Management","children":{

The first part {"Executives and Management": needs to be removed. I think I 
know what to do.
[/snip]

It has become painfully obvious that I do not know what to do. Ready to call it 
a day. Besides I just burned my finger setting up the smoker for the ribs.

It's a necessary part of building the structure. It can be removed but only as a post process. Why does it have to be removed? You can loop through the structure in JavaScript without paying heed to the key's value.

If it absolutely must go... you need to recurse through the final structure replacing each "children" entry with the results of passing it through array_values().

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.

--- End Message ---
--- Begin Message ---


On 3/23/2012 10:11 PM, Robert Cummings wrote:
On 12-03-23 06:30 PM, Simon Schick wrote:
2012/3/23 Robert Cummings<rob...@interjinn.com>

On 12-03-23 11:16 AM, Arno Kuhl wrote:


it still does not produce the correct result:
0 1 3 6 10 15 21
0 1 3 6 10 15 15


This looks like a bug... the last row should be the same. What version of
PHP are you using? Have you checked the online bug reports?



Hi, Robert

Does not seem like a bug to me ...
http://schlueters.de/blog/archives/141-References-and-foreach.html

What you should do to get the expected result:
Unset the variable after you don't need this reference any longer.

Ah yes... that clued me in. I disagree with the article's generalization with
respect to references since references accomplish some things that cannot be
accomplished otherwise, but even I missed the fact that the second loop was
using a variable that was a reference to the last element of the array as
created in the first loop *lol*. The user's very act of checking their results
was confounding the result... I love it :)

Cheers,
Rob.

Re, your "...that cannot be accomplished otherwise,..." Can you provide some examples? The only ones I've found are when using create_function() and the arguments for callback functions. I can't even remember or find in my code an example of my foreach()loops needed it. Seems, I recall earlier versions of PHP [<4? ]required references for variables.
--- End Message ---
--- Begin Message ---
On 12-03-24 11:15 AM, Al wrote:


On 3/23/2012 10:11 PM, Robert Cummings wrote:
On 12-03-23 06:30 PM, Simon Schick wrote:
2012/3/23 Robert Cummings<rob...@interjinn.com>

On 12-03-23 11:16 AM, Arno Kuhl wrote:


it still does not produce the correct result:
0 1 3 6 10 15 21
0 1 3 6 10 15 15


This looks like a bug... the last row should be the same. What version of
PHP are you using? Have you checked the online bug reports?



Hi, Robert

Does not seem like a bug to me ...
http://schlueters.de/blog/archives/141-References-and-foreach.html

What you should do to get the expected result:
Unset the variable after you don't need this reference any longer.

Ah yes... that clued me in. I disagree with the article's generalization with
respect to references since references accomplish some things that cannot be
accomplished otherwise, but even I missed the fact that the second loop was
using a variable that was a reference to the last element of the array as
created in the first loop *lol*. The user's very act of checking their results
was confounding the result... I love it :)

Cheers,
Rob.

Re, your "...that cannot be accomplished otherwise,..." Can you provide some
examples?  The only ones I've found are when using create_function() and the
arguments for callback functions. I can't even remember or find in my code an
example of my foreach()loops needed it. Seems, I recall earlier versions of PHP
[<4? ]required references for variables.

After I submitted "...that cannot be accomplished otherwise,...", I realized it was a patently false statement (a turing machine is a turing machine :). The intent of the statement though was to indicate the greater difficulty in achieving something relatively simple with references. See the other thread "Thinking out loud - continuation". My post dated 2012-03-24 00:24 shows a process that is cumbersome and inefficient to implement in another fashion. References are like pointers... very powerful but with cautions for the unwary.

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.

--- End Message ---

Reply via email to