|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
commando Smarty Rookie
Joined: 10 Aug 2004 Posts: 7
|
Posted: Tue Aug 10, 2004 9:05 pm Post subject: tricky little array question |
|
|
Here's a slightly curly one. In the following code, "hello world" is never printed because array_search returns int 0, and the if tests against 0 or un-initialised.
Code: |
$highlight = array();
array_push($highlight, 'name');
if (array_search('name', $highlight) ) {
echo "hello world";
}
|
I have to use either of the following methods to make it work
1) Use the is_int method to check if the return is an int, rather than testing against zero
Code: |
$highlight = array();
array_push($highlight, 'name');
if (is_int(array_search('name', $highlight)) ) {
echo "hello world";
}
|
2) adding a dummy value to the start of my array
Code: |
$highlight = array();
array_push($highlight, 'unused');
array_push($highlight, 'name');
if (array_search('name', $highlight)) {
echo "hello world";
}
|
3) Is it possible to do something like this? ie specifying the array key with an autoincrementing field. It doesn't seem to work but it might just be that i'm doing it wrong...
Code: |
$highlight = array();
$highlightIndex = 1;
array_push($highlight, 'unused');
array_push($highlight, 'name');
if (array_search('name', $highlightIndex++ => $highlight)) {
echo "hello world";
}
|
Thoughts anyone? |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Tue Aug 10, 2004 9:49 pm Post subject: |
|
|
You would have better luck asking this question in a PHP forum or mailing list. |
|
Back to top |
|
commando Smarty Rookie
Joined: 10 Aug 2004 Posts: 7
|
Posted: Tue Aug 10, 2004 9:53 pm Post subject: |
|
|
Do you know of any helpful forum sites for newbies to php? |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Tue Aug 10, 2004 10:04 pm Post subject: |
|
|
IIR, there is a discussion currently on the php-internals ML atm concerning something that looks suspiciously like this (array_search behaviour). Why not use in_array() instead? Also, why not use $highlight[] = 'name'; instead of the array_push syntax? Off-topic, so I'll leave it there. |
|
Back to top |
|
commando Smarty Rookie
Joined: 10 Aug 2004 Posts: 7
|
Posted: Tue Aug 10, 2004 10:31 pm Post subject: |
|
|
boots wrote: | IIR, there is a discussion currently on the php-internals ML atm concerning something that looks suspiciously like this (array_search behaviour). Why not use in_array() instead? Also, why not use $highlight[] = 'name'; instead of the array_push syntax? Off-topic, so I'll leave it there. |
Well now that I know about them I will! I'm just not used to the syntax and functions of php yet, those will do nicely, thanks |
|
Back to top |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|