Dillinger
Dillinger

Reputation: 1903

How to get last array index?

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?

Upvotes: 3

Views: 11618

Answers (5)

Hamid Abbasi
Hamid Abbasi

Reputation: 343

From PHP 7.3 you can use of array_key_last function.

$last_key = array_key_last($array);

Upvotes: 2

ʰᵈˑ
ʰᵈˑ

Reputation: 11375

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'];

Upvotes: 2

hijarian
hijarian

Reputation: 2228

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?

Upvotes: 0

Jan
Jan

Reputation: 43169

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

Upvotes: 1

Professor Abronsius
Professor Abronsius

Reputation: 33813

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'];

Upvotes: 0

Related Questions