?
Solved

multi-dimensional arrays and non multi-dimensional arrays

Posted on 2005-05-06
6
Medium Priority
?
175 Views
Last Modified: 2006-11-18
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
0
Comment
Question by:Timothy Golden
6 Comments
 
LVL 10

Expert Comment

by:ThaSmartUno
ID: 13947355
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
 
LVL 6

Accepted Solution

by:
German_Rumm earned 60 total points
ID: 13951089
Hi luckynh,

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

?>

---
German Rumm.
0
 
LVL 11

Expert Comment

by:ZhaawZ
ID: 13953560
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 6

Expert Comment

by:German_Rumm
ID: 13953584
ZhaawZ,

Nope, is_array() will not give desired results in that case. You should read the question.
0
 
LVL 3

Author Comment

by:Timothy Golden
ID: 13958997
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
 
LVL 3

Author Comment

by:Timothy Golden
ID: 13978495
ok so the both worked..
but the one i'm useing is German_Rumm's becuse it's less lines of code.

0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month17 days, 2 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question