"Cannot use assign-op operators with string offsets" - Laravel - php

I have an application in Laravel that is taking a chunk of data from the dB and rendering it as JSON. The following line of code is generating the title error:
$decodedData['detail']['is_stem'] = isset($detailData->is_stem) ? $detailData->is_stem : 0;
The error is: Cannot use assign-op operators with string offsets
$decodedData is a larger array that is eventually returned as JSON. It is created thusly:
$decodedData = json_decode($detailData->detail, true);
$detailData is an object that looks like this:
App\CareersDetails Object
(
[connection:protected] => mysql
[table:protected] =>
[primaryKey:protected] => id
[keyType:protected] => int
[incrementing] => 1
[with:protected] => Array
(
)
[withCount:protected] => Array
(
)
[perPage:protected] => 15
[exists] => 1
[wasRecentlyCreated] =>
[attributes:protected] => Array
(
[id] => 4
[code] => 1234
[title] => StackOverFlow
[category] => My Category
[detail] => "Some details in JSON"
[is_stem] => 1
[created_at] => 2018-12-28 17:05:15
[updated_at] => 2018-12-28 17:05:15
)
[original:protected] => Array
(
[id] => 7
[code] => 7890
[title] => StackOverFlowRocks
[category] => My Category
[detail] => "Some details in JSON format"
[is_stem] => 1
[created_at] => 2018-12-28 17:05:15
[updated_at] => 2018-12-28 17:05:15
)
[changes:protected] => Array
(
)
[casts:protected] => Array
(
)
[dates:protected] => Array
(
)
[dateFormat:protected] =>
[appends:protected] => Array
(
)
[dispatchesEvents:protected] => Array
(
)
[observables:protected] => Array
(
)
[relations:protected] => Array
(
)
[touches:protected] => Array
(
)
[timestamps] => 1
[hidden:protected] => Array
(
)
[visible:protected] => Array
(
)
[fillable:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
)
When I debug using:
print_r($detailData->is_stem);
The system outputs a 1. Hence it is set.
Alternatively, is my $decodedData array at fault?
Update
Thanks to the comments, I noticed my $decodedData is not an array, it is a string.
Hence I dumped
`$detailData->detail`
To my browser page with print_r and ran it via a simple, seperate PHP script:
$payload = "JSON FROM $detailData->detail";
$data = json_decode($payload,true);
$data['detail']['is_stem'] = 1;
print_r($data );
This works. Hence my question is now why does the string dump from print_r work and my Laravel based-app doesn't?
Or, in other words, why is json_decode returning a string in the Laravel App but an Array in the PHP app with the same input?

The issue in my instance was my JSON data was double encoded. The fix was:
$decodedData = json_decode(json_decode($detailData->detail), true);
Note, this is not best practice and is a complete oversight on our part so we will be changing this ASAP.

Related

PHP: Displaying data from Bigcommerce API

I am trying to display the data from Bigcommerce through php when I run the Bigcommerce::getCategories() i getting a array of data like this:
[0] => Bigcommerce\Api\Resources\Category Object
(
[ignoreOnCreate:protected] => Array
(
[0] => id
[1] => parent_category_list
)
[ignoreOnUpdate:protected] => Array
(
[0] => id
[1] => parent_category_list
)
[fields:protected] => stdClass Object
(
[id] => 88
[parent_id] => 0
[name] => Dell
[description] =>
[sort_order] => 0
[page_title] =>
[meta_keywords] =>
[meta_description] =>
[layout_file] =>
[parent_category_list] => Array
(
[0] => 88
)
[image_file] =>
[is_visible] => 1
[search_keywords] =>
[url] => /dell/
)
[id:protected] => 88
[ignoreIfZero:protected] => Array
(
)
[fieldMap:protected] => Array
(
)
)
but when I try to pass this to JQuery so that I can display it using this statement: <?php echo json_encode($categories); ?> I am getting an array of empty objects, what is the right way to get the array of objects in Bigcommerce API? Thanks.
From the first line of your code:
[0] => Bigcommerce\Api\Resources\Category Object
You have an object, not an array. Try casting it to an array first:
$array = (array) $yourObject;

Parsing JSON Array Data

I'm trying to get data out of some JSON DATA. I'm using the following lines to decode it right now
$json_array = (array)(json_decode($response));
When I print my JSON Decoded array, I have the following data below:
I would like to get the details from the details section, where there is multiple sets of from/to_date's, and up/down numbers. Nothing I seem to do works though to get me to that data. I can print out the data from other areas like usage, but, I can't get into the details.
Array (
[resp_code] => SUCCESS
[caller_ref] => 2017092002282130006180
[server_ref] => 2017092002282169760291
[data] => stdClass Object (
[type] => monthly
[group_id] => 19
[device_id] => 32
[sn] => sn1234
[usages] => Array (
[0] => stdClass Object (
[from_date] => 2017-09-01T00:00:00
[to_date] => 2017-09-30T23:59:59
[up] => 22370
[down] => 119217
[ts] => 2017-09-01T00:00:00
)
)
[details] => stdClass Object (
[3] => Array (
[0] => stdClass Object (
[from_date] => 2017-09-01T00:00:00
[to_date] => 2017-09-30T23:59:59
[up] => 5522
[down] => 40301
[ts] => 2017-09-01T00:00:00
)
)
[2] => Array (
[0] => stdClass Object (
[from_date] => 2017-09-01T00:00:00
[to_date] => 2017-09-30T23:59:59
[up] => 6905
[down] => 32029
[ts] => 2017-09-01T00:00:00
)
)
)
)
)
Whats wrong with objects?
$obj = json_decode($response);
echo $obj->data->details[0]->from_date;
Or to loop it:
foreach ($obj->data->details as $item) {
echo $item->from_date;
// same goes for: to_date, up etc.
}
Simple and sexy!
Update:
It looks as if $item would be an array as well, so if you have problems try:
foreach ($obj->data->details as $item) {
echo $item[0]->from_date;
// same goes for: to_date, up etc.
}

Extract data fron JSON array using loop

im trying to filter a JSON array response as i only need a small part of the results.
I need to get the players displayName only.
Here is the repose for the first player, there can be upto 12 player per match.
I need something that can loop through and extract the names..
[displayName] => jonhofun
At present the only way i can get the data i need is by doing
$player1 = $json11['Response']['data']['entries']['0']['player']['destinyUserInfo']['displayName'];
$player2 = $json11['Response']['data']['entries']['1']['player']['destinyUserInfo']['displayName'];
etc... etc...
heres the original response
Array
(
[Response] => Array
(
[data] => Array
(
[period] => 2016-08-20T10:16:46Z
[activityDetails] => Array
(
[referenceId] => 3156370656
[instanceId] => 5370359303
[mode] => 12
[activityTypeHashOverride] => 3614615911
)
[entries] => Array
(
[0] => Array
(
[standing] => 0
[score] => Array
(
[basic] => Array
(
[value] => 2190
[displayValue] => 2,190
)
)
[player] => Array
(
[destinyUserInfo] => Array
(
[iconPath] => /common/destiny_content/icons/d0d3cd4c26aa1a931d46c4bf720856ba.jpg
[membershipType] => 2
[membershipId] => 4611686018454971653
[displayName] => jonhofun
)
[characterClass] => Warlock
[characterLevel] => 40
[lightLevel] => 322
)
)
)
)
)
)
Any help would be appreciated.
You need to loop through the sub array under "entries".
foreach ($json11['Response']['data']['entries'] as $entries) {
$player_names[] = $entries['player']['destinyUserInfo']['displayname'];
}
echo "<pre>";
print_r($player_names); // Check all player names

get array name from multilevel array in php

how i can get WP_Widget_Archives from array,
This is my array:
$control = Array
(
[name] => Archives
[id] => archives-6
[callback] => Array
(
[0] => WP_Widget_Archives Object
(
[id_base] => archives
[name] => Archives
[widget_options] => Array
(
[classname] => widget_archive
[description] => A monthly archive of your site’s Posts.
)
[control_options] => Array
(
[id_base] => archives
)
[number] => 8
[id] => archives-8
[updated] =>
[option_name] => widget_archives
)
[1] => form_callback
)
[params] => Array
(
[0] => Array
(
[number] => 6
)
)
[width] => 250
[height] => 200
[id_base] => archives
)
i have try with this code
`echo '<pre>'; print_r(array_keys($control['callback'])); echo '</pre>';`
but I get result like this
Array
(
[0] => 0
[1] => 1
)
where I think the result will be like this
$result = Array
(
[0] => WP_Widget_Archives Object
[1] => form_callback
)
so i can write $result[0] for get WP_Widget_Archives, please help me and thank you for your help :)
Probably you misunderstood array_key function. It will give you keys of the array not value. In your case you require value which is an object 'WP_Widget_Archives', so you can directly use $control['callback'][0].

