Problem reading an one element array?

Discussion in 'PHP' started by 123GoToAndPlay, Aug 20, 2007.

  1. #1
    Hi all,

    The array is dyn. being populated sometimes it has just one element. If it does my foreach statement doesn't work.
    How can i fix this?
     
    123GoToAndPlay, Aug 20, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    Post your code.
     
    nico_swd, Aug 20, 2007 IP
  3. 123GoToAndPlay

    123GoToAndPlay Peon

    Messages:
    669
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    sorry i must simplefy my code to post here ;)

    But i made a mistake it turns out my array consists of arrays. And i have the problem when it my array just has one array.
    In the case my array has one arrayA as an element. The sizeof($myArray) is the length of arrayA and not 1???

    
    //working
    $roomsAvailable = [
    Array
    (
        [TotalRate] => 460];
    )
    Array
    (
        [TotalRate] => 230];
    )
    ]
    //non working
    $roomsAvailable = 
    Array
    (
        [TotalRate] => 460];
    )
    
    Code (markup):
    
    foreach($roomsAvailable as $rooms => $value) {
    		$totalPrice = $value['TotalRate'];
    }
    
    Code (markup):
    Hope you get what i am after?
     
    123GoToAndPlay, Aug 20, 2007 IP
  4. 123GoToAndPlay

    123GoToAndPlay Peon

    Messages:
    669
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    
    Array
    (
        [RoomType] => wert
        [RoomDescription] => wert
        [InfoText] => 
        [MinimumRate] => 105
        [MaximumRate] => 105
        [ArrivalRate] => 105
        [ArrivalTax] => 0
        [TotalRate] => 105
        [TotalTax] => 0
        [Available] => true
        [Rates] => Array
            (
                [RateDate] => Array
                    (
                        [Date] => 2007-08-25T00:00:00
                        [Rate] => 105
                        [MealplanRate] => 0
                        [Tax] => 0
                    )
    
            )
    
    )
    
    Code (markup):
    here i can use count or sizeof

    
    Array
    (
        [0] => Array
            (
                [RoomType] => er
                [RoomDescription] => ewrt
                [InfoText] => 
                [MinimumRate] => 125
                [MaximumRate] => 125
                [ArrivalRate] => 125
                [ArrivalTax] => 0
                [TotalRate] => 125
                [TotalTax] => 0
                [Available] => true
                [Rates] => Array
                    (
                        [RateDate] => Array
                            (
                                [Date] => 2007-08-29T00:00:00
                                [Rate] => 125
                                [MealplanRate] => 0
                                [Tax] => 0
                            )
    
                    )
    
            )
    
        [1] => Array
            (
                [RoomType] => asdf
                [RoomDescription] => asdfr
                [InfoText] => 
                [MinimumRate] => 125
                [MaximumRate] => 125
                [ArrivalRate] => 125
                [ArrivalTax] => 0
                [TotalRate] => 125
                [TotalTax] => 0
                [Available] => true
                [Rates] => Array
                    (
                        [RateDate] => Array
                            (
                                [Date] => 2007-08-29T00:00:00
                                [Rate] => 125
                                [MealplanRate] => 0
                                [Tax] => 0
                            )
    
                    )
    
            )
    )
    
    Code (markup):
     
    123GoToAndPlay, Aug 20, 2007 IP
  5. 123GoToAndPlay

    123GoToAndPlay Peon

    Messages:
    669
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #5
    123GoToAndPlay, Aug 21, 2007 IP
  6. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #6
    Pretty much, they're telling you to do this:
    
    
    function getArrayFromResponse($data)
    {
        if (sizeof($data) < 2) {
            $new = array();
            $new[] = $data;
            return $new;
        }
       
        return $data;
    }
    
    foreach(getArrayFromResponse($roomsAvailable) as $rooms => $value) {
    
    PHP:
    Try this.
     
    nico_swd, Aug 21, 2007 IP
  7. ssanders82

    ssanders82 Peon

    Messages:
    77
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Don't know if this makes it less confusing, but here's another way of showing the same thing:


    
    // If it's not an array of arrays, make it into one
    if (!isset($roomsAvailable[0])) $roomsAvailable = array($roomsAvailable);
    
    foreach($roomsAvailable as $rooms => $value) 
    {
       $totalPrice = $value['TotalRate'];
    }
    Code (markup):
     
    ssanders82, Aug 21, 2007 IP
  8. 123GoToAndPlay

    123GoToAndPlay Peon

    Messages:
    669
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #8
    @ssanders82, thx for thinking along

    but i found this solution
    
    function getArrayFromResponse($data)
    {
        $test = is_array($data)?$data:array($data);
        if(is_array($test[0])) {
            $data = $data;
        } else { 
            $data = array($data);
        }
        return $data;
    }
    
    Code (markup):
     
    123GoToAndPlay, Aug 22, 2007 IP