Page 1 of 1

using php 5 to loop through an array of objects

Posted: Fri Jun 26, 2009 8:50 am
by sfoley77
pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


Hi I am currently re-writing a manage my account java application into PHP as we bought a new CMS called EZ Publish and moving all our sites into this. now one of the methods I am having a little difficulty with I will display the java code below and hopefully someone can point me in the right direction. The data is coming from a soap responce and then is stored in an array object and then encapsulted in a DTO
Ok so here is the java part of method I am currently having trouble with converting to PHP:

Code: Select all

Address_info_select_responseCustomer_address customerAddresses[] = addressInfoSelectResponse.getCustomer_address();
Address_info_select_responseCustomer_address customerAddress = null;
OAGENetCustomerAddressTO enetCustomerAddressTO = null;
     String state = null;
            
     for (int i = 0; i < customerAddresses.length; i++)     
    {
    customerAddress = customerAddresses[i];             
    enetCustomerAddressTO = new OAGENetCustomerAddressTO()                      enetCustomerAddressTO.setAddressSequence(customerAddress.getCustomer_address_seq());
                
             if(customerAddress.getCompany() != null){
    enetCustomerAddressTO.setCompany(customerAddress.getCompany());
    }
                
    if(customerAddress.getAddress1() != null){
    enetCustomerAddressTO.setAddress1(customerAddress.getAddress1());
    }
Now I would like to know how to write that for loop in php as what I have tried is giving me an error's each time.

Code: Select all

$address_info_select_response = new address_info_select_response();
    $address_info_select_response = $ThinkSoap->AddressInfoSelect($address_info_select_request);
    $customer_addresses = new customer_address();
    $customer_addresses = array();
    $customer_addresses = $address_info_select_response->customer_address;
    $customer_address = new customer_address();
    $customer_address = null;
    
    for ($i = 0; $i < sizeof($customer_addresses); $i++){
    
        $customer_address .= $customer_addresses[$i];
        
        $eclipseAddress = new EclipseNetAddress();
        $eclipseAddress->addressSequence = $customer_address->customer_address_seq;
 
        if ($customer_address->company != null){
            $eclipseAddress->company = $customer_address->company;
            
        }
        
    }
I have tried this code and it is not working, is there something I am missing here, Please advise
the error I get is: Fatal error: Cannot use object of type customer_address as array

Thanks

Sean


pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.

Re: using php 5 to loop through an array of objects

Posted: Fri Jun 26, 2009 2:12 pm
by pickle
What line is causing that error?

This code doesn't make a lot of sense:

Code: Select all

$customer_addresses = new customer_address();
$customer_addresses = array();
$customer_addresses = $address_info_select_response->customer_address;
$customer_address = new customer_address();
$customer_address = null;
You're assigning a value to $customer_address 3 times, and to $customer_address twice. If I'm reading your Java correctly, you want to make $customer_addresses an array of customer addresses. $customer_address shouldn't be assigned until inside the loop, as that's where it's used.

Re: using php 5 to loop through an array of objects

Posted: Mon Jun 29, 2009 3:21 am
by sfoley77
Hi Thanks for your reply,
its at line eleven that the error is thrown when I am trying to assign the value in customer_addresses to customer_address exactly whats happening in the java version. I can assure you that the java version is fully functional and live online currently. what I am trying to do is assign the value for that index of the array customer_addresses to a customer_address object.

Re: using php 5 to loop through an array of objects

Posted: Mon Jun 29, 2009 10:23 am
by pickle
Ah. You have it backwards, and you're using a string concatenator. This is what you want:

Code: Select all

$customer_addresses[$i] = $customer_address;

Re: using php 5 to loop through an array of objects

Posted: Tue Jun 30, 2009 3:11 am
by sfoley77
Thanks alot for your help pickle. That worked great much appreciated.
cheers
Sean

Re: using php 5 to loop through an array of objects

Posted: Wed Jul 01, 2009 4:00 am
by sfoley77
Hi again, sorry for the incorrect spelling of your name. anyway The code didnt work after all and its still is complaining about it I will give an outline of whats happening and hopefully someone can give me some direction and what going wrong.

OK so here is the code for the soap transaction where I am using a loop to go through and populate the eclipseaddress object.
the customer address class is inheriting from ZZcustomerAddress below are those classes

Code: Select all

class ZZCustomerAddress {
    
    public $customer_id;
    public $customer_address_seq;
    public $address1;
    public $address2;
    public $address3;
    public $address_status;
    public $address_type;
    public $audit_company_id;
    public $audit_county;
    public $audit_name_change;
    public $audit_title_change;
    public $carrier;
    public $cass_date;
    public $change_type;
    public $city;
    public $company;
    public $county;
    public $delivery_point;
    public $department;
    public $dp_barcode;
    public $effective_date;
    public $eighthundred;
    public $email;
    public $faxnbr;
    public $fname;
    public $future_temp_exists;
    public $initial_name;
    public $lname;
    public $lot_nbr;
    public $mailstop;
    public $phone;
    public $replace_customer_address_seq;
    public $reverse_date;
    public $salutation;
    public $special_tax_id;
    public $state;
    public $suffix;
    public $supp_clean;
    public $tax_id_number;
    public $title;
    public $zip;
    public $zzaux_channel;
    public $zzaux_dnc;
    public $zzaux_dnc_date_add;
    public $zzaux_qasverified;
            
    
}

Code: Select all

class customer_address extends ZZCustomerAddress {
    /**
     * @access public
     * @var customer_address_match_code[]
     */
    public $customer_address_match_code;
}

Code: Select all

$address_info_select_response = new address_info_select_response();
    $address_info_select_response = $ThinkSoap->AddressInfoSelect($address_info_select_request);
    $customer_addresses = new customer_address();
    [b]$customer_addresses[] = $address_info_select_response->customer_address;[/b]
 
    for ($i = 0; $i < sizeof($customer_addresses); $i++) {
        
        $customer_address = new customer_address(); 
        
        $customer_addresses[$i] = $customer_address;
        
        $eclipseAddress = new EclipseNetAddress();
        $eclipseAddress->addressSequence = $customer_addresses->customer_address_seq;
        
        if ($customer_addresses->company != null){
            $eclipseAddress->company = $customer_address->company;
        }
        if ($customer_addresses->address1 != null){
            $eclipseAddress->address1 = $customer_address->address1;
        }
        if ($customer_addresses->address2 != null){
            $eclipseAddress->address2 = $customer_address->address2;
        }
        if ($customer_addresses->address3 != null){
            $eclipseAddress->address3 = $customer_address->address3;
        }
        if ($customer_addresses->city != null){
            $eclipseAddress->city = $customer_address->city;
        }
its giving the error on this line $customer_addresses[] = $address_info_select_response->customer_address;
error code = Fatal error: Cannot use object of type customer_address as array

Re: using php 5 to loop through an array of objects

Posted: Wed Jul 01, 2009 9:30 am
by BornForCode
Check this solution: ArrayIterator
And once you are there check this also: ArrayObject

Re: using php 5 to loop through an array of objects

Posted: Thu Jul 02, 2009 9:44 am
by pickle
This line creates $customer_addresses as an object:

Code: Select all

$customer_addresses = new customer_address();
This one treats it like an array

Code: Select all

$customer_addresses[] = $address_info_select_response->customer_address;
It might be easiest to change your Customer_Address() object to have a "addresses" property that is an array. You could then change the line above to read:

Code: Select all

$customer_addresses->addresses[] = $address_info_select_response->customer_address;