How to Fix Warning Illegal String Offset in PHP

Illegal string offset Warning PHP

Please try this way.... I have tested this code.... It works....

$memcachedConfig = array("host" => "127.0.0.1","port" => "11211");
print_r($memcachedConfig['host']);

How to fix this `Illegal string offset` error in php?

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

How to fix Warning: illegal string offset

$tagRow is a single row with 2 keys/values. $tag is a string value. You'll want to use a while loop to get each row, then you can get the values.

  $check_tag = mysql_query("SELECT tagName, tagSlug FROM tb_tags, tb_tag_posts WHERE tb_tags.tagID = tb_tag_posts.tagID AND tb_tag_posts.postID = '".$post['postID']."'") or die(mysql_error());
$tag_links = array();
while($tagRow = mysql_fetch_assoc($check_tag);){
$tag_link[] = "<a href=''>".$tagRow['tagName']."</a>";
}
echo implode(", ", $tag_link);

PHP: How to fix illegal string offset?

Your approach is OK but you've to make sure that your $value is an array and it contains the price key otherwise you'll get

Warning: Illegal string offset 'price'

To fix that you can use is_array() on $value , isset() and !empty() on $value['price']

foreach ($json as $key => $value) {
if(is_array($value) && isset($value['price']) && !empty($value['price'])){
$price = $value['price'];
echo '<br>' . $price;
}
}

Warning: Illegal string offset - php

Update efter edited question:

echo 'Printing data <br />' . PHP_EOL;
echo $chandet['status'] . '<br />' . PHP_EOL;
echo $chandet['message'] . '<br />' . PHP_EOL;


foreach( $chandet['channel']['details'] as $key => $value ) {
echo $key . ' ' . $value . '<br />' . PHP_EOL;
}

foreach( $chandet['channel']['videos'] as $video) {
foreach( $video as $key => $value ) {
echo $key . ' ' . $value . '<br />' . PHP_EOL;
}
}

Facing error in php Illegal string offset

As you can see in the fetched JSON, the actual data is contained within a data field.

So replace:

$user_data = json_decode($json_data, true);

With:

$result = json_decode($json_data, true);
$user_data = $result['data'];

How to fix simpleXML “Warning: Illegal string offset” for node name?

There are two possible ways to interpret code of this form:

$object->$foo['bar'];
  • Versions of PHP since 7.0 will look up the property identified by $foo, and then look up the key 'bar' on the result, which is what you want.
  • Older versions of PHP would evaluate $foo['bar'] first, and then look up the property based on that result.

The warning in your IDE is assuming the old interpretation: it's warning you that $foo['bar'] doesn't make sense, because $foo is a string.

Unless you're actually running your code in an ancient version of PHP (5.6 hasn't had an official security update for over 2 years), there is no actual problem with the code. You need to either upgrade your IDE, or re-configure it, so that it interprets code the same way the actual PHP will. Since you don't mention what IDE you're using, I can't be more specific.

How to resolve Illegal string offset problem in laravel

You would need to add an index to the array inputs you are creating where the key for the element is the id of the record (settings[key][attribute]). At the moment you have a single dimensional array that on the backside would end up only being 1 set of attributes for an address.

Assuming $addresses is an Eloquent Collection of models:

@foreach ($addresses as $key => $address)
<tr>
<td><input type="text" name="settings[{{ $key }}][house_no]" value="{{ $address->house_no}}" class="form-control" /></td>
<td><input type="text" name="settings[{{ $key }}][street_name]" value="{{ $address->street_name}}" class="form-control" /></td>
<td><input type="text" name="settings[{{ $key }}][area]" value="{{ $address->area}}" class="form-control" /></td>
<td><input type="text" name="settings[{{ $key }}][pincode]" value="{{ $address->pincode}}" class="form-control" /></td>
<tr>
@endforeach

Then your loop on the backside could iterate the 'sets' of settings:

foreach ($request->input('settings', []) as $key => $value) {
Address::whereKey($key)->update(
'house_no' => $value['house_no'],
'street_name' => $value['street_name'],
'area' => $value['area'],
'pincode' => $value['pincode'],
]);
}

Wordpress: Warning: Illegal string offset in multiple places

Warning: Illegal string offset 'something' message means that you're trying to access the value of $someVar['something'] but it doesn't exist.

To avoid it, you should always use isset($someVar['something']) to check if the index exists (or not) in the given variable.



Related Topics



Leave a reply



Submit