Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.

tricky little array question

 
Post new topic   Reply to topic    Smarty Forum Index -> Smarty Development
View previous topic :: View next topic  
Author Message
commando
Smarty Rookie


Joined: 10 Aug 2004
Posts: 7

PostPosted: Tue Aug 10, 2004 9:05 pm    Post subject: tricky little array question Reply with quote

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
View user's profile Send private message Visit poster's website
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Tue Aug 10, 2004 9:49 pm    Post subject: Reply with quote

You would have better luck asking this question in a PHP forum or mailing list.
Back to top
View user's profile Send private message Visit poster's website
commando
Smarty Rookie


Joined: 10 Aug 2004
Posts: 7

PostPosted: Tue Aug 10, 2004 9:53 pm    Post subject: Reply with quote

Do you know of any helpful forum sites for newbies to php?
Back to top
View user's profile Send private message Visit poster's website
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Tue Aug 10, 2004 9:55 pm    Post subject: Reply with quote

The mailing lists are usually a good place to search/discuss.

http://www.php.net/mailing-lists.php
Back to top
View user's profile Send private message Visit poster's website
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Tue Aug 10, 2004 10:04 pm    Post subject: Reply with quote

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
View user's profile Send private message
commando
Smarty Rookie


Joined: 10 Aug 2004
Posts: 7

PostPosted: Tue Aug 10, 2004 10:31 pm    Post subject: Reply with quote

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 Smile
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> Smarty Development All times are GMT
Page 1 of 1

 
Jump to:  
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
Protected by Anti-Spam ACP