The code was working in php 7.0 . I am getting error after moved to 7.2 version .
I need help in understand this .
I have an $map and $tempArray
print_r value of $locMap is given below
Array
(
[1] => Array
(
[ID] => 1
[LEVEL] => 1
[NAME] => Operations1
[CLASS] => ["e2"]["00"]
)
[2] => Array
(
[ID] => 2
[LEVEL] => 1
[NAME] => POperations2
[CLASS] => ["e2"]["01"]
)
[26] => Array
(
[ID] => 26
[LEVEL] => 2
[NAME] => Operations3
[CLASS] => ["e2"]["01"]["e2"]["00"]
)
)
print_r value of $tempArray is as below
Array
(
[account] => Array
(
[0] => Array
(
[id] => 1000
[name] => Homes
[level] =>
[rno] => 9
)
)
)
Looping this $map as below
foreach ($map $rec=>$det) {
$dummyArray = array();
$dummyArray['id'] = $det['ID'];
$dummyArray['CLASS'] = $det['CLASS'];
eval("\$tempArray['account'][0]".$det["CLASS"]." = \$dummyArray;");
}
Error: Cannot use string offset as an array in *************: eval()'d code:1
Inside eval function, "\$tempArray['account'][0]" is a string and is concatenating with the string again.
Why do I get the error in this case?
Help me understand this and how do I fix this issue ?
I believe the problem comes your concatenation of "\$tempArray['account'][0]".$det["CLASS"]." = \$dummyArray;". If you output the string, you can see what is evaluated. Most probably, you forgot [] around $det["CLASS"]. In your case, the first part might be a string, not an array.
By the way, I am not sure to understand what you want to do but I am not sure you use the easiest way.
I do not understand why it worked with php7.0.
Related
I have this multi-dimensionnal array called $response (I only get one result but there are many in reality..)
Array
(
[took] => 17
[timed_out] =>
[_shards] => Array
(
[total] => 5
[successful] => 5
[skipped] => 0
[failed] => 0
)
[hits] => Array
(
[total] => 16
[max_score] => 13.034995
[hits] => Array
(
[0] => Array
(
[_index] => zh3
[_type] => zone_humide
[_id] => 4950467
[_score] => 13.034995
[_source] => Array
(
[surfcal_ha] => 2.26
[#timestamp] => 2019-01-21T19:25:48.826Z
[#version] => 1
[donnee_id] => 3355
[zhu_cbi] => 82
[zh_nom] => Coupigny
[gid] => 4950467
[zh_part_id] => 079FONTEN0011
[is_pe] =>
)
)
)
)
)
I want to get the gid attribute. To do that, I tried :
foreach ($response['hits']['hits'][0]['_source'] as $result) {
echo $result['gid'];
}
But I get that PHP Warning :
PHP Warning: Illegal string offset 'gid'
I tried different things.. add another foreach loop, change the echo but nothing works. I tried that coming from StackOverflow but no results too. Well I think I'm really stuck.
Any ideas on that problem ?
Any help would be very appreciated !
Thanks
$response array has only one parent element. Why do you use foreach?
Just try
echo $response['hits']['hits'][0]['_source']['gid'];
it is because you are using foreach on $response['hits']['hits'][0]['_source']
eg. 1.in first loop of foreach it will get$result['surfcal_ha']
2. in second loop it is getting $result[#timestamp]
so till it doesnt get $result['gid'] it will print warning.Try using below without foreach
echo $response['hits']['hits'][0]['_source']['gid'];
In my php query I got this output:
{"projects":[{"id":127,"name":"efrat","status":{"id":10,"name":"development","label":"development"},"description":"","enabled":true,"view_state":{"id":10,"name":"public","label":"public"},"access_level":{"id":90,"name":"administrator","label":"administrator"},"custom_fields":[{"id":1,"name":"Customer email","type":"email","default_value":"","possible_values":"","valid_regexp":"","length_min":0,"length_max":50,"access_level_r":{"id":10,"name":"viewer","label":"viewer"},"access_level_rw":{"id":10,"name":"viewer","label":"viewer"},"display_report":true,"display_update":true,"display_resolved":true,"display_closed":true,"require_report":false,"require_update":false,"require_resolved":false,"require_closed":false}],"versions":[],"categories":[{"id":93,"name":"Monitor","project":{"id":0,"name":null}},{"id":31,"name":"Proactive","project":{"id":0,"name":null}},{"id":30,"name":"Project","project":{"id":0,"name":null}},{"id":29,"name":"Support","project":{"id":0,"name":null}}]}]}
after using 'json_decode' method on it, I get this:
"(
[projects] => Array
(
[0] => Array
(
[id] => 127
[name] => myprojectname
[status] => Array
(
[id] => 10
[name] => development
[label] => development
)
[description] =>
[enabled] => 1
[view_state] => Array
(
[id] => 10
[name] => public
[label] => public
)
[access_level] => Array
(
[id] => 90
[name] => administrator
[label] => administrator
)
[custom_fields] => Array
(
[0] => Array
(
[id] => 1
[name] => Customer email
[type] => email
[default_value] =>
[possible_values] =>
[valid_regexp] =>
[length_min] => 0
[length_max] => 50
[access_level_r] => Array
(
[id] => 10
[name] => viewer
[label] => viewer
)
[access_level_rw] => Array
(
[id] => 10
[name] => viewer
[label] => viewer
)
[display_report] => 1
[display_update] => 1
[display_resolved] => 1
[display_closed] => 1
[require_report] =>
[require_update] =>
[require_resolved] =>
[require_closed] =>
)
)
[versions] => Array
(
)
[categories] => Array
(
[0] => Array
(
[id] => 93
[name] => Monitor
[project] => Array
(
[id] => 0
[name] =>
)
)
[1] => Array
(
[id] => 31
[name] => Proactive
[project] => Array
(
[id] => 0
[name] =>
)
)
[2] => Array
(
[id] => 30
[name] => Project
[project] => Array
(
[id] => 0
[name] =>
)
)
[3] => Array
(
[id] => 29
[name] => Support
[project] => Array
(
[id] => 0
[name] =>
)
)
)
)
)
)"
In my PHP, how can I release the "name" object value (the result should be 'myprojectname') from this array? I've tried many foreach loops that got me nowhere.
Thank you,
It looks like you have one object, that when decoded actually only has one array item. So, in your case, ‘myprojectname’ may simply be “$projects[0][‘name’]”
If many array items, you could
foreach ($projects as $project) {
echo $project[‘name’];
}
EDIT: I took object provided and json_decoded it myself, it doesn't match the json_decoded item presented by OP -- the first image shows the code to var_dump 'name' OP desired, part of the code also below:
$decoded = json_decode($obj);
$projects = $decoded->projects;
$name = $projects[0]->name;
Your 'projects' contains an array ("projects":[{"id":127, ... }]). I assume that the 'projects'-array might contain multiple 'project'-objects like this?
{
"projects":
[
{
"id":127,
"name":"my-project"
},
{
"id":128,
"name":"my-other-project"
}
]
}
In that case you need the arrow notation to access the name property, for example:
foreach ($projects as $project_object) {
foreach ($project_object as $project) {
echo $project->name . '<br/>';
}
}
EDIT:
I took a minimal code example of the OP and got the expected result:
Can you add more details in your code snippets in your original question or provide us with a working example of your code?
There are some online PHP sandboxes that can help you with this. For example: I stripped out all code that does not seem related to your question and got the result you are looking for in two different ways:
http://sandbox.onlinephpfunctions.com/code/009c53671fd9545e4fcecfe4b0328974381ee2ce
It is also a good idea to sum up all the foreach loops that you already tried, so we can see if you were nearly there with your own solution. This way we can understand your question better and it prevents us from offering solutions that you already used.
When I have an array like this :
Array (
[0] => Array ( [0] => 1 [1] => 12 [2] => Essener [3] => 1 )
[1] => Array ( [0] => 2 [1] => 12 [2] => Dinkel Spezial [3] => 0.2 )
[2] => Array ( [0] => 1 [1] => 1 [2] => Essener [3] => 1 )
)
and I use json_encode and echo it, I get this:
[["1","12","Essener","1"],["2","12","Dinkel Spezial","0.2"],["1","1","Essener","1"]]
which is good for me.
Now I have an array with stdClass Objects, which I wasn't able to transform into JSON with json_encode. When I echo it, it just doesn't show anything.
Then I transformed this array with objects to an array like this (with get_object_vars()):
Array (
[0] => Array (
[item_id] => 1
[item_name] => Essener
)
[1] => Array (
[item_id] => 2
[item_name] => Dinkel Spezial
)
[2] => Array (
[item_id] => 3
[item_name] => Saatenbrot
)
)
and when I use json_encode and echo it still doesn't show anything. Can anyone tell me what I am doing wrong or what I need to do to get a JSON array?
I need this json array to send data to an IOS App.
From the documentation:
Note:
In the event of a failure to encode, json_last_error() can be used to determine the exact nature of the error.
So you can try to detect the exact error by yourself. From your information I can't see any error.
Furthermore I don't think, it doesn't return anything. Try to var_dump() the result of json_encode(). I assume it returns false, which means an error occured.
So if anybody wondered what was wrong,
the problem was that i had "ä,ü,ö,ß" in my array and i needed to convert these to unicode and then everything worked just fine.
I'm struggling with extracting values from an array. print_r($offers); output is as follow:
Array
(
[0] => Object
(
[id] => 41302512
[amount] => 244
[price] => 10.17
[sellerId] => 1678289
[sellerName] => stan_J23
)
[1] => Object
(
[id] => 41297403
[amount] => 51
[price] => 10.18
[sellerId] => 2510426
[sellerName] => kszonek1380
)
[2] => Object
(
[id] => 41297337
[amount] => 581
[price] => 10.18
[sellerId] => 2863620
[sellerName] => NYski
)
)
However, echo $offers[0]['id']; doesn't work. I need to extract the values for each array node to variables i.e. $id=value_of_key_id and so on. The array has 10 nodes from 0 to 9.
Try echo $offer[0]->{'id'}.
It says it's an object, and you need to get the 'id' key in that way with objects.
See Docs: http://www.php.net/manual/en/language.types.object.php
$offers is an array of objects, not array. You will need to access the elements via $offers[0]->id; and so on
Thanks to all, working code is:
foreach ($offers as $offer) {
$id = $offer->id;
$amount = $offer->amount;
$price = $offer->price;
$sellerId = $offer->sellerId;
$sellerName = $offer->sellerName;
echo "$id<br />$amount<br />$price<br />$sellerId<br />$sellerName<br /><hr /><br />";
}
I have the following stdObject obtained thru cURL / json_decode():
stdClass Object
(
[response] => stdClass Object
(
[status] => OK
[num_elements] => 1030
[start_element] => 0
[results] => stdClass Object
(
[publisher] => stdClass Object
(
[num_elements] => 1030
[results] => Array
(
[0] => stdClass Object
(
[id] => 1234
[weight] => 4444
[name] => Pub 1
[member_id] => 1
[state] => active
[code] =>
)
[1] => stdClass Object
(
[id] => 1235
[weight] => 4444
[name] => Pub 2
[member_id] => 2
[state] => active
[code] =>
)
)
)
)
[dbg_info] => stdClass Object
(
[instance] => instance1.server.com
[slave_hit] => 1
[db] => db1.server.com
[reads] => 3
[read_limit] => 100
[read_limit_seconds] => 60
[writes] => 0
[write_limit] => 60
[write_limit_seconds] => 60
[awesomesauce_cache_used] =>
[count_cache_used] =>
[warnings] => Array
(
)
[time] => 70.440053939819
[start_microtime] => 1380833763.4083
[version] => 1.14
[slave_lag] => 0
[member_last_modified_age] => 2083072
)
)
)
I'm looping through it in order to obtain each result's ID:
foreach ($result->response->results->publisher->results as $object) {
$publishers .= $object->id.",";
}
And although the code is working fine, PHP is rising the following notices/warnings:
PHP Notice: Trying to get property of non-object in /var/www/vhosts/domain.net/script.php on line 1
PHP Notice: Trying to get property of non-object in /var/www/vhosts/domain.net/script.php on line 1
PHP Warning: Invalid argument supplied for foreach() in /var/www/vhosts/domain.net/script.php on line 1
Any ideas? Thanks in advance!
Is it possible you have set the second parameter $assoc in json_decode() to true? Like this:
$result = json_decode($json_from_curl, true);
Thereby, getting back $result with all stdClass Objects converted to associative array.
EDIT:
If the $result you are getting is indeed an associative array, then we should treat it as such. Try replacing your foreach with this:
foreach ($result['response']['results']['publisher']['results'] as $arr) {
$publishers .= $arr['id'] . ",";
}
EDIT2:
From my own testing based on your code, everything should be working correctly. The notices/warnings should not occur. Perhaps some other code not shown in your question is causing it.