foreach

Discussion in 'PHP' started by personalpa, May 24, 2007.

  1. #1
    I wrote a code for getting data via SOAP using NuSOAP, then parse it and convert it to a php data structure.

    I am using a foreach loop like :

    foreach ($data['item']['optstats'] as $x)
    {
    echo $x['ProgramID'];
    }

    And data structure is like :

    Array
    (
    [item] => Array
    (
    [optstats] => Array
    (
    [0] => Array
    (
    [Date] => 05/23/2007
    [Campaign] => aa
    [ProgramID] => 12
    [Info] => 12
    [Amount] => LEAD N/A
    [TransactionID] => LEAD N/A
    [Commission] => $ 10
    )

    [1] => Array
    (
    [Date] => 05/22/2007
    [Campaign] => qwqw
    [ProgramID] => 5121
    [Info] => 3421
    [Amount] => LEAD N/A
    [TransactionID] => LEAD N/A
    [Commission] => $ 1.00
    )

    It works fine as long as there is more than one row in that array.

    If it has only 1, the output is bogus data.

    How do I fix this ?

    Thanks,
     
    personalpa, May 24, 2007 IP
  2. alemcherry

    alemcherry Guest

    Best Answers:
    0
    #2
    I guess you need one more foreach - ProgramID key is for the third array, not the second as shown in your eg.
     
    alemcherry, May 24, 2007 IP
  3. gibex

    gibex Active Member

    Messages:
    1,060
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    95
    #3
    alemcherry is right :)

    from what I see your code should look like this:

    
    
    foreach ($data as $x)
    {
         $ar = $x['item']['optstats'];
         foreach ($ar as $y) {
            echo $y['ProgramID'];
         }
    }
    
    
    PHP:
     
    gibex, May 24, 2007 IP
  4. personalpa

    personalpa Peon

    Messages:
    98
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thanks.

    If I do that I get an error :

    Warning: Invalid argument supplied for foreach() .

    If two foreach loops are required, why would it work when there are more than one entries ?
     
    personalpa, May 24, 2007 IP
  5. personalpa

    personalpa Peon

    Messages:
    98
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Before this, I was using :

    foreach ($data['item']['optstats'] as $x)
    {
    echo $x['ProgramID'];
    }

    Edited the first post.
     
    personalpa, May 24, 2007 IP
  6. personalpa

    personalpa Peon

    Messages:
    98
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Bump, Anyone ?
     
    personalpa, May 25, 2007 IP
  7. projectshifter

    projectshifter Peon

    Messages:
    394
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #7
    $i=0; before foreach, and $i++; at the end inside the foreach loop. Then echo '$i: '.$x['ProgramID'].'<br />'; and look at the output or so. From what I'm getting you just aren't getting anything showing up, which may mean that what you're trying to print is blank, but exists. This way at least you get a null placeholder and you can tell if it's showing an empty result or no results at all.
     
    projectshifter, May 25, 2007 IP