Help with foreach and case returning

Discussion in 'PHP' started by glasglow, Mar 25, 2009.

  1. #1
    I am using this and when I am echoing there, it's coming from an array and the results are not even. For example if A goes with 1, I am getting B with 1 and c with 2 etc.. The results are uneven which I think is because of the case. Is there anyway that I can even them out on the return?



       foreach ($values as $key=>$val) 
         {
    
          switch ($val['tag'])
           {
            case 'DetailPageURL':
              $DetailPageURL = $val['value'];
    		  echo '<img border="0" src="'.$MediumImage.'" height="100">';
              break;
            case 'ItemAttributes':
              $inItemAttributes = ($val['type'] == 'open');
    		  echo '<h1><a href='.$DetailPageURL.'>'.$itemAttributes['Title'].'</a></h1><br>'.$itemAttributes['FormattedPrice'].'<hr>';
              break;
            case 'MediumImage':
              $inMediumImage = ($val['type'] == 'open');
              break;
            case 'EditorialReview':
              $inEditorialReview = ($val['type'] == 'open');
              break;
            case 'Content':
              if ($inEditorialReview) $EditorialReview = $val['value'];
              break;
            case 'URL':
              if ($inMediumImage) $MediumImage = $val['value'];
              break;
            default:
              if ($inItemAttributes)
                {
                 $itemAttributes[$val['tag']] = $val['value'];
                }
           }
         }
       //$Title = $itemAttributes['Title'];
    
       //echo "<h1><a href=\"$DetailPageURL\">$Title</a></h1>";
    
       //echo "<img src=\"$MediumImage\" align=\"right\">";
    
       //echo $EditorialReview; 
      }
    PHP:

     
    glasglow, Mar 25, 2009 IP
  2. centralb

    centralb Peon

    Messages:
    26
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Can you clarify with an example what you mean by this?

    From a quick glance: You might want to clear some or all of the contents of $itemAtributes before the switch block. Each iteration of foreach is re-using $itemAttributes that was changed in previous iterations. If this isn't intentional, start there.
     
    centralb, Mar 25, 2009 IP
  3. glasglow

    glasglow Active Member

    Messages:
    926
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #3
    Actually that is an array.

    $inItemAttributes = false;
       $inMediumImage = false;
       $inEditorialReview = false;
       $itemAttributes = array();
       foreach ($values as $key=>$val) 
         {
    
          switch ($val['tag'])
           {
            case 'DetailPageURL':
              $DetailPageURL = $val['value'];
    		  echo '<img border="0" src="'.$MediumImage.'" height="100">';
              break;
            case 'ItemAttributes':
              $inItemAttributes = ($val['type'] == 'open');
    		  echo '<h1><a href='.$DetailPageURL.'>'.$itemAttributes['Title'].'</a></h1><br>'.$itemAttributes['FormattedPrice'].'<hr>';
              break;
            case 'MediumImage':
              $inMediumImage = ($val['type'] == 'open');
              break;
            case 'EditorialReview':
              $inEditorialReview = ($val['type'] == 'open');
              break;
            case 'Content':
              if ($inEditorialReview) $EditorialReview = $val['value'];
              break;
            case 'URL':
              if ($inMediumImage) $MediumImage = $val['value'];
              break;
            default:
              if ($inItemAttributes)
                {
                 $itemAttributes[$val['tag']] = $val['value'];
                }
           }
         }
    PHP:
     
    glasglow, Mar 25, 2009 IP
  4. centralb

    centralb Peon

    Messages:
    26
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Yes, from the usage in your sample code, it was clear that $itemAttributes is an array there. However, without further details, the recommendation is the same:

    Please carefully read and provide additional detail if your problem persists. Good luck!
     
    centralb, Mar 25, 2009 IP
  5. glasglow

    glasglow Active Member

    Messages:
    926
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #5
    I see I see.. Thanks for the help.
     
    glasglow, Mar 25, 2009 IP