SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot
    Join Date
    Feb 2005
    Location
    Aveiro, Portugal
    Posts
    165
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Recursively traverse a multidimensional array

    Hi all,

    I have an array as so:

    PHP Code:
    PharmacyList_t Object
    (
        [
    Total] => 4
        
    [Pharmacies] => ArrayOfPharmacy_t Object
            
    (
                [
    Pharmacy] => Array
                    (
                        [
    0] => Pharmacy_t Object
                            
    (
                                [
    LastUpdate] => 2009-06-02T09:30:01+01:00
                                
    [Code] => 8729
                                
    [Name] => Farmácia Bello Marques
                                
    [Address] => Address_t Object
                                    
    (
                                        [
    Street] => Rua Alexandre Herculano23
                                        
    [Coordinates] => Coordinates_t Object
                                            
    (
                                                [
    Latitude] => 39.5494633175
                                                
    [Longitude] => -8.97972721326
                                            
    )

                                        [
    ZipCode] => 2460-022
                                        
    [DistrictId] => 10
                                        
    [District] => Leiria
                                        
    [MunicipalityId] => 1001
                                        
    [Municipality] => Alcobaça
                                        
    [ParishId] => 100101
                                        
    [Parish] => Alcobaça
                                    
    )

                                [
    Phone] => 262582115
                                
    [Fax] => 262596511
                                
    [Director] => 
                                [
    Distance] => -1
                                
    [Services] => ArrayOfService_t Object
                                    
    (
                                        [
    Service] => Service_t Object
                                            
    (
                                                [
    Date] => 2009-06-02T00:00:00
                                                
    [Type] => PERMANENTE
                                                
    [LastUpdate] => 2009-06-02T09:30:01+01:00
                                            
    )

                                    )

                                [
    IsAtService] => 1
                                
    [IsPermanent] => 1
                                
    [IsLateNight] => 
                            )

                        [
    1] => Pharmacy_t Object
                            
    (
                                [
    LastUpdate] => 2009-06-02T09:30:01+01:00
                                
    [Code] => 14630
                                
    [Name] => Farmácia Vilhena
                                
    [Address] => Address_t Object
                                    
    (
                                        [
    Street] => Rua do Louriçal1
                                        
    [Coordinates] => Coordinates_t Object
                                            
    (
                                                [
    Latitude] => 39.9133355627
                                                
    [Longitude] => -8.63050001817
                                            
    )

                                        [
    ZipCode] => 3100-428
                                        
    [DistrictId] => 10
                                        
    [District] => Leiria
                                        
    [MunicipalityId] => 1015
                                        
    [Municipality] => Pombal
                                        
    [ParishId] => 101509
                                        
    [Parish] => Pombal
                                    
    )

                                [
    Phone] => 236212067
                                
    [Fax] => 236207399
                                
    [Director] => 
                                [
    Distance] => -1
                                
    [Services] => ArrayOfService_t Object
                                    
    (
                                        [
    Service] => Service_t Object
                                            
    (
                                                [
    Date] => 2009-06-02T00:00:00
                                                
    [Type] => PERMANENTE
                                                
    [LastUpdate] => 2009-06-02T09:30:01+01:00
                                            
    )

                                    )

                                [
    IsAtService] => 1
                                
    [IsPermanent] => 1
                                
    [IsLateNight] => 
                            )

                        [
    2] => Pharmacy_t Object
                            
    (
                                [
    LastUpdate] => 2009-06-02T09:30:01+01:00
                                
    [Code] => 13358
                                
    [Name] => Farmácia Higiene
                                
    [Address] => Address_t Object
                                    
    (
                                        [
    Street] => Rua Tenente Valadim29
                                        
    [Coordinates] => Coordinates_t Object
                                            
    (
                                                [
    Latitude] => 39.742586834
                                                
    [Longitude] => -8.80389571326
                                            
    )

                                        [
    ZipCode] => 2410-190
                                        
    [DistrictId] => 10
                                        
    [District] => Leiria
                                        
    [MunicipalityId] => 1009
                                        
    [Municipality] => Leiria
                                        
    [ParishId] => 100912
                                        
    [Parish] => Leiria
                                    
    )

                                [
    Phone] => 244833140
                                
    [Fax] => 244812618
                                
    [Director] => 
                                [
    Distance] => -1
                                
    [Services] => ArrayOfService_t Object
                                    
    (
                                        [
    Service] => Service_t Object
                                            
    (
                                                [
    Date] => 2009-06-02T00:00:00
                                                
    [Type] => PERMANENTE
                                                
    [LastUpdate] => 2009-06-02T09:30:01+01:00
                                            
    )

                                    )

                                [
    IsAtService] => 1
                                
    [IsPermanent] => 1
                                
    [IsLateNight] => 
                            )

                        [
    3] => Pharmacy_t Object
                            
    (
                                [
    LastUpdate] => 2009-06-02T09:30:01+01:00
                                
    [Code] => 11533
                                
    [Name] => Farmácia Rosa
                                
    [Address] => Address_t Object
                                    
    (
                                        [
    Street] => Avenida 1 de Maio12A -  R/c
                                        
    [Coordinates] => Coordinates_t Object
                                            
    (
                                                [
    Latitude] => 39.4068426226
                                                
    [Longitude] => -9.13772410473
                                            
    )

                                        [
    ZipCode] => 2500-081
                                        
    [DistrictId] => 10
                                        
    [District] => Leiria
                                        
    [MunicipalityId] => 1006
                                        
    [Municipality] => Caldas da Rainha
                                        
    [ParishId] => 100603
                                        
    [Parish] => Caldas da Rainha (N Senhora do Pópulo)
                                    )

                                [
    Phone] => 262831996
                                
    [Fax] => 262824694
                                
    [Director] => 
                                [
    Distance] => -1
                                
    [Services] => ArrayOfService_t Object
                                    
    (
                                        [
    Service] => Service_t Object
                                            
    (
                                                [
    Date] => 2009-06-02T00:00:00
                                                
    [Type] => PERMANENTE
                                                
    [LastUpdate] => 2009-06-02T09:30:01+01:00
                                            
    )

                                    )

                                [
    IsAtService] => 1
                                
    [IsPermanent] => 1
                                
    [IsLateNight] => 
                            )

                    )

            )


    I'm trying to flatten this multidimensional array with no success.

    I've tried some function like:

    PHP Code:
    function got_one($arr,&$str){
        foreach(
    $arr as $k => $v){
            if(
    is_array($v)){
                
    $str .=$k::<br />\n";
                
    got_one($v,$str);
            }else{
                
    $str .=$k:$v<br />\n";
            }
        }

    But I always get the error:
    Catchable fatal error: Object of class ArrayOfPharmacy_t could not be converted to string on the line with:
    PHP Code:
    $str .=$k:$v<br />\n"
    Thanks for your attention.

    Mrio
    Yours truely
    Mrio Ramos

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    This is not a multidimensional array, it's an object containing an array of objects. You're going to need to write code specific to serializing those objects. You can't use a foreach loop to directly iterate the properties of the objects.

  3. #3
    SitePoint Zealot
    Join Date
    Feb 2005
    Location
    Aveiro, Portugal
    Posts
    165
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Hi Dan,

    Can you provide some code or point to a tuturial showing how to do just that?

    Thanks
    Yours truely
    Mrio Ramos

  4. #4
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If the properties are public, foreach can iterate them.

    You need to test $v to see what kind of data it is before trying to use it as a string. See is_array() and is_object() and friends.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •