I have the following array:
["theme","1,strand","Medical Ethics and Law,strandyear","Year 3"]
How can I extract the second last value in the array, then the second part of that value after the comma e.g. "Medical Ethics and Law,strandyear"?
This should result, for example, in 'strandyear'.
The actual value, not the position from the end, will be different for other arrays.
PHP 5.3.3...
6 Answers 6
What about the following ...
$secondLast = array_slice($source, -2, 1);
The PHP function array_slice can do the job in one line of code.
Then you 've got the second last entry from the original array. Now you can explode the string with a comma.
$parts = explode(",", $secondLast);
$strand = array_pop($parts);
The first line explodes the string by a comma. The second line gives you the last part (strand) of all exploded parts.
-
OK I have this result ["Year 4,unit"] just using array_slice with $row->type = array_slice(explode ( ':', $row->global_id), -2, 1);IlludiumPu36– IlludiumPu362019年06月17日 06:33:47 +00:00Commented Jun 17, 2019 at 6:33
-
The end() function is good for that. You can use array_pop() also. The end() function sets the internal pointer to the end and gives you the last entry of the array. The array_pop() function pops the last entry of the array. By using array_pop() the array will be shortened by the last entry. A little advice from me.For readability you should not nest your functions like that. Also, debugging becomes easier if you store the result of each function call in a variable.Marcel– Marcel2019年06月17日 06:39:57 +00:00Commented Jun 17, 2019 at 6:39
Try This,
First do json_decode()
then get secondLast
then do strstr()
to get value after comma and the do ltrim()
thats it.
$str = '["theme","1,strand","Medical Ethics and Law,strandyear","Year 3"]';
echo $str;
$array = json_decode($str);
$secondLast = $array[2];//count($array)-2
echo'<pre>';print_r($array);
echo $secondLast.'<pre>';
echo ltrim(strstr($secondLast, ','),',');
die;
OUTPUT:
["theme","1,strand","Medical Ethics and Law,strandyear","Year 3"]
Array
(
[0] => theme
[1] => 1,strand
[2] => Medical Ethics and Law,strandyear
[3] => Year 3
)
Medical Ethics and Law,strandyear
strandyear
Best way to get the second last value in the array
end($array);
$second_last = prev($array);
after that you cant get the second part i.e the value after comma you can use
explode(separator,string) //function in PHP
$temp= explode(',', $second_last);//converted to an array
$second_part=$temp[1];
<?php
$a = ["theme","1,strand","Medical Ethics and Law,strandyear","Year 3"];
$b = explode(',',$a[1]);
$c = $b[1];
var_dump($c);
// tring(6) "strand"
Use explode explode
Returns an array of strings, each of which is a substring of string formed by splitting it on boundaries formed by the string delimiter.
usage: explode ( string $delimiter , string $string [, int $limit = PHP_INT_MAX ] ) : array
$arr = Array("theme","1,strand","Medical Ethics and Law,strandyear","Year 3");
$secondValueAfterComma = explode(',',$arr[2]);
echo $secondValueAfterComma[1];
You may do like
$arr = ["theme","1,strand","Medical Ethics and Law,strandyear","Year 3"];
Here is your code to access 2nd last element
$value = $arr[count($arr)-2];
$secondValueAfterComma = explode(',',$arr[1])[1];