1

I have a multidimensional array as below:

$rows[] = $row;

Now I want to create variable from looping this array. This is how I tried it:

foreach ($rows as $k => $value) {
  //echo '<pre>',print_r($value).'</pre>';
  $id    = $value['news_id'];
  $title = $value['news_title'];
  echo $title; 
}

But it produce an error as below:

...... Illegal string offset 'news_id'

This is the output of - echo '<pre>',print_r($value).'</pre>';

Array
(
    [news_id] => 1110
    [news_title] => test
    [news] => test des
)
1

Array
(
    [news_id] => 1109
    [news_title] => ශ්‍රී ලංකාවේ ප්‍රථම....
    [news] => දහසක් බාධක....
)
1

Can anybody tell me what is the wrong I have done?

UPDATE output for echo '<pre>',print_r($rows).'</pre>';

Array
(
  [0] => 
  [1] => Array
      (
          [news_id] => 1110
          [news_title] => test
          [news] => test des
      )

  [2] => Array
      (
          [news_id] => 1109
          [news_title] => ශ්‍රී ලංකාවේ ප්‍රථම....
          [news] => දහසක් බාධක....        
      )

)
1
user3733831
  • 2,886
  • 9
  • 36
  • 68
  • 1
    @BilalAhmed we can use that comma for it. – user3733831 Sep 24 '18 at 11:23
  • @BilalAhmed Does not matter at all. – Petr Nagy Sep 24 '18 at 11:24
  • Please add this to your code and put the result here: `echo "
    " . print_r($rows) . "
    ";` Because your syntax (`array['index']`) works for me. I would like to see how $rows is created.
    – Nic3500 Sep 24 '18 at 11:29
  • @Nic3500 I updated question – user3733831 Sep 24 '18 at 11:33
  • 1
    Ok thanks. So `$rows[0]['news_id']` does not exist. Add a check `if (isset($value['news_id'])) { ... }` – Nic3500 Sep 24 '18 at 11:34
  • Your problem is that array element 0 doesn't contain a record in the format you're expecting. Either make sure the array is only populated with records in the expected format, or add some kind of check into your loop (eg ```if (empty($value)) {continue;}``` – GordonM Sep 24 '18 at 11:36
  • 1
    Possible duplicate of [Illegal string offset Warning PHP](https://stackoverflow.com/questions/9869150/illegal-string-offset-warning-php) – Matt Raines Sep 24 '18 at 11:47
  • Weird. I just tried with both PHP 5.6.30 and 7.2.10. I setup a test with your `$rows` and your `foreach` code, no modifications. Well I got no errors. For `$rows[0]` it just prints nothing, and moves on. No errors, no logs, nothing ... – Nic3500 Sep 24 '18 at 12:19

1 Answers1

2

use isset function because your 0 index is empty in $row

foreach ($rows as $k => $value) {
  if(isset($value['news_id'])){
    $id    = $value['news_id'];
    $title = $value['news_title'];
    echo $title; 
  }

}

you should add check (condition) when you assign data to $rows

Bilal Ahmed
  • 4,005
  • 3
  • 22
  • 42
  • The error `Illegal string offset 'news_id'` tells that $value is a string and `'news_id'` is an illegal index, this *may* suppress error but not explaining what is wrong or how to fix it. – Cemal Sep 24 '18 at 11:50
  • 0th index doesn't need to be empty, simply being a string instead of an array would suffice. Let `$rows = array('key' => array('news_id' => 'foo', 'news_title' => 'bar'), 'key1' => array('news_id' => 'foo', 'news_title' => 'bar'), 'key2' => array('news_id' => 'foo', 'news_title' => 'bar'), 'key3' => 'I am a simple string') `, then trraversing this array with a foreach loop and checking `$value['news_title']` will simply fail with `Illegal string offset 'news_id'` . – Cemal Sep 24 '18 at 12:05