-2

I have array data like this :

$word[0]="search";
$word[1]="journal";
$word[2]="information";
$word[3]="system";

If I make PHP code like this :

$output=implode(" ",$word);

The output (implode result) is a combination of word ("search journal information system"). If I want to implode from index 2 so the result="information system". How the solution this problem?

Atina
  • 17
  • 6

4 Answers4

3

Using array_slice() function get last two element and after implode this array

<?php
 $word[0]="search";
 $word[1]="journal";
 $word[2]="information";
 $word[3]="system";
 $word=array_slice($word, -2, 2, true);
 $output=implode(" ",$word);
 echo $output; //information system
?>
Reena Mori
  • 647
  • 6
  • 15
2

You can only pass whole array in Implode, As per guideline of implode function we can not pass index of array in so we have to modify array here.

you can use array slice function for that:

<?php
$word[0]="search";
$word[1]="journal";
$word[2]="information";
$word[3]="system";
echo $secondnames = implode(" ",array_slice($word,2));
?>
Ahmed Ginani
  • 6,522
  • 2
  • 15
  • 33
1

Try this:

$word[0]="search";
$word[1]="journal";
$word[2]="information";
$word[3]="system";

echo implode(array($word[2], $word[3]), " ");
Jay Patel
  • 2,341
  • 2
  • 22
  • 43
0

Please try,

echo join(' ', array_slice($word, 2, 2));

// information system

Josmy Jose
  • 36
  • 3