Function parameters and arrays
Function parameters and arrays
am 18.09.2008 17:29:53 von Frank Stanovcak
I am trying to pass a multi dimmed array as a variable parameter to a custom
function to build a query for me. Here is the basic code and what I am
getting.
$WhereList[0][0] = 'OESalesOrder.OrderNo';
$WhereList[0][1] = '=';
$WhereList[0][2] = '2272';
$SQLString = SQLCall('OESalesOrder',$FieldList,$WhereList);
I then use a foreach in the function to process it.
$i = 0;
foreach(func_get_arg(2) as $WhereArray) {
echo $WhereArray[0][0];
if($i == 0) {
$SQLStmt .= ' ' . $WhereArray[$i][0] . ' ' . $WhereArray[$i][1] . ' ' .
$WhereArray[$i][2];
$i += 1;
} else {
$SQLStmt .= ' ' . $WhereArray[$i][0] . ' ' . $WhereArray[$i][1] . ' ' .
$WhereArray[$i][2] . ' ' . $WhereArray[$i][3];
$i += 1;
};
};
What I get when it processes is the first three letters of [0][0]
[0][0] = O
[0][1] = E
[0][2] = S
Did I do something wrong, or is this not possible? I have done an array
processing this way before, but not multidimmed.
Frank.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Function parameters and arrays
am 18.09.2008 17:33:48 von Stut
On 18 Sep 2008, at 16:29, Frank Stanovcak wrote:
> I am trying to pass a multi dimmed array as a variable parameter to
> a custom
> function to build a query for me. Here is the basic code and what
> I am
> getting.
>
> $WhereList[0][0] = 'OESalesOrder.OrderNo';
> $WhereList[0][1] = '=';
> $WhereList[0][2] = '2272';
> $SQLString = SQLCall('OESalesOrder',$FieldList,$WhereList);
>
> I then use a foreach in the function to process it.
>
> $i = 0;
> foreach(func_get_arg(2) as $WhereArray) {
This foreach will give you each array at the first level into
$WhereArray...
> echo $WhereArray[0][0];
So there is no need for the first [0] here.
> if($i == 0) {
> $SQLStmt .= ' ' . $WhereArray[$i][0] . ' ' . $WhereArray[$i]
> [1] . ' ' .
> $WhereArray[$i][2];
> $i += 1;
> } else {
> $SQLStmt .= ' ' . $WhereArray[$i][0] . ' ' . $WhereArray[$i]
> [1] . ' ' .
> $WhereArray[$i][2] . ' ' . $WhereArray[$i][3];
> $i += 1;
> };
> };
And $i is not needed at all.
> What I get when it processes is the first three letters of [0][0]
> [0][0] = O
> [0][1] = E
> [0][2] = S
>
> Did I do something wrong, or is this not possible? I have done an
> array
> processing this way before, but not multidimmed.
Suggest you read this: http://php.net/foreach
-Stut
--
http://stut.net/
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Function parameters and arrays
am 18.09.2008 18:06:22 von List Manager
Frank Stanovcak wrote:
> I am trying to pass a multi dimmed array as a variable parameter to a custom
> function to build a query for me. Here is the basic code and what I am
> getting.
>
> $WhereList[0][0] = 'OESalesOrder.OrderNo';
> $WhereList[0][1] = '=';
> $WhereList[0][2] = '2272';
> $SQLString = SQLCall('OESalesOrder',$FieldList,$WhereList);
>
> I then use a foreach in the function to process it.
>
> $i = 0;
> foreach(func_get_arg(2) as $WhereArray) {
> echo $WhereArray[0][0];
> if($i == 0) {
> $SQLStmt .= ' ' . $WhereArray[$i][0] . ' ' . $WhereArray[$i][1] . ' ' .
> $WhereArray[$i][2];
> $i += 1;
> } else {
> $SQLStmt .= ' ' . $WhereArray[$i][0] . ' ' . $WhereArray[$i][1] . ' ' .
> $WhereArray[$i][2] . ' ' . $WhereArray[$i][3];
> $i += 1;
> };
> };
>
> What I get when it processes is the first three letters of [0][0]
> [0][0] = O
> [0][1] = E
> [0][2] = S
>
> Did I do something wrong, or is this not possible? I have done an array
> processing this way before, but not multidimmed.
>
> Frank.
>
>
>
Something like this should do.
function SQLCall($table, $FieldList, $WhereList=array()) {
# work with your field list...
$where_parts = array();
foreach($WhereList AS $cond) {
$where_parts[] = $cond[0].' '.$cond[1].' '.$cond[2];
}
$WHERE = '';
if ( count($where_parts) ) {
$WHERE = 'WHERE ' . join(' AND ', $where_parts);
}
# put it all together.
}
--
Jim Lucas
"Some men are born to greatness, some achieve greatness,
and some have greatness thrust upon them."
Twelfth Night, Act II, Scene V
by William Shakespeare
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Function parameters and arrays
am 18.09.2008 18:25:10 von Frank Stanovcak
Actually Stut pointed out that I was calling both dimmensions when my sorry
butt should only have been referencing the second one.
I had everything over expanded so I could see where the code was breaking.
Thank you everyone for the help! That was fantastic!
Frank
""Frank Stanovcak"" wrote in message
news:15.76.44246.FF272D84@pb1.pair.com...
>I am trying to pass a multi dimmed array as a variable parameter to a
>custom function to build a query for me. Here is the basic code and what I
>am getting.
>
> $WhereList[0][0] = 'OESalesOrder.OrderNo';
> $WhereList[0][1] = '=';
> $WhereList[0][2] = '2272';
> $SQLString = SQLCall('OESalesOrder',$FieldList,$WhereList);
>
> I then use a foreach in the function to process it.
>
> $i = 0;
> foreach(func_get_arg(2) as $WhereArray) {
> echo $WhereArray[0][0];
> if($i == 0) {
> $SQLStmt .= ' ' . $WhereArray[$i][0] . ' ' . $WhereArray[$i][1] . ' ' .
> $WhereArray[$i][2];
> $i += 1;
> } else {
> $SQLStmt .= ' ' . $WhereArray[$i][0] . ' ' . $WhereArray[$i][1] . ' ' .
> $WhereArray[$i][2] . ' ' . $WhereArray[$i][3];
> $i += 1;
> };
> };
>
> What I get when it processes is the first three letters of [0][0]
> [0][0] = O
> [0][1] = E
> [0][2] = S
>
> Did I do something wrong, or is this not possible? I have done an array
> processing this way before, but not multidimmed.
>
> Frank.
>
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
RE: Function parameters and arrays
am 18.09.2008 18:41:44 von Simcha
This would be a little neater:
foreach(func_get_arg(2) as $k => $WhereArray) {
//echo $WhereArray[0][0];
$SQLStmt .= ' ' . $WhereArray[0] . ' ' . $WhereArray[1] . ' ' .
$WhereArray[2];
if($k != 0) $SQLStmt .=' ' . $WhereArray[3];
};
Simcha Younger
-----Original Message-----
From: Frank Stanovcak [mailto:blindspotpro@comcast.net]
Sent: Thursday, September 18, 2008 5:30 PM
To: php-general@lists.php.net
Subject: [PHP] Function parameters and arrays
I am trying to pass a multi dimmed array as a variable parameter to a custom
function to build a query for me. Here is the basic code and what I am
getting.
$WhereList[0][0] = 'OESalesOrder.OrderNo';
$WhereList[0][1] = '=';
$WhereList[0][2] = '2272';
$SQLString = SQLCall('OESalesOrder',$FieldList,$WhereList);
I then use a foreach in the function to process it.
$i = 0;
foreach(func_get_arg(2) as $WhereArray) {
echo $WhereArray[0][0];
if($i == 0) {
$SQLStmt .= ' ' . $WhereArray[$i][0] . ' ' . $WhereArray[$i][1] . ' ' .
$WhereArray[$i][2];
$i += 1;
} else {
$SQLStmt .= ' ' . $WhereArray[$i][0] . ' ' . $WhereArray[$i][1] . ' ' .
$WhereArray[$i][2] . ' ' . $WhereArray[$i][3];
$i += 1;
};
};
What I get when it processes is the first three letters of [0][0]
[0][0] = O
[0][1] = E
[0][2] = S
Did I do something wrong, or is this not possible? I have done an array
processing this way before, but not multidimmed.
Frank.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
No virus found in this incoming message.
Checked by AVG - http://www.avg.com
Version: 8.0.169 / Virus Database: 270.6.21/1675 - Release Date: 17/09/2008
17:07
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php