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

2012-03-24 Thread php-general-digest-help

php-general Digest 24 Mar 2012 12:39:43 - 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


--
---BeginMessage---
[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---
---BeginMessage---

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---
---BeginMessage---

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---
---BeginMessage---
[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---
---BeginMessage---
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---
---BeginMessage---

On 12-03-23 06:30 PM, Simon Schick wrote:

2012/3/23 Robert Cummingsrob...@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


Re: [PHP] Thinking out loud - a continuation...

2012-03-24 Thread Jay Blanchard
[snip]
 Did you send me a sample dump for your table :)
[/snip]

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


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



Re: [PHP] Thinking out loud - a continuation...

2012-03-24 Thread Jay Blanchard

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



Re: [PHP] Thinking out loud - a continuation...

2012-03-24 Thread tamouse mailing lists
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!

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




Re: [PHP] foreach weirdness

2012-03-24 Thread Al



On 3/23/2012 10:11 PM, Robert Cummings wrote:

On 12-03-23 06:30 PM, Simon Schick wrote:

2012/3/23 Robert Cummingsrob...@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.


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



Re: [PHP] foreach weirdness

2012-03-24 Thread Robert Cummings

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 Cummingsrob...@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.

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



Re: [PHP] Thinking out loud - a continuation...

2012-03-24 Thread Robert Cummings

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.

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



Re: [PHP] Thinking out loud - a continuation...

2012-03-24 Thread Jay Blanchard
[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.



Re: [PHP] Thinking out loud - a continuation...

2012-03-24 Thread Robert Cummings

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.

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



Re: [PHP] Thinking out loud - a continuation...

2012-03-24 Thread Jay Blanchard
[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!



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



Re: [PHP] Thinking out loud - a continuation...

2012-03-24 Thread Jay Blanchard
[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.



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



Re: [PHP] Thinking out loud - a continuation...

2012-03-24 Thread Jay Blanchard
[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.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Thinking out loud - a continuation...

2012-03-24 Thread Robert Cummings

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.

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