Forech question (Sorry if duplicate)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Lorenzo Thurman

    #1

    Forech question (Sorry if duplicate)

    I'm passing an array which contains 2 arrays as subarrays to a function
    that uses foreach to iterate through each sub-array. Each subarray has
    exactly 7 items.
    function listitems($anAr ray){

    // $anArray is 2-dim array
    foreach($anArra y as $item=>$arr){
    var_dump($arr);

    }

    array(2) {
    [0]=>
    array(1) {
    ["X"]=>
    string(4) "this"
    }
    [1]=>
    array(1){
    ["y"]=>
    string(3) "xyz"
    }
    Instead the items show up as scalars. Like this:
    string[5]='string'
    string[3]='xyz'

    In short, I would expect the foreach to create an array containing 1
    item from subarray[0] and subarray[1] and display it as such. So clearly
    I don't understand something, but how do I reference each item?

  • Mike P2

    #2
    Re: Forech question (Sorry if duplicate)

    On May 11, 5:38 pm, Lorenzo Thurman <lore...@diespa mmerhethurmans. com>
    wrote:
    I'm passing an array which contains 2 arrays as subarrays to a function
    that uses foreach to iterate through each sub-array. Each subarray has
    exactly 7 items.
    function listitems($anAr ray){
    >
    // $anArray is 2-dim array
    foreach($anArra y as $item=>$arr){
    var_dump($arr);
    >
    }
    >
    array(2) {
    [0]=>
    array(1) {
    ["X"]=>
    string(4) "this"
    }
    [1]=>
    array(1){
    ["y"]=>
    string(3) "xyz"}
    >
    Instead the items show up as scalars. Like this:
    string[5]='string'
    string[3]='xyz'
    >
    In short, I would expect the foreach to create an array containing 1
    item from subarray[0] and subarray[1] and display it as such. So clearly
    I don't understand something, but how do I reference each item?
    I think you may be doing something wrong. It works for me. Or maybe I
    don't understand your question. Here's what I tested:
    <?php
    $t = array(
    array( 'a1', 'b1' ),
    array( 'a2', 'b2' )
    );

    foreach( $t as $i =$v )
    var_dump( $v );
    ?>

    And here's what I get:
    array(2) {
    [0]=>
    string(2) "a1"
    [1]=>
    string(2) "b1"
    }
    array(2) {
    [0]=>
    string(2) "a2"
    [1]=>
    string(2) "b2"
    }

    Is this test similar to yours?

    -Mike PII

    Comment

    • Lorenzo Thurman

      #3
      Re: Forech question (Sorry if duplicate)

      Mike P2 wrote:
      On May 11, 5:38 pm, Lorenzo Thurman <lore...@diespa mmerhethurmans. com>
      wrote:
      >I'm passing an array which contains 2 arrays as subarrays to a function
      >that uses foreach to iterate through each sub-array. Each subarray has
      >exactly 7 items.
      >function listitems($anAr ray){
      >>
      >// $anArray is 2-dim array
      >foreach($anArr ay as $item=>$arr){
      > var_dump($arr);
      >>
      >}
      >>
      >array(2) {
      > [0]=>
      > array(1) {
      > ["X"]=>
      > string(4) "this"
      > }
      > [1]=>
      > array(1){
      > ["y"]=>
      > string(3) "xyz"}
      >>
      >Instead the items show up as scalars. Like this:
      >string[5]='string'
      >string[3]='xyz'
      >>
      >In short, I would expect the foreach to create an array containing 1
      >item from subarray[0] and subarray[1] and display it as such. So clearly
      > I don't understand something, but how do I reference each item?
      >
      I think you may be doing something wrong. It works for me. Or maybe I
      don't understand your question. Here's what I tested:
      <?php
      $t = array(
      array( 'a1', 'b1' ),
      array( 'a2', 'b2' )
      );
      >
      foreach( $t as $i =$v )
      var_dump( $v );
      ?>
      >
      And here's what I get:
      array(2) {
      [0]=>
      string(2) "a1"
      [1]=>
      string(2) "b1"
      }
      array(2) {
      [0]=>
      string(2) "a2"
      [1]=>
      string(2) "b2"
      }
      >
      Is this test similar to yours?
      >
      -Mike PII
      >
      Your setup is the same, but the result is different. Its what I would
      expect. I just get those unreferenced scalars.

      Comment

      • Mike P2

        #4
        Re: Forech question (Sorry if duplicate)

        On May 14, 8:02 am, Lorenzo Thurman <lore...@diespa mmerhethurmans. com>
        wrote:
        Your setup is the same, but the result is different. Its what I would
        expect. I just get those unreferenced scalars.
        Verify that the array you are passing to your function is like mine.

        -Mike PII

        Comment

        Working...