PHP Count A Child of XML Result



I'm trying to count the number of checkin_id's for each customer. Currently im looping through the customers, then using a nested loop to loop through the checkin ids. My thought was to put them in array then use count($checkin_ids). But, what happens is when the $ids array is printed for each customer, it is cumlative containing the checkin_ids from all previous loops. For example, by the end of the loop, the array contains all checkin_ids for all customers. I need a count per customer.


May data looks like this:



<customers>
<customer>
<customer_id>7234872934</customer_id>
<firstname>customer</firstname>
<middlename/>
<lastname>name</lastname>
<dob>1982-10-02</dob>
<checkins>
<checkin>
<checkin_id>89764</checkin_id>
<checkin_time>11:00</checkin_time>
<checkin_id>874629</checkin_id>
<checkin_time>2:00</checkin_time>
</checkin>
</checkins>
<primary_email>customer@email.com</primary_email>
</customer>

<customer>
<customer_id>7234872934</customer_id>
<firstname>customer</firstname>
<middlename/>
<lastname>name</lastname>
<dob>1982-10-02</dob>
<checkin>
<checkin_id>89764</checkin_id>
<checkin_time>11:00</checkin_time>
<checkin_id>874629</checkin_id>
<checkin_time>2:00</checkin_time>
</checkin>
<primary_email>customer@email.com</primary_email>
</customer>

</customers>


My loops



$ids = array();

foreach($customer_result->customers->customer as $customer){
$email = $customer->primary_email;
$dob = $customer->dob;
$firstname = $customer->firstname;
$lastname = $customer->lastname;

//Count Number of Checkins
foreach ($customer->checkins->checkin->checkin_id as $checkin_id) {
$ids[] = $checkin_id;
}

print_r($ids);
count($ids);

}

No comments:

Post a Comment