Timothy Golden
asked on
multi-dimensional arrays and non multi-dimensional arrays
I have a function the builds an SQL statement and Appends to the WHERE clause based on the array passed.
It works ok when i pass a multi-dimensional array but when the array is not multi-dimensional it breaks
Is there a way to test if an array that is pass a multi-dimensional array?
Here is the function
function lookUpPartNumbers($partArr ay){
$sql = "SELECT PRTNUM_01,PMDES1_01, STAENG_01 from ".MAX_PART_MASTER." WHERE ";
$flag = true;
foreach ($partArray as $v1) {
foreach ($v1 as $partNumber) {
if($flag) {
$sql = $sql. " prtnum_01 LIKE '$partNumber-%'";
$flag = false;
} else {
$sql = $sql. " OR prtnum_01 LIKE '$partNumber-%'";
}
}
}
$sql = $sql. " and (STAENG_01 = '2' OR STAENG_01 ='5') GROUP BY PRTNUM_01,PMDES1_01, STAENG_01 ORDER BY PMDES1_01";
echo"$sql"; exit();
*********** works
When $partArray is passed with this array it works ok
Array (
[0] => Array ( [familycode] => 3120 )
[1] => Array ( [familycode] => 3190 )
)
*********** does NOT work
Array (
[familycode] => 3190
)
How can i change the foreach loops to work if the array is multi-dimensional or not
It works ok when i pass a multi-dimensional array but when the array is not multi-dimensional it breaks
Is there a way to test if an array that is pass a multi-dimensional array?
Here is the function
function lookUpPartNumbers($partArr
$sql = "SELECT PRTNUM_01,PMDES1_01, STAENG_01 from ".MAX_PART_MASTER." WHERE ";
$flag = true;
foreach ($partArray as $v1) {
foreach ($v1 as $partNumber) {
if($flag) {
$sql = $sql. " prtnum_01 LIKE '$partNumber-%'";
$flag = false;
} else {
$sql = $sql. " OR prtnum_01 LIKE '$partNumber-%'";
}
}
}
$sql = $sql. " and (STAENG_01 = '2' OR STAENG_01 ='5') GROUP BY PRTNUM_01,PMDES1_01, STAENG_01 ORDER BY PMDES1_01";
echo"$sql"; exit();
*********** works
When $partArray is passed with this array it works ok
Array (
[0] => Array ( [familycode] => 3120 )
[1] => Array ( [familycode] => 3190 )
)
*********** does NOT work
Array (
[familycode] => 3190
)
How can i change the foreach loops to work if the array is multi-dimensional or not
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
German_Rumm, if you're using count() function, then remember, that array also can have one element.
$arr[] = 'blah';
$arr = array($arr);
print_r($arr);
This will be:
Array (
[0] => Array (
[0] => blah
)
)
It's better to use is_array() instead of count() to check if variable is array or smth else.
function lookUpPartNumbers($partArr ay){
if ( is_array($partArray) ) {
$partArray = array($partArray);
}
// ... rest of your code.
}
$arr[] = 'blah';
$arr = array($arr);
print_r($arr);
This will be:
Array (
[0] => Array (
[0] => blah
)
)
It's better to use is_array() instead of count() to check if variable is array or smth else.
function lookUpPartNumbers($partArr
if ( is_array($partArray) ) {
$partArray = array($partArray);
}
// ... rest of your code.
}
ZhaawZ,
Nope, is_array() will not give desired results in that case. You should read the question.
Nope, is_array() will not give desired results in that case. You should read the question.
ASKER
so far what ThaSmartUno has posted seams to be working
However German_Rumm what you posted is a fewl lins less code and might offer less overhead.
I'll give it a try....
However German_Rumm what you posted is a fewl lins less code and might offer less overhead.
I'll give it a try....
ASKER
ok so the both worked..
but the one i'm useing is German_Rumm's becuse it's less lines of code.
but the one i'm useing is German_Rumm's becuse it's less lines of code.
foreach ($partArray as $v1) {
if(is_array($v1)){
foreach ($v1 as $partNumber) {
if($flag) {
$sql = $sql. " prtnum_01 LIKE '$partNumber-%'";
$flag = false;
} else {
$sql = $sql. " OR prtnum_01 LIKE '$partNumber-%'";
}
}
}else {
if($flag) {
$sql = $sql. " prtnum_01 LIKE '$v1-%'";
$flag = false;
} else {
$sql = $sql. " OR prtnum_01 LIKE '$v1-%'";
}
}
}