Ask a Question related to PHP Development, Design and Development.
-
Bob pilly #1
newbie array question
Hi All
Im new to php and are getting a bit confused about the sybase_fetch_array function (i think that this is the same as mysql_fetch_array?).
If i have a valid sql query that returns three the records 'john','jack' and 'mary' and i want to put that into an array where array[0]->john,array[1]->jack etc how do i do this? Im trying to use sybase_fetch_array as follows:
$query = "select name from names (where age <'23'')";
$numero= sybase_connect("database" , "user" , "password" )
or die ("Unable to connect to database server");
$result=sybase_query($query,$numero );
$names=sybase_fetch_array($result);
I then try and do something like print $names[0];
but it doesnt work. Sorry if this is really basic any help would be greatly appreciated!
---------------------------------
Want to chat instantly with your online friends?*Get the FREE Yahoo!Messenger
Bob pilly Guest
-
Byte array to string and back - newbie question
Hi, I am trying to implement DES algorithm as described in the Microsoft article at... -
[newbie]saving and reading array of associative array
i'm looking for examples of saving to file and reading back an array of associative array, in a ruby like way. saying i have something like : ... -
newbie question. how to assign an array to a hash table?
Hi, I have something like: $value = 'A' 'C' 'G'; and I would like store this value on a hash table. my @value = split(' ',$value); print... -
Last number in the array --- access problem --Newbie question
Here is the complete problem: I have two files MAER_FILE and EVAL_FILE MAER_FILE contains ITR Max_error Avg_error Min_error... -
array access problem - newbie question
#-------------code----------# open (MAER_FILE, "MAER_output.txt"); while (<MAER_FILE>) { chomp; my @maer_array = split(/\t/); print... -
Harry Wiens #2
Re: newbie array question
try this:
$query = "select name from names (where age <'23'')";
$numero= sybase_connect("database" , "user" , "password" ) or die ("Unable
to connect to database server");
$result=sybase_query($query,$numero );
while($temp=sybase_fetch_array($result)){
$names[] = $temp[name];
}
mfg.
harry wiens
""Bob pilly"" <bobpilly@yahoo.co.uk> schrieb im Newsbeitrag
news:20030710142939.70314.qmail@web40505.mail.yaho o.com...function (i think that this is the same as mysql_fetch_array?).> Hi All
>
> Im new to php and are getting a bit confused about the sybase_fetch_arrayand 'mary' and i want to put that into an array where>
> If i have a valid sql query that returns three the records 'john','jack'
array[0]->john,array[1]->jack etc how do i do this? Im trying to use
sybase_fetch_array as follows:greatly appreciated!>
> $query = "select name from names (where age <'23'')";
> $numero= sybase_connect("database" , "user" , "password" )
> or die ("Unable to connect to database server");
> $result=sybase_query($query,$numero );
> $names=sybase_fetch_array($result);
>
>
> I then try and do something like print $names[0];
>
> but it doesnt work. Sorry if this is really basic any help would beYahoo!Messenger>
>
>
> ---------------------------------
> Want to chat instantly with your online friends? Get the FREE
Harry Wiens Guest
-
Martien van Wanrooij #3
newbie array question
After some looking around - and maybe entering just the wrong keywords in
Google :) - a short question: is it possible to retrieve the index number
of the element of an array, let say that there is an array $myArray =
("John", "Tom", "Dick","Harry","Peter")
I am looking for a function that tells me that Harry is the fourth element.
(I have tried in google things like "retrieve index array php" but without
result)
Thx on beforehand
Martien van Wanrooij.
Martien van Wanrooij Guest
-
Justin Koivisto #4
Re: newbie array question
Martien van Wanrooij wrote:
Here:> After some looking around - and maybe entering just the wrong keywords in
> Google :) - a short question: is it possible to retrieve the index number
> of the element of an array, let say that there is an array $myArray =
> ("John", "Tom", "Dick","Harry","Peter")
> I am looking for a function that tells me that Harry is the fourth element.
> (I have tried in google things like "retrieve index array php" but without
> result)
<?php
$myArray = array("John", "Tom", "Dick","Harry","Peter");
print_r($myArray);
var_dump(whereInArray('Harry',$myArray,$error));
echo $error;
function whereInArray($needle,$haystack, &$err){
// first be sure we have an array
if(!is_array($haystack)){
$err='The haystack was not an array.';
return FALSE;
}
for($i=0;$i<count($haystack);$i++)
if($haystack[$i] == $needle)
return $i;
$err='Needle '.$needle.' not found in haystack.';
return FALSE;
}
?>
--
Justin Koivisto - [email]spam@koivi.com[/email]
PHP POSTERS: Please use comp.lang.php for PHP related questions,
alt.php* groups are not recommended.
Justin Koivisto Guest
-
Disco Plumber #5
Re: newbie array question
Justin Koivisto, obviously a huge fan of Elton John, wrote:
Alternately:>
> for($i=0;$i<count($haystack);$i++)
> if($haystack[$i] == $needle)
> return $i;
foreach($haystack as $key => $value)
if($value==$needle)
return $key;
This also works for non-numeric keyed arrays.
/joe
--
The router is retarded and bastardized. In Dramatech, the Bursar's Office's
emo kid is cute. The disassembler is exhausting. Jess's 2x-error-correcting
variable from nick black's apartment is faulty.
Disco Plumber Guest
-
Tom Thackrey #6
Re: newbie array question
On 10-Nov-2003, "Martien van Wanrooij" <info@martienvanwanrooij.nl> wrote:
$myFlippedArray = array_flip($myArray);> After some looking around - and maybe entering just the wrong keywords in
> Google :) - a short question: is it possible to retrieve the index number
> of the element of an array, let say that there is an array $myArray =
> ("John", "Tom", "Dick","Harry","Peter")
> I am looking for a function that tells me that Harry is the fourth
> element.
> (I have tried in google things like "retrieve index array php" but
> without
> result)
> Thx on beforehand
echo "Harry is element $myFlippedArray[Harry] of myArray";
--
Tom Thackrey
[url]www.creative-light.com[/url]
tom (at) creative (dash) light (dot) com
do NOT send email to [email]jamesbutler@willglen.net[/email] (it's reserved for spammers)
Tom Thackrey Guest
-
Zurab Davitiani #7
Re: newbie array question
Martien van Wanrooij wrote on Monday 10 November 2003 12:59:
How about:> After some looking around - and maybe entering just the wrong keywords in
> Google :) - a short question: is it possible to retrieve the index number
> of the element of an array, let say that there is an array $myArray =
> ("John", "Tom", "Dick","Harry","Peter")
> I am looking for a function that tells me that Harry is the fourth
> element.
> (I have tried in google things like "retrieve index array php" but
> without result)
[url]http://www.php.net/manual/en/function.array-search.php[/url]
--
Business Web Solutions
ActiveLink, LLC
[url]www.active-link.com/intranet/[/url]
Zurab Davitiani Guest
-
Justin Koivisto #8
Re: newbie array question
Disco Plumber wrote:
Ya, surprized that I didn't catch then when I copied it out of the> Justin Koivisto, obviously a huge fan of Elton John, wrote:
>>>> for($i=0;$i<count($haystack);$i++)
>> if($haystack[$i] == $needle)
>> return $i;
>
> Alternately:
>
> foreach($haystack as $key => $value)
> if($value==$needle)
> return $key;
>
> This also works for non-numeric keyed arrays.
file... ;)
--
Justin Koivisto - [email]spam@koivi.com[/email]
PHP POSTERS: Please use comp.lang.php for PHP related questions,
alt.php* groups are not recommended.
Justin Koivisto Guest
-
Justin Koivisto #9
Re: newbie array question
Justin Koivisto wrote:
Note, it is possible that a value of int(0) is returned. On errors,> Martien van Wanrooij wrote:
>>>> After some looking around - and maybe entering just the wrong keywords in
>> Google :) - a short question: is it possible to retrieve the index
>> number
>> of the element of an array, let say that there is an array $myArray =
>> ("John", "Tom", "Dick","Harry","Peter")
>> I am looking for a function that tells me that Harry is the fourth
>> element.
>> (I have tried in google things like "retrieve index array php" but
>> without
>> result)
>
> Here:
> <?php
> $myArray = array("John", "Tom", "Dick","Harry","Peter");
> print_r($myArray);
> var_dump(whereInArray('Harry',$myArray,$error));
> echo $error;
>
> function whereInArray($needle,$haystack, &$err){
> // first be sure we have an array
> if(!is_array($haystack)){
> $err='The haystack was not an array.';
> return FALSE;
> }
>
> for($i=0;$i<count($haystack);$i++)
> if($haystack[$i] == $needle)
> return $i;
>
> $err='Needle '.$needle.' not found in haystack.';
> return FALSE;
> }
> ?>
boolean(FALSE) is returned. A simple if(whereInArray($find,$ar)) will
not work...
You will either need to check via:
$error='';
if(whereInArray($find,$ar,$error) && empty($error))
or
if(whereInArray($find,$ar,$error)!==FALSE)
(I forgot to metioon that in the last message as well.)
--
Justin Koivisto - [email]spam@koivi.com[/email]
PHP POSTERS: Please use comp.lang.php for PHP related questions,
alt.php* groups are not recommended.
Justin Koivisto Guest
-
Martien van Wanrooij #10
Re: newbie array question
"Zurab Davitiani" <agt@mindless.com> schreef in bericht
news:TvTrb.5411$Qi3.3366@newssvr29.news.prodigy.co m...
Thank you Zurab,> How about:
> [url]http://www.php.net/manual/en/function.array-search.php[/url]
I have been actually looking at that page but I couldn't deduce from the
code this particular problem of the "how-manieth" place a certain element
took in the array.
Martien.
Martien van Wanrooij Guest
-
Martien van Wanrooij #11
Re: newbie array question
"Justin Koivisto" <spam@koivi.com> schreef in bericht
news:UdTrb.770$Uz.22362@news7.onvoy.net...I thought there would be some standard functie to solve my problem but> Here:
> <?php
> $myArray = array("John", "Tom", "Dick","Harry","Peter");
> print_r($myArray);
> var_dump(whereInArray('Harry',$myArray,$error));
> echo $error;
>
> function whereInArray($needle,$haystack, &$err){
> // first be sure we have an array
> if(!is_array($haystack)){
> $err='The haystack was not an array.';
> return FALSE;
> }
>
> for($i=0;$i<count($haystack);$i++)
> if($haystack[$i] == $needle)
> return $i;
>
> $err='Needle '.$needle.' not found in haystack.';
> return FALSE;
> }
> ?>
obviously there isn't . Thanks for your help
Martien.
Martien van Wanrooij Guest
-
Zurab Davitiani #12
Re: newbie array question
Martien van Wanrooij wrote on Tuesday 11 November 2003 14:12:
It returns the array key. If you take your original example:>>> How about:
>> [url]http://www.php.net/manual/en/function.array-search.php[/url]
> Thank you Zurab,
>
> I have been actually looking at that page but I couldn't deduce from the
> code this particular problem of the "how-manieth" place a certain element
> took in the array.
$myArray = ("John", "Tom", "Dick","Harry","Peter")
$key = array_search("Harry", $myArray);
The key should also be the position of the element in the array. This
obviously doesn't take into account if you unset element(s) from the middle
of the array, or if you have an associative array keyed on anything else
than the position.
If your real scenario includes other factors such as above, then do
something like:
$key = array_search(array_search("Harry", $myArray), array_keys($myArray));
Untested, but something like that. :)
--
Business Web Solutions
ActiveLink, LLC
[url]www.active-link.com/intranet/[/url]
Zurab Davitiani Guest



Reply With Quote

