Can't display column of multi-dimensional array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • news@celticbear.com

    Can't display column of multi-dimensional array

    Using php.net I got this far, and so far so good!
    But I can't fenagle it quite the way I need to.

    I'm creating an array (which I didn't know how to do before today, so a
    success for me *g*):

    $a = array();
    $a[0][0] = "Name";
    $a[0][1] = $name;
    $a[1][0] = "Message";
    $a[1][1] = $msg;
    $a[2][0] = "Subject";
    $a[2][2] = $subject;

    Then I'm feeding it through a pre-defined function (also which I'm
    learning how to do, so in general I feel pretty good *g*):

    foreach ($a as $v1) {
    $fieldname = $v1;
    foreach ($v1 as $v2) {
    $returned = valid_field($fi eldname,$v2);
    if ($returned != "true") {
    $error .= $returned;
    $errflag = "1";
    }
    }
    }

    That's the last way I tried. I also tried without the "$fieldname =
    $v1;" and using eith $a and $v1 in the first of the two valid_field()
    variables.

    In all cases, what gets echoed back are: "array" and the correct value
    of the second column:

    "array", $name
    "array", $msg
    "array", $subject

    If someone could point me to a clue, don't give me the answer, just a
    push in the right direction for me to figure it out...because I can't
    think of what to try.

    Thanks!
    Liam

  • Geoff Berrow

    #2
    Re: Can't display column of multi-dimensional array

    I noticed that Message-ID:
    <1127000738.976 833.221680@f14g 2000cwb.googleg roups.com> from
    news@celticbear .com contained the following:
    [color=blue]
    >foreach ($a as $v1) {
    > $fieldname = $v1;[/color]
    $v1 is an array and so $fieldname is also an array[color=blue]
    > foreach ($v1 as $v2) {
    > $returned = valid_field($fi eldname,$v2);[/color]
    we don't know what your function does[color=blue]
    > if ($returned != "true") {
    > $error .= $returned;
    > $errflag = "1";
    > }
    > }
    >}[/color]

    Try setting up a simple multidimensiona l array and echoing it to screen.
    Then add your checking function when you understand what's going on.
    --
    Geoff Berrow (put thecat out to email)
    It's only Usenet, no one dies.
    My opinions, not the committee's, mine.
    Simple RFDs http://www.ckdog.co.uk/rfdmaker/

    Comment

    • news@celticbear.com

      #3
      Re: Can't display column of multi-dimensional array


      Geoff Berrow wrote:[color=blue]
      > Try setting up a simple multidimensiona l array and echoing it to screen.
      > Then add your checking function when you understand what's going on.[/color]

      Is this not a "simple multidimensiona l array"? (Wouldn't surprise me if
      it's not.

      OK, here's all of it, plus a bit of code to show what's in the array.
      Using that' I've tried to manipulate it to match the a with the b, but
      it's all coming out as individual items.
      That made no sense.

      $a = array();
      $a[0][0] = "Name";
      $a[0][1] = $name;
      $a[1][0] = "Message";
      $a[1][1] = $msg;
      $a[2][0] = "Subject";
      $a[2][2] = $subject;

      function valid_field($fi eld,$value)
      {
      // return FALSE if it contains characters which
      // which AREN'T on the specified list
      if(ereg("[^[:space:]a-zA-Z0-9_.-\!\\'\,]{1,}", $value))
      {
      $funcerror = $field." value of: <span
      class='bolding' >\"".$value."\" </span> contains invalid characters.<br
      />";
      return $funcerror;
      }
      else
      {
      return true;
      }
      }

      while (list ($x, $tmp) = each ($a)) {
      while (list ($y, $val) = each ($tmp)) {
      echo "$x, $y, $val<br />";
      }
      }
      foreach ($a as $v1) {
      $fieldname = $v1;
      foreach ($v1 as $v2) {
      $returned = valid_field($fi eldname,$v2);
      if ($returned != "true") {
      $error .= $returned;
      $errflag = "1";
      }
      }
      }


      The while loop puts out the following:
      0, 0, Name
      0, 1, Liam
      1, 0, Message
      1, 1, Hello, this is a test msg.
      2, 0, Subject
      2, 2, It's a test subject.

      Comment

      • Ewoud Dronkert

        #4
        Re: Can't display column of multi-dimensional array

        news@celticbear .com wrote:[color=blue]
        > $a = array();
        > $a[0][0] = "Name";
        > $a[0][1] = $name;
        > $a[1][0] = "Message";
        > $a[1][1] = $msg;
        > $a[2][0] = "Subject";
        > $a[2][2] = $subject;[/color]

        I guess you mean [2][1] on the last line. A more instructive array
        definition with the same result:

        $a = array(
        array('Name', $name),
        array('Message' , $msg),
        array('Subject' , $subj)
        );
        [color=blue]
        > foreach ($a as $v1) {
        > $fieldname = $v1;[/color]

        As you can see above, $a[0] is an array containing two items: 'Name' and
        $name. So $v1 (an alias for $a[0], $a[1], etc.) is not the fieldname, but
        $v1[0] is. A better way would be:

        foreach ($a as $field) {
        list($fieldname , $fieldvalue) = $field;

        Or maybe even:

        $a = array(
        array('name' => 'Name', 'value' => $name),
        array('name' => 'Message', 'value' => $msg),
        array('name' => 'Subject', 'value' => $subj)
        );
        foreach ($a as $field)
        echo "{$field['name']} = {$field['value']}<br />\n";

        --
        E. Dronkert

        Comment

        • news@celticbear.com

          #5
          Re: Can't display column of multi-dimensional array

          Thanks for the help!
          I went this way:

          $a = array(
          array('name' => 'Name', 'value' => $name),
          array('name' => 'Message', 'value' => $msg),
          array('name' => 'Subject', 'value' => $subj)
          );

          foreach ($a as $v1) {
          $a1 = $v1['name'];
          $a2 = $v1['value'];
          $returned = valid_field($a1 ,$a2);
          if ($returned != "true") {
          $error .= $returned;
          $errflag = "1";
          }
          }

          I get that; that makes sense to me.
          I was just about the write how I still don't "get" the LIST version,
          and when I went to copy-n-paste it into here...I got it.
          The relationship of the $field, which is the next iteration of the
          array, and the LIST parses the twho columns, right?

          Well, in any case, that works AND I get it!. =)
          Much appreciated!!
          Liam

          Comment

          • Bent Stigsen

            #6
            Re: Can't display column of multi-dimensional array

            news@celticbear .com wrote:[color=blue]
            > Thanks for the help!
            > I went this way:
            >
            > $a = array(
            > array('name' => 'Name', 'value' => $name),
            > array('name' => 'Message', 'value' => $msg),
            > array('name' => 'Subject', 'value' => $subj)
            > );[/color]
            [snip]

            In this case there is no need to use a multidimensiona l array, as
            PHP's arrays are maps, which has many purposes.

            For the above, you might as well use:
            $a = array(
            'Name' => $name),
            'Message' => $msg),
            'Subject' => $subj)
            );

            [snip][color=blue]
            > The relationship of the $field, which is the next iteration of the
            > array, and the LIST parses the twho columns, right?[/color]

            You can say that. An important note is that "list" should be used only
            when the array is sequently indexed like an ordinary array or vector.
            Each "argument"(vari able) passed to "list" will given the value of
            respectively the 1st(indexed zero), 2nd, 3rd and so forth, value in
            the array.
            I.e. given the array:
            $arr = array('a', 'b', 'c', 7 => 'd');

            This:
            list($a, $b, $c) = $arr;

            is equivalent to:
            $a = $arr[0];
            $b = $arr[1];
            $c = $arr[2];

            This:
            list($a, $b, $c, $d) = $arr;

            will give a warning for undefined index, because there is no $arr[3].


            /Bent

            Comment

            Working...