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?

Dillinger
  • 1,823
  • 4
  • 33
  • 78

5 Answers5

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'];
ʰᵈˑ
  • 11,279
  • 3
  • 26
  • 49
2

From PHP 7.3 you can use of array_key_last function.

$last_key = array_key_last($array);
Hamid Abbasi
  • 346
  • 2
  • 14
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
Jan
  • 42,290
  • 8
  • 54
  • 79
  • Uhm but how I can check if m is the last value inside the foreach? This is the problem – Dillinger Nov 15 '15 at 16:19
  • See my updated answer, the **LAST** item is stored at `count($array)-1` – Jan Nov 15 '15 at 16:34
  • $c and $l should be before $m echo? Could you post the complete code? Thanks – Dillinger Nov 15 '15 at 16:37
  • 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. – Jan Nov 15 '15 at 16:39
  • Oh okay, so I need only the last code. And for check if is the last value? Something like this: if($c < $l) ..? – Dillinger Nov 15 '15 at 16:42
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'];
Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46
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?

hijarian
  • 2,159
  • 1
  • 28
  • 34