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
LVL 3
Timothy GoldenWeb DevAsked:
Who is Participating?
 
German_RummConnect With a Mentor Commented:
Hi luckynh,

Even simpler way:
<?php
       function lookUpPartNumbers($partArray){
            if (count($partArray) == 1) {
                $partArray = array($partArray);
            }
        // ... rest of your code.    

?>

---
German Rumm.
0
 
ThaSmartUnoCommented:
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-%'";
                }
             }
          }
0
 
ZhaawZSoftware DeveloperCommented:
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.
}
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
German_RummCommented:
ZhaawZ,

Nope, is_array() will not give desired results in that case. You should read the question.
0
 
Timothy GoldenWeb DevAuthor Commented:
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....
0
 
Timothy GoldenWeb DevAuthor Commented:
ok so the both worked..
but the one i'm useing is German_Rumm's becuse it's less lines of code.

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.