php-general Digest 24 Mar 2012 12:39:43 -0000 Issue 7742

Topics (messages 317240 through 317245):

Re: Thinking out loud - a continuation...
        317240 by: Jay Blanchard
        317243 by: Robert Cummings
        317244 by: Robert Cummings
        317245 by: Jay Blanchard

Re: foreach weirdness
        317241 by: Simon Schick
        317242 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 ---
[snip]
…stuff….
[/snip]

For those interested here is where the problem seems to occur - 

$child = array
               (
                   'id'       => $id,
                   'parentId' => $pid,
                   'children' => array()
               );
               
               $children[$id][] = &$child;
               
               foreach( $parents[$pid] as &$items ){
                   foreach( $items as &$item ){   
                         $item['children'][$id] = &$child; // error: Cannot use 
string offset as an array
                   }
               }

Maybe someone will see this and know what's going on. Before the foreach 
$children is an array. 


--- End Message ---
--- Begin Message ---
On 12-03-23 05:26 PM, Jay Blanchard wrote:
[snip]
$item['children'] should be an array, somehow a string has been assigned :/
[/snip]

Yep. I am trying to figure that out now. I'm sure it is something really small.
[/snip]

I have been hammering away at it for a while now and still cannot find the 
issue. I'll push away for a while and come back to it. Robert I owe you so many 
thinks for getting me this far and opening me up to making this more efficient. 
I just have to push on through and get to the point where  the JSON can be 
created and consumed. If any light bulb goes on over your head would you let me 
know. I have tried everything that I know works to keep this from being a 
string - I am just missing something.

Hi Jay,

Did you send me a sample dump for your table :)

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 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.
--
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]
> Did you send me a sample dump for your table :)
[/snip]

I'll do that today. I got side-tracked last night.


--- End Message ---
--- Begin Message ---
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.

Bye
Simon

--- End Message ---
--- Begin Message ---
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.
--
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