Site icon Anthony Carbon

How to get the last array value in PHP

For example I have ” /category/blog/ ” string and I want to convert it in an array.

#String value

$x = '/category/blog/';

#Convert to array

$x2 = explode( '/', $x );

#Output array

Array (
    [0] => 
    [1] => category
    [2] => blog
    [3] => 
)

#Remove empty array

$x3 = array_filter( $x2 );

#Output array

Array (
    [0] => category
    [1] => blog 
)

#Get the last array

$x4 = end( $x3 ); 

echo $x4;

#Output is :

blog
Exit mobile version