0

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...

asked Jun 17, 2019 at 6:10
2
  • $secondValueAfterComma = explode(',',$arr[1])[1]; Commented Jun 17, 2019 at 6:15
  • won't work with php 5.3.3 Commented Jun 17, 2019 at 6:16

6 Answers 6

2

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.

answered Jun 17, 2019 at 6:22
2
  • OK I have this result ["Year 4,unit"] just using array_slice with $row->type = array_slice(explode ( ':', $row->global_id), -2, 1); Commented 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. Commented Jun 17, 2019 at 6:39
1

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
answered Jun 17, 2019 at 6:18
1

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];
answered Jun 17, 2019 at 6:19
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"
answered Jun 17, 2019 at 6:24
0

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];
answered Jun 17, 2019 at 6:26
0

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];
answered Jun 17, 2019 at 6:29

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.