Re: [PHP] Dynamically creating multi-array field
On Sun, 2008-10-26 at 22:39 -0700, Jim Lucas wrote: Even slimmer ?php $node = '[5][1][]'; $text = 'some text'; preg_match_all('|\[([^\]\[]*)\]|', $node, $matches, PREG_PATTERN_ORDER); $recursive = $matches[1]; $recursive = array_reverse($recursive); foreach ( $recursive AS $index ) { $out = array(); $out[(int)$index] = $text; $text = $out; } print_r($out); ? It's buggy... you need to test for an blank string to properly handle the append to array case when the index is blank []. ?php $node = '[5][1][]'; $text = 'some text'; preg_match_all( '|\[([^\]\[]*)\]|', $node, $matches, PREG_PATTERN_ORDER ); $recursive = $matches[1]; $recursive = array_reverse($recursive); foreach( $recursive AS $index ) { $out = array(); if( trim( $index ) === '' ) { $out[] = $text; } else { $out[$index] = $text; } $text = $out; } print_r( $out ); ? I also removed the (int) cast since integer keys will be cast automatically by PHP and then it will have support for text keys also. Cheers, Rob. -- http://www.interjinn.com Application and Templating Framework for PHP -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Dynamically creating multi-array field
On Mon, 2008-10-27 at 02:09 -0400, Robert Cummings wrote: On Sun, 2008-10-26 at 22:39 -0700, Jim Lucas wrote: Even slimmer ?php $node = '[5][1][]'; $text = 'some text'; preg_match_all('|\[([^\]\[]*)\]|', $node, $matches, PREG_PATTERN_ORDER); $recursive = $matches[1]; $recursive = array_reverse($recursive); foreach ( $recursive AS $index ) { $out = array(); $out[(int)$index] = $text; $text = $out; } print_r($out); ? It's buggy... you need to test for an blank string to properly handle the append to array case when the index is blank []. ?php $node = '[5][1][]'; $text = 'some text'; preg_match_all( '|\[([^\]\[]*)\]|', $node, $matches, PREG_PATTERN_ORDER ); $recursive = $matches[1]; $recursive = array_reverse($recursive); foreach( $recursive AS $index ) { $out = array(); if( trim( $index ) === '' ) I probably shouldn't trim... since we're not supporting quotes in any way, it might be desirable to have a key that is one or more spaces... for whatever reason :) Personally, I have a similar implementation I use all the time, but I use forward slashes to separate keys. ?php hashPathSet( $hash, 'this/is/the/hash/path', $value ) ? Cheers, Rob. { $out[] = $text; } else { $out[$index] = $text; } $text = $out; } print_r( $out ); ? I also removed the (int) cast since integer keys will be cast automatically by PHP and then it will have support for text keys also. -- http://www.interjinn.com Application and Templating Framework for PHP -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Dynamically creating multi-array field
:-D :-D :-D :-D :-D :-D :-D :-D ok :) Robert Cummings napsal(a): On Mon, 2008-10-27 at 02:09 -0400, Robert Cummings wrote: On Sun, 2008-10-26 at 22:39 -0700, Jim Lucas wrote: Even slimmer ?php $node = '[5][1][]'; $text = 'some text'; preg_match_all('|\[([^\]\[]*)\]|', $node, $matches, PREG_PATTERN_ORDER); $recursive = $matches[1]; $recursive = array_reverse($recursive); foreach ( $recursive AS $index ) { $out = array(); $out[(int)$index] = $text; $text = $out; } print_r($out); ? It's buggy... you need to test for an blank string to properly handle the append to array case when the index is blank []. ?php $node = '[5][1][]'; $text = 'some text'; preg_match_all( '|\[([^\]\[]*)\]|', $node, $matches, PREG_PATTERN_ORDER ); $recursive = $matches[1]; $recursive = array_reverse($recursive); foreach( $recursive AS $index ) { $out = array(); if( trim( $index ) === '' ) I probably shouldn't trim... since we're not supporting quotes in any way, it might be desirable to have a key that is one or more spaces... for whatever reason :) Personally, I have a similar implementation I use all the time, but I use forward slashes to separate keys. ?php hashPathSet( $hash, 'this/is/the/hash/path', $value ) ? Cheers, Rob. { $out[] = $text; } else { $out[$index] = $text; } $text = $out; } print_r( $out ); ? I also removed the (int) cast since integer keys will be cast automatically by PHP and then it will have support for text keys also. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Dynamically creating multi-array field
2008/10/26 Martin Zvarík [EMAIL PROTECTED]: PHP Version 5.2.4 ? $node = '[5][1][]'; ${'tpl'.$node} = 'some text'; print_r($tpl); // null ? I really don't like to use the EVAL function, but do I have choice?? This sucks. Hi there, While this question can spur some neat solutions, it raises a red flag in that if you need to do this, you probably need to rethink things a bit. In cases like this it is easier for people to help if you describe the actual problem you are trying to solve, not how you think it needs to be solved. It could be that you don't really need weird (but beautiful, like Jim's idea) solutions. If you explain why you believe that you need to do this in the first place, maybe someone can suggest something else which doesn't require a weird solution (however beautiful). Torben -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Dynamically creating multi-array field
2008/10/26 Martin Zvarík [EMAIL PROTECTED]: PHP Version 5.2.4 ? $node = '[5][1][]'; ${'tpl'.$node} = 'some text'; print_r($tpl); // null ? I really don't like to use the EVAL function, but do I have choice?? This sucks. Hi there, While this question can spur some neat solutions, it raises a red flag in that if you need to do this, you probably need to rethink things a bit. In cases like this it is easier for people to help if you describe the actual problem you are trying to solve, not how you think it needs to be solved. It could be that you don't really need weird (but beautiful, like Jim's idea) solutions. If you explain why you believe that you need to do this in the first place, maybe someone can suggest something else which doesn't require a weird solution (however beautiful). Torben Hi, I am aware of this, but explaining my problem in this case would take me an hour --- and eventually would lead to a) misunderstanding, b) weird solution, c) no solution... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Dynamically creating multi-array field
2008/10/27 Martin Zvarík [EMAIL PROTECTED]: Hi, I am aware of this, but explaining my problem in this case would take me an hour --- and eventually would lead to a) misunderstanding, b) weird solution, c) no solution... Forgive me if I misunderstand, but it seems like you are willing to trade off an hour at the beginning of the project at the expense of perhaps many hours of pain later on. Has this thread not already taken more than an hour? I find that often if I have a weird question, just taking the time to formulate and write a good descriptive post to explain the problem helps me understand why I'm going at it the wrong way to start with. I remember years ago being faced with the same problem you now have. It turned out that the most elegant solution was to change the design so that I didn't need to solve the problem at all. Torben -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Dynamically creating multi-array field
At 12:33 AM -0700 10/27/08, Lars Torben Wilson wrote: 2008/10/27 Martin Zvarík [EMAIL PROTECTED]: Hi, I am aware of this, but explaining my problem in this case would take me an hour --- and eventually would lead to a) misunderstanding, b) weird solution, c) no solution... Forgive me if I misunderstand, but it seems like you are willing to trade off an hour at the beginning of the project at the expense of perhaps many hours of pain later on. Has this thread not already taken more than an hour? I find that often if I have a weird question, just taking the time to formulate and write a good descriptive post to explain the problem helps me understand why I'm going at it the wrong way to start with. I remember years ago being faced with the same problem you now have. It turned out that the most elegant solution was to change the design so that I didn't need to solve the problem at all. Torben I agree with Torben. Quite often when I formulate a question for this group, the answer appears before I send the question. One needs to fully understand the problem themselves and preparing the right question helps in finding a solution. Of course, you can do like so many others do and just don't worry about it -- expose your ignorance to the group and hope for some magical code. Despite my own advice, I've done that several times. That works too, but has it's downside. Cheers, tedd -- --- http://sperling.com http://ancientstones.com http://earthstones.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Dynamically creating multi-array field
On Mon, 2008-10-27 at 00:33 -0700, Lars Torben Wilson wrote: 2008/10/27 Martin Zvarík [EMAIL PROTECTED]: Hi, I am aware of this, but explaining my problem in this case would take me an hour --- and eventually would lead to a) misunderstanding, b) weird solution, c) no solution... Forgive me if I misunderstand, but it seems like you are willing to trade off an hour at the beginning of the project at the expense of perhaps many hours of pain later on. Has this thread not already taken more than an hour? Took me 3 minutes :) Cheers, Rob. -- http://www.interjinn.com Application and Templating Framework for PHP -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Dynamically creating multi-array field
On Mon, 2008-10-27 at 08:03 -0400, tedd wrote: At 12:33 AM -0700 10/27/08, Lars Torben Wilson wrote: 2008/10/27 Martin Zvarík [EMAIL PROTECTED]: Hi, I am aware of this, but explaining my problem in this case would take me an hour --- and eventually would lead to a) misunderstanding, b) weird solution, c) no solution... Forgive me if I misunderstand, but it seems like you are willing to trade off an hour at the beginning of the project at the expense of perhaps many hours of pain later on. Has this thread not already taken more than an hour? I find that often if I have a weird question, just taking the time to formulate and write a good descriptive post to explain the problem helps me understand why I'm going at it the wrong way to start with. I remember years ago being faced with the same problem you now have. It turned out that the most elegant solution was to change the design so that I didn't need to solve the problem at all. Torben I agree with Torben. Quite often when I formulate a question for this group, the answer appears before I send the question. One needs to fully understand the problem themselves and preparing the right question helps in finding a solution. Of course, you can do like so many others do and just don't worry about it -- expose your ignorance to the group and hope for some magical code. Despite my own advice, I've done that several times. That works too, but has it's downside. Not to necessarily sweep aside the new issue being raised, but there are certainly times when the kind of thing being requested are indeed desireable. Cheers, Rob. -- http://www.interjinn.com Application and Templating Framework for PHP -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Dynamically creating multi-array field
PHP Version 5.2.4 ? $node = '[5][1][]'; ${'tpl'.$node} = 'some text'; print_r($tpl); // null ? I really don't like to use the EVAL function, but do I have choice?? This sucks. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Dynamically creating multi-array field
Martin Zvarík wrote: PHP Version 5.2.4 ? $node = '[5][1][]'; ${'tpl'.$node} = 'some text'; print_r($tpl); // null ? I really don't like to use the EVAL function, but do I have choice?? This sucks. You should print the results that you are looking for! Are you looking for something like this? Array ( [5] = Array ( [1] = Array ( [0] = some text ) ) ) how is the $node string being created? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Dynamically creating multi-array field
No offense, but I thought it's obvious what I want to print. print_r() shows null, and it should print what you just wrote = array field. It works when first defining with eval(): eval('$tpl'.$node.'=array();'); I guess that's the only way. Anyway, I appreciate your quick reply, Martin Jim Lucas napsal(a): Martin Zvarík wrote: PHP Version 5.2.4 ? $node = '[5][1][]'; ${'tpl'.$node} = 'some text'; print_r($tpl); // null ? I really don't like to use the EVAL function, but do I have choice?? This sucks. You should print the results that you are looking for! Are you looking for something like this? Array ( [5] = Array ( [1] = Array ( [0] = some text ) ) ) how is the $node string being created? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Dynamically creating multi-array field
Nope, you have to use the eval() everytime for read/write. Martin Zvarík napsal(a): No offense, but I thought it's obvious what I want to print. print_r() shows null, and it should print what you just wrote = array field. It works when first defining with eval(): eval('$tpl'.$node.'=array();'); I guess that's the only way. Anyway, I appreciate your quick reply, Martin Jim Lucas napsal(a): Martin Zvarík wrote: PHP Version 5.2.4 ? $node = '[5][1][]'; ${'tpl'.$node} = 'some text'; print_r($tpl); // null ? I really don't like to use the EVAL function, but do I have choice?? This sucks. You should print the results that you are looking for! Are you looking for something like this? Array ( [5] = Array ( [1] = Array ( [0] = some text ) ) ) how is the $node string being created? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Dynamically creating multi-array field
Martin Zvarík wrote: Nope, you have to use the eval() everytime for read/write. Wrong. Their is always more then one way to skin a cat! ?php $node = '[5][1][]'; $text = 'some text'; preg_match_all('|\[([^\]\[]*)\]|', $node, $matches, PREG_PATTERN_ORDER); $recursive = $matches[1]; $recursive = array_reverse($recursive); $index = array_shift($recursive); $in = array((int)$index = $text); $out = array(); foreach ( $recursive AS $index ) { $out = array(); $out[(int)$index] = $in; $in = $out; } print_r($out); ? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Dynamically creating multi-array field
Jim Lucas wrote: Martin Zvarík wrote: Nope, you have to use the eval() everytime for read/write. Wrong. Their is always more then one way to skin a cat! ?php $node = '[5][1][]'; $text = 'some text'; preg_match_all('|\[([^\]\[]*)\]|', $node, $matches, PREG_PATTERN_ORDER); $recursive = $matches[1]; $recursive = array_reverse($recursive); $index = array_shift($recursive); $in = array((int)$index = $text); $out = array(); foreach ( $recursive AS $index ) { $out = array(); $out[(int)$index] = $in; $in = $out; } print_r($out); ? Even slimmer ?php $node = '[5][1][]'; $text = 'some text'; preg_match_all('|\[([^\]\[]*)\]|', $node, $matches, PREG_PATTERN_ORDER); $recursive = $matches[1]; $recursive = array_reverse($recursive); foreach ( $recursive AS $index ) { $out = array(); $out[(int)$index] = $text; $text = $out; } print_r($out); ? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php