PHP "Illegal string offset" error when looping through array

I am using the Amazon API to get some XML data, which is then passed through the json_decode() function.
Here are two samples of the data that is returned:
[BrowseNodes] => Array
(
[Request] => Array
(
[IsValid] => True
[BrowseNodeLookupRequest] => Array
(
[BrowseNodeId] => 2645269011
[ResponseGroup] => BrowseNodeInfo
)
)
[BrowseNode] => Array
(
[BrowseNodeId] => 2645269011
[Name] => Featured Categories
[Children] => Array
(
[BrowseNode] => Array
(
[0] => Array
(
[BrowseNodeId] => 3741261
[Name] => Cooktops
)
[1] => Array
(
[BrowseNodeId] => 3741271
[Name] => Dishwashers
)
[2] => Array
(
[BrowseNodeId] => 3741331
[Name] => Freezers
)
[3] => Array
(
[BrowseNodeId] => 2399939011
[Name] => Ice Makers
)
)
)
and
[BrowseNodes] => Array
(
[Request] => Array
(
[IsValid] => True
[BrowseNodeLookupRequest] => Array
(
[BrowseNodeId] => 3774781
[ResponseGroup] => BrowseNodeInfo
)
)
[BrowseNode] => Array
(
[BrowseNodeId] => 3774781
[Name] => Vitamin D
[Children] => Array
(
[BrowseNode] => Array
(
[BrowseNodeId] => 6936848011
[Name] => D3
)
)
I am then using this code to obtain data for each of the children:
if(isset($result['BrowseNodes']['BrowseNode']['Children'])){
$childs = $result['BrowseNodes']['BrowseNode']['Children']['BrowseNode'];
foreach($childs as $child){
$browsenodeid = $child['BrowseNodeId'];
$name = $child['Name'];
}
}
Everything works fine in the first sample, but in the second one, I am getting the following errors:
Warning: Illegal string offset 'BrowseNodeId'
Warning: Illegal string offset 'Name'
If I echo $browsenodeid and $name in the second example, it gives me 6 and D as the output.
Any ideas as to what I am doing wrong? I am quite confused; to me the data I am looping through in both cases seems to be the same, the only difference being that in the first case, the array has 4 elements and in the second it has 1 element.
Thanks in advance.
The reason I think is that in case 1: the structure is [BrowseNode][0][BrowseNodeId] whereas in case 2 it is : [BrowseNode][BrowseNodeId] that [0] is missing , you can fix it in the data.
Read more at: Illegal string offset Warning PHP

Categories