0

I've found some similar question on StackOverflow, but my problem is different. I'll try to explain more clear possible. First of all the array structure: $appointment

Array ( 
  [id_users_provider] => 85  
  [start_datetime] => 2015-11-15 17:15:00  
  [end_datetime] => 2015-11-15 17:15:00  
  [notes] =>  
  [is_unavailable] =>  
  [id_users_customer] => 87  
  [id_services] => 15 
)
Array (  
  [id_users_provider] => 85  
  [start_datetime] => 2015-11-15 17:15:00  
  [end_datetime] => 2015-11-15 17:15:00  
  [notes] =>  
  [is_unavailable] =>  
  [id_users_customer] => 87  
  [id_services] => 13  
)

How you can see I've two array included in the $appointment variable. Now I want get the end of the last array, in this case the array with id_services: 13. I actually execute an iteration through the appointment['id_services']. Like this:

foreach($appointment['id_services'] as $services)
{
   print_r(end($appointment));
}

but this return me:

15
13

and this is wrong, 'cause I want get only 13 in this case. How I can do that? The main problem is to check inside the foreach loop if the actual $services is the last key of the last array inside the foreach loop.

2
  • use count() function to know how many elements there are inside each array, and then just substract one to that number and you will get the last element Commented Nov 16, 2015 at 14:21
  • 1
    That loop, the result, and the explanation hardly make any sense whatsoever. Commented Nov 16, 2015 at 14:23

2 Answers 2

4

Man, why not just end($appointment)['id_services']? Why do you need a foreach in this case?

$last_appointment = end($appointment);
$id_services = $last_appointment['id_services'];
$id_services === 13; // true
Sign up to request clarification or add additional context in comments.

4 Comments

How why? I've multiple array inside the $appointment variable, how I said I must check which is the last key of the last array actually in the loop. I don't need to check the last index of the current array.
@Jss What is the difference between "last key" and "last index"?
@Jss You said you don't want 15, 13 output, just 13, right? Then, you get the last element from your source array using end(), and then you get the value under the last key in that array! If you don't know the keys, use the array_keys function to get them and run the same end function over the result.
@Jss If this is not the desired result you wanted you may want to rephrase your question. However, If this answer resolved your problem, could you mark it as accepted by clicking the green checkmark next to the answer? It helps the community know that this question has been resolved. Thanks!
1

Your foreach loop is wrong....

Two easy ways to do it...

// using count (not recommended)
echo $appointment[count($appointment)-1]['id_services'];


//using end
echo end($appointment)['id_services'];

based on your comments you might be trying to do this (which i fail to understand why)

$last_appointment = end($appointment);
echo end($last_appointment);

A fix of your code

//not recommended!!
foreach(end($appointment) as $services)
{
   print_r(end($services));
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.