Link to home
Start Free TrialLog in
Avatar of Timothy Golden
Timothy GoldenFlag for United States of America

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($partArray){
            $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
Avatar of ThaSmartUno
ThaSmartUno

im a bit rusty in php ... but i think this would work

          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-%'";
                }
             }
          }
ASKER CERTIFIED SOLUTION
Avatar of German_Rumm
German_Rumm

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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($partArray){
  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.
Avatar of Timothy Golden

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....
ok so the both worked..
but the one i'm useing is German_Rumm's becuse it's less lines of code.