3

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?

5
  • see this Q&A stackoverflow.com/questions/2348205/… could be of help. Commented Nov 15, 2015 at 16:15
  • Thanks but as I said I've more array in the same variable so end not working properly in my case. Commented Nov 15, 2015 at 16:20
  • You're welcome. I did say "could be" ;-) Commented Nov 15, 2015 at 16:20
  • is "SUCCESS" part of the array too ? Commented Nov 15, 2015 at 16:21
  • Oh no sorry, was the response of ajax request that I've inadvertently copied, sorry for this. Commented Nov 15, 2015 at 16:27

5 Answers 5

2

Get the last array index

Simply invert the array, then use end

echo end(array_keys($s));

Get all contents of the last array index

Simply use end through an iteration

foreach($appointments as $app) {
   echo end($app) . PHP_EOL;
}

Get only the last element from the sub-array (only output 13)

Simply grab the last sub-array and put it through end

echo end($appointments[ count($appointments) - 1 ]);

And if you want to just get id_services as you can't guarantee this key will always be last, simply reference it as follows;

echo $appointments[ count($appointments) - 1 ]['id_services'];
Sign up to request clarification or add additional context in comments.

Comments

2

From PHP 7.3 you can use of array_key_last function.

$last_key = array_key_last($array);

Comments

1

The following code assumes that $services are only numbers. It iterates over all numbers, checks if the current is greater than $m and eventually stores a new $m:

$m = 0;
foreach($appointment['id_services'] as $services)
    $m = ($services > $m)?$services:$m;

// after the iteration $m has the maximum value
echo $m;

EDIT: To get the LAST (not necessarily the greatest), you could do sth. like this:

$c = count($appointment['id_services']);
$l = $appointment['id_services'][$c-1]; // 13

5 Comments

Uhm but how I can check if m is the last value inside the foreach? This is the problem
See my updated answer, the LAST item is stored at count($array)-1
$c and $l should be before $m echo? Could you post the complete code? Thanks
Just to be clear, the first code gets the maximum number while the last code gets the last number. They work independently from each other.
Oh okay, so I need only the last code. And for check if is the last value? Something like this: if($c < $l) ..?
0

Can you not do something like this?

$appointments=array( 
    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 )
);

echo $appointments[ count( $appointments )-1 ]['id_services'];

1 Comment

I need to check if the actual $service iteration is the last key
0

Man, to get last element of an array you do end($array). In your case it's end($appointments). After you get last element, which is in turn a associative array with keys 'id_service' and so on, you just get the value you need, like end($appointments)['id_service'], that's all, what's wrong?

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.