Direct Access to an Array Item?
Direct Access to an Array Item?
am 09.08.2006 04:34:55 von Peter Beckman
I want to access a variable within a function-returned array without
setting the array to a variable first. Example -- test for equal to string
'foo' on the 4th element of a returned fetch row:
if (($row = mysql_fetch_row($result))[3] == 'foo') {
$user = $row;
}
or
$bar = explode('#', $str)[2];
I know I can do this in perl, but can it be done in PHP? Obviously this is
pseudo code, it doesn't actually work, but I wonder if there is a way that
escapes me currently?
I know I can assign the result to a variable and then test the element; I
am addicted to trying to cut down the amount and complexity of code. Even
if you disagree with my goal as good computing practices, I simply want to
know if what I ask is possible, and if so, how. :-)
Beckman
------------------------------------------------------------ ---------------
Peter Beckman Internet Guy
beckman@purplecow.com http://www.purplecow.com/
------------------------------------------------------------ ---------------
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
RE: Direct Access to an Array Item?
am 09.08.2006 17:14:06 von Bastien Koert
use mysql_result
mysql_result ( $result, 0, 2 )
where 2 is the offset of the fields in the row
Bastien
>From: Peter Beckman
>To: PHP-DB Mailing List
>Subject: [PHP-DB] Direct Access to an Array Item?
>Date: Tue, 8 Aug 2006 22:34:55 -0400 (EDT)
>
>I want to access a variable within a function-returned array without
>setting the array to a variable first. Example -- test for equal to string
>'foo' on the 4th element of a returned fetch row:
>
> if (($row = mysql_fetch_row($result))[3] == 'foo') {
> $user = $row;
> }
>
>or
>
> $bar = explode('#', $str)[2];
>
>I know I can do this in perl, but can it be done in PHP? Obviously this is
>pseudo code, it doesn't actually work, but I wonder if there is a way that
>escapes me currently?
>
>I know I can assign the result to a variable and then test the element; I
>am addicted to trying to cut down the amount and complexity of code. Even
>if you disagree with my goal as good computing practices, I simply want to
>know if what I ask is possible, and if so, how. :-)
>
>Beckman
>----------------------------------------------------------- ----------------
>Peter Beckman Internet Guy
>beckman@purplecow.com http://www.purplecow.com/
>----------------------------------------------------------- ----------------
>
>--
>PHP Database Mailing List (http://www.php.net/)
>To unsubscribe, visit: http://www.php.net/unsub.php
>
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
RE: Direct Access to an Array Item?
am 09.08.2006 21:54:08 von Peter Beckman
On Wed, 9 Aug 2006, Bastien Koert wrote:
> use mysql_result
>
> mysql_result ( $result, 0, 2 )
>
> where 2 is the offset of the fields in the row
In one line of code, I want to fetch the row into a variable, and test a
variable within that row.
I want to know how to do this both within a DB query and outside of.
Your solution is valid, but not what I'm looking for.
------------------------------------------------------------ ---------------
Peter Beckman Internet Guy
beckman@purplecow.com http://www.purplecow.com/
------------------------------------------------------------ ---------------
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Direct Access to an Array Item?
am 10.08.2006 01:14:01 von J R
------=_Part_40943_32241200.1155165241624
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
On 8/9/06, Peter Beckman wrote:
>
> I want to access a variable within a function-returned array without
> setting the array to a variable first. Example -- test for equal to
> string
> 'foo' on the 4th element of a returned fetch row:
>
> if (($row = mysql_fetch_row($result))[3] == 'foo') {
> $user = $row;
> }
>
> or
>
> $bar = explode('#', $str)[2];
i don't think it is possible to do such syntax. nor theres a work around on
to make it way you want it. (i'm not an expert. just IMHO)
I know I can do this in perl, but can it be done in PHP? Obviously this is
> pseudo code, it doesn't actually work, but I wonder if there is a way that
> escapes me currently?
>
> I know I can assign the result to a variable and then test the element; I
> am addicted to trying to cut down the amount and complexity of code. Even
> if you disagree with my goal as good computing practices,
i agree, its not a good programming practice and its makes your code less
readable. :)
I simply want to
> know if what I ask is possible, and if so, how. :-)
>
> Beckman
>
> ------------------------------------------------------------ ---------------
> Peter Beckman Internet
> Guy
> beckman@purplecow.com
> http://www.purplecow.com/
>
> ------------------------------------------------------------ ---------------
>
> --
> PHP Database Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
--
GMail Rocks!!!
------=_Part_40943_32241200.1155165241624--
Direct Access to an Array Item?
am 10.08.2006 01:24:27 von J R
------=_Part_41095_26057082.1155165867647
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
On 8/9/06, Peter Beckman wrote:
>
> I want to access a variable within a function-returned array without
> setting the array to a variable first. Example -- test for equal to
> string
> 'foo' on the 4th element of a returned fetch row:
>
> if (($row = mysql_fetch_row($result))[3] == 'foo') {
> $user = $row;
> }
>
> or
>
> $bar = explode('#', $str)[2];
i don't think it is possible to do such syntax. nor theres a work around on
to make it way you want it. (i'm not an expert. just IMHO)
I know I can do this in perl, but can it be done in PHP? Obviously this is
> pseudo code, it doesn't actually work, but I wonder if there is a way that
> escapes me currently?
>
> I know I can assign the result to a variable and then test the element; I
> am addicted to trying to cut down the amount and complexity of code. Even
>
> if you disagree with my goal as good computing practices,
i agree, its not a good programming practice and its makes your code less
readable. :)
I simply want to
> know if what I ask is possible, and if so, how. :-)
>
> Beckman
>
> ------------------------------------------------------------ ---------------
> Peter Beckman Internet
> Guy
> beckman@purplecow.com
> http://www.purplecow.com/
> ------------------------------------------------------------ ---------------
>
>
> --
> PHP Database Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
--
GMail Rocks!!!
--
GMail Rocks!!!
------=_Part_41095_26057082.1155165867647--
Re: Direct Access to an Array Item?
am 10.08.2006 01:33:15 von Stut
Peter Beckman wrote:
> if (($row = mysql_fetch_row($result))[3] == 'foo') {
> $user = $row;
> }
>
> or
>
> $bar = explode('#', $str)[2];
PHP does not currently support this type of syntax in any form. You must
assign the return value of a function to a variable if you want to
access it as an array.
-Stut
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Direct Access to an Array Item?
am 10.08.2006 02:33:28 von Bastien Koert
Closest you might get is
if ( mysql_result($result,0,3 ) == 'foo') {
$user = $mysql_fetch_row($result);
}
bastien
>From: Stut
>To: Peter Beckman
>CC: PHP-DB Mailing List
>Subject: Re: [PHP-DB] Direct Access to an Array Item?
>Date: Thu, 10 Aug 2006 00:33:15 +0100
>
>Peter Beckman wrote:
> > if (($row = mysql_fetch_row($result))[3] == 'foo') {
> > $user = $row;
> > }
> >
> > or
> >
> > $bar = explode('#', $str)[2];
>
>PHP does not currently support this type of syntax in any form. You must
>assign the return value of a function to a variable if you want to
>access it as an array.
>
>-Stut
>
>--
>PHP Database Mailing List (http://www.php.net/)
>To unsubscribe, visit: http://www.php.net/unsub.php
>
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
RE: Direct Access to an Array Item?
am 03.09.2006 22:07:14 von Naintara
Perhaps you could elaborate on the exact motive
Say, why would you be checking against the 4th element of an array.
I could understand why you would want to check every element of an array
against a particular value, but why just the 4th element, just for interest.
In response to your question, there wasn't a way to access the index of an
array using the square bracket, but you could explore the various array
functions available in php. If you have a dynamic variable/counter, you
could instead use each() or next()
For instance,
You can get this to work
$str = "123#456#789";
"123" == current(explode('#', $str))
The result will be true.
Not sure if this helps you, but worth a shot.
-----Original Message-----
From: J R [mailto:blue.jr@gmail.com]
Sent: 10 August 2006 4:54 AM
To: php-db@lists.php.net
Subject: [PHP-DB] Direct Access to an Array Item?
On 8/9/06, Peter Beckman wrote:
>
> I want to access a variable within a function-returned array without
> setting the array to a variable first. Example -- test for equal to
> string 'foo' on the 4th element of a returned fetch row:
>
> if (($row = mysql_fetch_row($result))[3] == 'foo') {
> $user = $row;
> }
>
> or
>
> $bar = explode('#', $str)[2];
i don't think it is possible to do such syntax. nor theres a work around on
to make it way you want it. (i'm not an expert. just IMHO)
I know I can do this in perl, but can it be done in PHP? Obviously this is
> pseudo code, it doesn't actually work, but I wonder if there is a way
> that escapes me currently?
>
> I know I can assign the result to a variable and then test the
> element; I am addicted to trying to cut down the amount and complexity
> of code. Even
>
> if you disagree with my goal as good computing practices,
i agree, its not a good programming practice and its makes your code less
readable. :)
I simply want to
> know if what I ask is possible, and if so, how. :-)
>
> Beckman
>
>
------------------------------------------------------------ ---------------
> Peter Beckman Internet
> Guy
> beckman@purplecow.com
> http://www.purplecow.com/
> ------------------------------------------------------------ ----------
> -----
>
>
> --
> PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit:
> http://www.php.net/unsub.php
>
>
--
GMail Rocks!!!
--
GMail Rocks!!!
--
No virus found in this incoming message.
Checked by AVG Free Edition.
Version: 7.1.405 / Virus Database: 268.10.8/413 - Release Date: 08/08/2006
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php