PHP parsing of a multi-dimensional array from Elastic Search - php

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'];

Related

PHP 7.2 Cannot use string offset as an array in eval

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.

Get JSON value from PHP

All, I have got a JSON response from NEO4J:
Array
(
[columns] => Array
(
[0] => n
)
[data] => Array
(
[0] => Array
(
[0] => Array
(
[outgoing_relationships] => http://localhost:7474/db/data/node/1/relationships/out
[labels] => http://localhost:7474/db/data/node/1/labels
[data] => Array
(
[position] => Developer
[awesome] => 1
[name] => Michael
[children] => 3
)
[traverse] => http://localhost:7474/db/data/node/1/traverse/{returnType}
[all_typed_relationships] => http://localhost:7474/db/data/node/1/relationships/all/{-list|&|types}
[property] => http://localhost:7474/db/data/node/1/properties/{key}
[self] => http://localhost:7474/db/data/node/1
[properties] => http://localhost:7474/db/data/node/1/properties
[outgoing_typed_relationships] => http://localhost:7474/db/data/node/1/relationships/out/{-list|&|types}
[incoming_relationships] => http://localhost:7474/db/data/node/1/relationships/in
[extensions] => Array
(
)
[create_relationship] => http://localhost:7474/db/data/node/1/relationships
[paged_traverse] => http://localhost:7474/db/data/node/1/paged/traverse/{returnType}{?pageSize,leaseTime}
[all_relationships] => http://localhost:7474/db/data/node/1/relationships/all
[incoming_typed_relationships] => http://localhost:7474/db/data/node/1/relationships/in/{-list|&|types}
)
)
)
)
I do not know how to retrieve the value "position" under the data array, because the array is under another array.
Can you tell me how to do it with PHP?
Thx
Access them like this.
echo $yourarr['data'][0][0]['data']['position'];
Tips on how to locate :
Just locate where is the position keyword, Now look up to the array, As you can see the parent of position is data, think of how you reach from the start
(data)to the destination(position) (like a maze).
When you have more than 1 record try this
foreach($var['data'] as $inside){ //inner 1st stage
foreach($inside as $index => $main){ //inner 2nd stage
if($index == 'data'){ //check if index is data
echo $main['position']; //output position
}
}
}

How to print data from an array php?

