SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2002
    Location
    Santa Fe, NM
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Walking a Multidimensional Array

    Hi Folks,

    I have a form that loads its values into a multidimensional array and then inserts into a mySQL database and sends an email copy of the input to the database manager.

    I'm trying to figure out the most efficient way to walk the following array when creating the email.

    Code:
    Array
    (
        [title] => My Paper Title
        [first] => Array
            (
                [0] => First0
                [1] => First1
                [2] => First2
                [3] => First3
                [4] => First4
            )
    
        [middle] => Array
            (
                [0] => Middle0
                [1] => Middle1
                [2] => Middle2
                [3] => Middle3
                [4] => Middle4
            )
    
        [last] => Array
            (
                [0] => Last0
                [1] => Last1
                [2] => Last2
                [3] => Last3
                [4] => Last4
            )
    
        [keywords] => apple, orange, pear
        [journal] => The Best Journal
        [date_submitted] => 1047420492
        [uploadfile] => paper.pdf
    )
    I wrote the following bit of code which works but my suspicion is there's a better way to do this. For example, if the array was deeper I'd have to add additional foreach loops to traverse deeper into the array.

    PHP Code:
    <?php
    foreach ($pubform as $key => $value) {
        if (
    is_array ($value)) {
            foreach (
    $value as $k => $v) {
                if (
    $v != '') {
                    
    $my_mailbody .= $k ': ' $v "<br>\r\n";
                }
            }
        } elseif (
    $value != '') {
            
    $my_mailbody .= $key ': ' $value "<br>\r\n";
        }
    }
    ?>
    Thanks,

    Kevin
    Last edited by kevinpd; Mar 12, 2003 at 14:19.

  2. #2
    ********* wombat firepages's Avatar
    Join Date
    Jul 2000
    Location
    Perth Australia
    Posts
    1,717
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This should go as deep as you want - but no promises , but it seems to work, though really it would be nice to add some tabs for nicer formatting ?

    PHP Code:
    <?
    function got_one($arr,&$str){
        foreach(
    $arr as $k => $v){
            if(
    is_array($v)){
                
    $str .=$k::<br />\n";
                
    got_one($v,$str);
            }else{
                
    $str .=$k:$v<br />\n";
            }
        }
    }

    foreach (
    $pubform as $key => $var) { 
        if (
    is_array ($var)) { 
            
    $str .="$key ::<br />\n";
            
    got_one($var,$str);
        }else{
            
    $str .="$key : $var<br />\n";
        }


    echo 
    $str;
    ?>
    <edit>
    lol - I am a dill - you can just do this..
    PHP Code:
    <?
    function got_one($arr,&$str){
        foreach(
    $arr as $k => $v){
            if(
    is_array($v)){
                
    $str .=$k::<br />\n";
                
    got_one($v,$str);
            }else{
                
    $str .=$k:$v<br />\n";
            }
        }
    }

    got_one($pubform,$str);
    echo 
    $str;
    ?>

    </edit>
    Last edited by firepages; Mar 12, 2003 at 12:08.

  3. #3
    SitePoint Enthusiast
    Join Date
    Oct 2002
    Location
    Santa Fe, NM
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by firepages
    PHP Code:
    <?
    function got_one($arr,&$str){
        foreach(
    $arr as $k => $v){
            if(
    is_array($v)){
                
    $str .=$k::<br />\n";
                
    got_one($v,$str);
            }else{
                
    $str .=$k:$v<br />\n";
            }
        }
    }

    got_one($pubform,$str);
    echo 
    $str;
    ?>
    Nice. That works well. A couple of new but not unknown concepts for me: passing variables by reference and recursion. Does passing $str by reference just allow you to avoid having to return($str) or is there some other functionality that I'm missing?

    I just added an if loop to check that $v is not empty before it's added to $str. I guess one thought would be to drop the empty values from the array before processing it but I don't know that it's worth the effort.

    Thanks,

    Kevin

  4. #4
    ********* wombat firepages's Avatar
    Join Date
    Jul 2000
    Location
    Perth Australia
    Posts
    1,717
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ". Does passing $str by reference just allow you to avoid having to return($str) or is there some other functionality that I'm missing? "

    just less clutter there may be a millsecond or so of speed gain as well I assume?(read guess)

  5. #5
    chown linux:users\ /world Hartmann's Avatar
    Join Date
    Aug 2000
    Location
    Houston, TX, USA
    Posts
    6,455
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    Yeah, I wouldn't guess that you would see much speed increase with that little bit. I just think it makes the code easier to read.

  6. #6
    SitePoint Enthusiast
    Join Date
    Oct 2002
    Location
    Santa Fe, NM
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm now trying to do some very basic error checking on my multidimensional array of form data. My original thought was to create an array of required fields and use it in a function to check wether or not those fields contained data. Like this:

    PHP Code:
    <?php
    function validate ($validatearray$required_fields) {
      foreach (
    $required_fields as $name) {
              echo 
    $name;
            if (
    $validatearray[$name] == '') {
              
    $error_array[] = $name;
                }
      }
      return (
    $error_array);
    }
     
    // can I add first[0] to this? I don't think so...
    $required_fields = array ('submittor_e-mail_address','abstract');

    $myarray =
    Array
    (
        [
    pubform] => Array
            (
                [
    submittor_e-mail_address] => test
                
    [first] => Array
                    (
                        [
    0] => test
                     
    )

                [
    middle] => Array
                    (
                        [
    0] => test
                     
    )

                [
    last] => Array
                    (
                        [
    0] => test
                     
    )

                [abstract] =>
            )

    );

    $errors validate ($required_fields$myarray);
    ?>
    It all works fine with the first level of the array but I'm not sure how to handle passing the function one of the name fields (first[0], middle[0],etc). Do I need to add recursion to this function? Should I rethink the way I'm doing this?

    Any help or nudges are welcome,

    Kevin
    Last edited by kevinpd; Mar 18, 2003 at 19:29.

  7. #7
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes, add recursion. try this function:

    PHP Code:
    <?php

    function validate($validatearray$required_fields) {
        
    $error_array = array();

        foreach (
    $required_fields as $name) {
            echo 
    $name;

            if (
    is_array($validatearray[$name])) {
                
    $error_array array_merge($error_arrayvalidate($validatearray[$name], $required_fields));
            } elseif (
    $validatearray[$name] == '') {
                
    $error_array[] = $name;
            }
        }

        return (
    $error_array);
    }

    ?>
    hope that works. and i'm not sure it'll do exactly what you want if it does, but hopefully it's a start.
    - Matt ** Ignore old signature for now... **
    Dr.BB - Highly optimized to be 2-3x faster than the "Big 3."
    "Do not enclose numeric values in quotes -- that is very non-standard and will only work on MySQL." - MattR

  8. #8
    SitePoint Enthusiast
    Join Date
    Oct 2002
    Location
    Santa Fe, NM
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by DR_LaRRY_PEpPeR
    PHP Code:
    <?php

    function validate($validatearray$required_fields) {
        
    $error_array = array();

        foreach (
    $required_fields as $name) {
            echo 
    $name;

            if (
    is_array($validatearray[$name])) {
                
    $error_array array_merge($error_arrayvalidate($validatearray[$name], $required_fields));
            } elseif (
    $validatearray[$name] == '') {
                
    $error_array[] = $name;
            }
        }

        return (
    $error_array);
    }

    ?>
    This appears to be working but it's giving me the following error:

    Warning: Illegal offset type in /path/to/my/functionfile.php on line 41

    I'm guessing my syntax is wrong when I build my $required_fields array.

    PHP Code:
    <?php
    $required_fields 
    = array (
    'working_paper_title',
    'person_submitting_first_name',
    'person_submitting_middle_name',
    'person_submitting_last_name',
    'submittor_e-mail_address',
    'keywords',
    array (
    'first' => 0),
    array (
    'middle' => 0),
    array (
    'last' => 0)
    );
    ?>
    <edit>
    Actually, It's not working now that I'm looking a little more closely. Perhaps it has to do with my syntax for the first, middle, and last fields in the $required_fields array. It's appears to be adding a value for these fields to $error_array even when I pass a value in that particular form field.
    </edit>

    Kevin
    Last edited by kevinpd; Mar 18, 2003 at 19:43.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •