In this tutorial we’re going to share how to print first 50 value of data and the last value of data
To print the first 50 values and the last 10 values of an array in PHP, you can use a combination of array_slice()
and implode()
functions. Here’s how you can do it:
1st step create below file
index.php
1st example:-
<?php
$string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed tristique purus vitae mauris cursus ultrices. Nulla facilisi. Nullam at enim vel velit elementum varius. Morbi auctor, magna sit amet malesuada ultrices, urna augue vestibulum libero, id rhoncus eros nisi id odio. Vivamus aliquet ultrices sem, a interdum quam imperdiet vitae. Etiam nec dolor ullamcorper, fringilla massa eu, dictum justo. Praesent luctus justo sit amet tincidunt dictum. Donec id eros id mauris congue mollis. Sed tincidunt euismod odio, eget maximus justo mattis et.";
$words = explode(" ", $string);
$first_50_words = implode(" ", array_slice($words, 0, 50));
echo $first_50_words;
?>
Output:-
2nd example print last 10 value
<?php
$string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed tristique purus vitae mauris cursus ultrices. Nulla facilisi. Nullam at enim vel velit elementum varius. Morbi auctor, magna sit amet malesuada ultrices, urna augue vestibulum libero, id rhoncus eros nisi id odio. Vivamus aliquet ultrices sem, a interdum quam imperdiet vitae.Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed tristique purus vitae mauris cursus ultrices. Nulla facilisi. Nullam at enim vel velit elementum varius. Morbi auctor, magna sit amet malesuada ultrices, urna augue vestibulum libero, id rhoncus eros nisi id odio. Vivamus aliquet ultrices sem, a interdum quam imperdiet vitae.";
$convert_to_array = explode(" ",$string);
$get_last_value = implode(" ",array_slice($convert_to_array,-10));
print_r($get_last_value);
?>
Output:-