arrays - PHP 'Illegal string offset' in foreach loop -


i getting assoc array mysql database using pdo.

i want perform function on trim down number of words using following code:

$newscontent = words::truncatewords($rows); 

i getting error , the function hasn't worked

warning: illegal string offset 'content' in c:\www\mvc\libs\words.php on line 14

notice: uninitialized string offset: 0 in c:\www\mvc\libs\words.php on line 14

warning: illegal string offset 'content' in c:\www\mvc\libs\words.php on line 14

the first error repeated 8 times. line 14 points line

$rows[$key]['content'] = self::trunc($row['content'], 60); 

here words class

class words {      // truncate each of news item's content set number of words     public static function truncatewords($rows) {          // loop through array          foreach($rows $key => $row) {             // , truncate content 60 words             $rows[$key]['content'] = self::trunc($row['content'], 60);         }          return $rows;     }      public function trunc($phrase, $max_words)     {         $phrase_array = explode(' ',$phrase);          if(count($phrase_array) > $max_words && $max_words > 0)             $phrase = implode(' ',array_slice($phrase_array, 0, $max_words)).'...';          return $phrase;     } } 

this because content not subscript of $row check first , see if is.

array_key_exists checks if variable set, not check variable not null

if(array_key_exists('content', $row) {    self::trunc($row['content'], 60); } 

to check subscript exists , not null, use isset


Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -