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);
How do I fix these illegal string offset warnings?
The usage of the function is_array()
in the beginning of your function can give you the insurance, that, if someone pass you something else than an array, the variable is reinitialised as an empty array.
Unsetting or nulling it before doing that is useless, because, as of PHP 5.3, PHP does have a garbage collector mechanism.
/**
* @params array $array
*/
function my_function( $array ) {
if ( ! is_array ( $array ) ) { $array = [] };
/**
* Or, if you don't like the short array notation:
* if ( ! is_array ( $array ) ) { $array = array(); };
*/
if ( ! isset( $array['where'] ) ) { $array['where'] = 'after'; }
if ( ! isset( $array['echo'] ) ) { $array['echo'] = false; }
if ( ! isset( $array['content'] ) ) { $array['content'] = false; }
$array['shortcode'] = true;
$array['devs'] = true;
return social_warfare( $array );
}
add_shortcode( 'my_shortcode', 'my_function' );
How do I correct this Illegal String Offset?
if ($inputs['type'] == 'attach') {
The code is valid, but it expects the function parameter $inputs
to be an array. The "Illegal string offset" warning when using $inputs['type']
means that the function is being passed a string instead of an array. (And then since a string offset is a number, 'type'
is not suitable.)
So in theory the problem lies elsewhere, with the caller of the code not providing a correct parameter.
However, this warning message is new to PHP 5.4. Old versions didn't warn if this happened. They would silently convert 'type'
to 0
, then try to get character 0 (the first character) of the string. So if this code was supposed to work, that's because abusing a string like this didn't cause any complaints on PHP 5.3 and below. (A lot of old PHP code has experienced this problem after upgrading.)
You might want to debug why the function is being given a string by examining the calling code, and find out what value it has by doing a var_dump($inputs);
in the function. But if you just want to shut the warning up to make it behave like PHP 5.3, change the line to:
if (is_array($inputs) && $inputs['type'] == 'attach') {
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'],
]);
}
Warning: Illegal string offset 'quantity' Opencart 2.1
use: $qnt = !empty((int)$data['quantity']) ? (int)$data['quantity'] : ' ';
Related Topics
Redirect to Page and Send Custom Http Headers
How to Use PHP Reserved Names for My Functions and Classes
Using Square Brackets in Hidden HTML Input Fields
Differencebetween Sessions and Cookies in PHP
How to Make Pdo Run Set Names Utf8 Each Time I Connect, in Zendframework
Separate Space-Delimited Words in a String
How to Handle Double Quotes in String Before Xpath Evaluation
How to Convert Boolean to String
Too Much Data with Var_Dump in Symfony2 Doctrine2
PHP Carbon, Get All Dates Between Date Range
Concatenating a String and Primary Key Id While Inserting
How to Show a Where Clause Just for One Field in MySQL
Swift Httppost Data Not Inserting to MySQL Database
Understanding Pdo MySQL Transactions
Why am I Getting Mime-Type of .CSV File as "Application/Octet-Stream"