last day (15 days later) » 

3:50 PM
1
Q: String path to array

Morgan Touverey QuillingI want to convert HTML-style form array to PHP array. I tried a lot of solutions but I only get errors or reference ("pointer") problems. The HTML array pathes are formatted like this : tree[] tree2[key1] tree2[key2][key3] tree3[][key4] Here is my method that builds the array, where I remove...

 
Can you show your input data?
 
The $name argument is the raw HTML-like array node name, tree[key][] in the method example, or like the possible values in the list at the top of the post. The $content argument is a string.
 
Without understanding your whole code and problem at moment,!$firstKey = strpos($name, '[') cannot work. You need to put the assignment between parentheses: !($firstKey = strpos($name, '['))
 
Not necessary :) This works!
 
Ok, then I post it as an answer ... I wasn't sure if that was all..
 
3:50 PM
Sorry, I said "not necessary" for the parentheses. Actually, $firstKey is assigned correctly and then $treeRootName too. Maybe the parentheses were necessary in previous versions of PHP, but currently with 5.6.6 it works. The code cannot work here because it is incomplete. Look at the lines beginning with "Here I want to build the array using", that's where the array should be built according to the $treeRootName.
 
This has nothing to do with the php version. Its because of operator precedence which has not changed in any PHP version. !$firstKey gets executed before the assignment
 
This is not true, at least in that 'if' case... The assignment is being done before. If !$firstkey would be executed first, I would get an Undefined variable notice, then the else would never be reached, and that's not the case. This code works and the parentheses are useless.
 
!$a = strpos('foo bar', 'bar'); What would you expect is $a 's value?
 
And why do you use the ! ?
 
3:58 PM
Because strpos return false if the substring is not found into the haystack
In that case, the field name is not an array notation
 
The right syntax would be if(($a = strpos($haystack, $needle)) === false)
note the === since $a could be set to 0 if haystack starts with $needle. This would be evaluated to false unless you use === false .
 
Yes, but in my case, no. Because I also consider that a field name beginning with "[" is not an array component but a simple name.
So the use of "!" is perfect.
 
4:12 PM
And just about the operator precedence : you should try this code, it has exactly the same behaviour with, or without parentheses :

<?php

if(!$a = strpos('foo bar', 'bar'))
{
var_dump('ko');
}
else
{
var_dump('ok = ' . $a);
}
 
4:32 PM
what does this show?
 
The parentheses have no effect
 
sorry, I'm eating and have just two fingers free for typing atm.. will respond in 5 min
 
No problem!
I have just done an opcode dump of this code, with and without parentheses. There is a difference, but I can't tell what is changing exactly because the result is the same.
 
4:59 PM
so you don't care about items which a) contain no [ or b) contain [ at position zero?
 
Yes, these types of values are considered as simple names :
notATree
[notATree]

But this is treated in the else:
tree[]
tree[key]
tree[][key]
 
ok, then the statement is ok
note that I was wrong when saying the ! operation applies before the assignment
 
5:57 PM
Ok, so there is definitely no problem with this statement. Indeed, there is no problem in the code I posted, the question was just about the way to build an array. However, thanks for your interest!
 

last day (15 days later) »