I am having some troubles calling the correct values in the php array. Here is the array.
Array ( [count] => 1 [threads] => Array ( [13] => Array ( [thread_id] => 13 [node_id] => 4 [title] => Forum Integration nearly complete! [reply_count] => 0 [view_count] => 0 [user_id] => 59 [username] => Faeron [post_date] => 1369257302 [sticky] => 0 [discussion_state] => visible [discussion_open] => 1 [discussion_type] => [first_post_id] => 23 [first_post_likes] => 0 [last_post_date] => 1369257302 [last_post_id] => 23 [last_post_user_id] => 59 [last_post_username] => Faeron [prefix_id] => 1 [content] => Array ( [count] => 1 [content] => Array ( [23] => Array ( [post_id] => 23 [thread_id] => 13 [user_id] => 59 [username] => Faeron [post_date] => 1369257302 [message] => It's been quite a while since we began to integrate the phanime Forums with the main site. We have now finished the integration with the phanime Forums and the main site. You will no longer notice that there are two platforms running phanime, but instead only one. Our next step is to theme the forums to make it look like the main site! [ip_id] => 268 [message_state] => visible [attach_count] => 0 [position] => 0 [likes] => 0 [like_users] => a:0:{} [warning_id] => 0 [warning_message] => ) ) ) ) ) )
Now lets say this array was named $array then to get the first element's value "[count]" can't I just say the following: print $array["[count]"] <-- this returns an error.
What about the element that has a value as an array itself, which is the [threads] element. How do I get, perhaps the [thread_id] element's value?
Use it like this:
echo $array['count']; // would output '1'
echo $array['threads'][13]['thread_id']; // outputs '13'
echo $array['threads'][13]['content']['content'][23]['message']; // "It's been.."
Here is the (brief) documentation on multidimensional arrays: http://php.net/manual/en/language.types.array.php#language.types.array.syntax.accessing
Here's a good guide to them with examples: http://www.developerdrive.com/2012/01/php-arrays-array-functions-and-multidimensional-arrays/
Update: To get the value of 'message' without knowing the numbered array keys beforehand you could use:
reset($array);
$first = array_keys($array['threads']);
$first = $first[0];
$second = array_keys($array['threads'][$first]['content']['content']);
$second = $second[0];
echo $array['threads'][$first]['content']['content'][$second]['message'];
You can access a value in an array by using the following:
echo $array['count'];
You can also print the entire array like so:
print_r($array);
or
var_dump($array);
If you want to write a value from a multidimensional array then use the following:
echo $array[23]['post_id'];
So, to summarize, see the following:
$array = array(
'bar' => 'testing value',
'foo' => array(
'bar' => 'test'
)
);
print_r($array) // Will print whole array
echo $array['bar']; // Will print 'testing value'
print_r($array['foo']); // Will print the second level array
echo $array['foo']['bar']; // Will print 'test'

Issue getting info out of array

I'm trying to get info out of this information:
Array (
[result] => success
[totalresults] => 1
[startnumber] => 0
[numreturned] => 1
[tickets] => Array (
[ticket] => Array (
[0] => Array (
[id] => 7
[tid] => 782755
[deptid] => 1
[userid] => 39
[name] => Mark Lønquist
[email] => mark.loenquist#outlook.com
[cc] =>
[c] => 79rzVBeJ
[date] => 2013-04-25 16:14:24
[subject] => test
[status] => Open
[priority] => Medium
[admin] =>
[attachment] =>
[lastreply] => 2013-04-25 16:14:24
[flag] => 0
[service] =>
)
)
)
)
The results are printed using:
print_r($results);
Usually, I've been able to do a simple:
$var = $results['something'];
To get it out, but it wont work with this :( Any help is appreciated.
After reformatting the array you pasted, it becomes clear that some elements are nested several levels deep. (It's a "multidimensional array"; see example #6 in the docs.) In those cases, you have to add additional brackets containing each successive key to reach the depth you want. For example, a sample from your $results array:
Array (
[result] => success
[totalresults] => 1
...
[tickets] => Array (
[ticket] => Array (
[0] => Array (
[id] => 7
[tid] => 782755
...
)
)
)
)
You simply need to do $results['totalresults'] to access "totalresults", but to get "tid" you would need to use $results['tickets']['ticket'][0]['tid'].
If you want to get "tid" from all of the tickets when there are multiple, you will have to iterate (loop) over the array of tickets. Probably something like this (untested, but should be close enough for you to figure out):
foreach ($results['tickets']['ticket'] as $ticket) {
echo $ticket['tid'];
}
To see what the problem is with your print_r() you may add error_reporting(E_ALL); to the top of your code.
Note that if you want to retrieve the value for a key such as 'totalresults' then $results['totalresults'] would be sufficient.
However, if you want to get a key from one of the nested arrays such as email then you would have to use $results['result']['tickets']['ticket'][0]['email'].

How to echo a specific value from an array in php using REST API , need to echo name from the given array

Array
(
[root] => Array
(
[status] => Array
(
[statusCode] => 200
[message] => OK
)
[response] => Array
(
[success] => 1
[result] => Array
(
[0] => Array
(
[id] => 1
[content] => 12
[enabled] => 1
[date_modified] => 2013-03-07 19:28:19
[name] => Home page
[category_id] => 1
)
)
[error_messages] =>
[debug] =>
Please help me to echo the name from the given array. i have tried many things but not able to find correct , please help me on it.
Chances are you're going to get more than one name, so:
foreach($yourArrayRef['root']['response']['result'] as $result) {
echo $result['name'];
}
Try this:
$arr['response']['result'][0]['name']
Try this
echo $response["result"][0]["name"];

Categories