user3496013
user3496013

Reputation: 23

how can i get the first and last word of element in array php

i was trying to get the first and last word of element in array, i've tried this code but it didn't get the proper result

    foreach(unserialize($q['qlite']) as $qkey => ql)
    {
         foreach($ql as zp)
         {
              $response = $zp[0];
              echo '$response';
         }
    }

and this is the result i got array( [0] => michael jordan is the best player of all time [23] => 23 )

what i want is to get the first value "michael" and the last value "time"

Upvotes: 0

Views: 356

Answers (1)

Cjmarkham
Cjmarkham

Reputation: 9681

foreach($ql as $zp)
{
   $response = $zp[0];
   $words = explode(' ', $zp[0]);

   echo $words[0];
   echo $words[count($words) - 1];
}

As explained in a comment, you need to split the string to get each word and then return the first and last of the array created by the split.

Upvotes: 3

Related Questions