我有以下数组:
["theme","1,strand","Medical Ethics and Law,strandyear","Year 3"]如何提取数组中的第二个最后值,然后在逗号后面提取该值的第二部分。“医学伦理与法律,绞刑年”?
例如,这应该会导致“strand放年”。
对于其他数组,实际值(而不是末尾的位置)将有所不同。
PHP 5.3.3..。
发布于 2019-06-17 06:22:31
那下面呢..。
$secondLast = array_slice($source, -2, 1);切片函数可以在一行代码中完成这项工作。
然后你从原来的数组中得到最后一个条目,现在你可以用逗号打开字符串了。
$parts = explode(",", $secondLast);
$strand = array_pop($parts);第一行用逗号爆炸字符串。第二行给出了所有爆炸部件的最后一部分(链)。
发布于 2019-06-17 06:18:53
试试这个,先做json_decode(),然后得到secondLast,然后做strstr(),在逗号之后得到值,do ltrim()就是这样。
$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;产出:
["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发布于 2019-06-17 06:19:28
获取数组中的第二个最后值的最佳方法
end($array);
$second_last = prev($array);之后,您无法得到第二部分,即您可以使用的逗号后面的值。
explode(separator,string) //function in PHP
$temp= explode(',', $second_last);//converted to an array
$second_part=$temp[1];https://stackoverflow.com/questions/56625552
复制相似问题