I have weird problem with my PHP code. I decoded an elasticsearch json, and I got this array:
Array ( [took] => 1 [timed_out] => [_shards] => Array ( [total] => 6 [successful] => 6 [skipped] => 0 [failed] => 0 ) [hits] => Array ( [total] => 7336 [max_score] => 0.8790647 [hits] => Array ( [0] => Array ( [_index] => logstash-2019.02.25 [_type] => doc [_id] => 5T8GJWkBAZbF3w3t4NF2 [_score] => 0.8790647 [_source] => Array ( [#version] => 1 [log-level] => ERROR [port] => 50906 [host] => 6b14cd1f183d.mynetwork [#timestamp] => 2019-02-25T14:20:01.367Z [Timestamp] => 2019-02-25T13:57:40+0000 [message] => Array ( [0] => 2019-02-25T13:57:40+0000 ERROR something happened in this execution. [1] => something happened in this execution. ) ) ) [1] => Array ( [_index] => logstash-2019.02.25 [_type] => doc [_id] => 7z8GJWkBAZbF3w3t4NF2 [_score] => 0.8790647 [_source] => Array ( [#version] => 1 [log-level] => INFO [port] => 50906 [host] => 6b14cd1f183d.mynetwork [#timestamp] => 2019-02-25T14:20:01.369Z [Timestamp] => 2019-02-25T14:00:13+0000 [message] => Array ( [0] => 2019-02-25T14:00:13+0000 INFO takes the value and converts it to string. [1] => takes the value and converts it to string. ) ) ) ) ) )
What I want is only to print the "messages".
If I run this code:
$echo json_decoded['hits']['hits']['0']['_source']['message']['0']
It echo "2019-02-25T13:57:40+0000 ERROR something happened in this execution."
And if I run this code:
$echo json_decoded['hits']['hits']['1']['_source']['message']['0']
It echo "2019-02-25T14:00:13+0000 INFO takes the value and converts it to string."
ALL GOOD SO FAR, but now I want to run it as loop, to print the messages. I made the following loop:
foreach($json_decoded['hits']['hits'] as $host) {
echo $host[$index]['_source']['message']['0'];
$index++;
}
I don't get any output. What is the problem?
When you foreach over ['hits']['hits'] this will iterate over the next level of the array (so the ['0'] and ['1'] elements). So you don't need to add in the [$index] part of your echo or the $index value...
foreach($json_decoded['hits']['hits'] as $host) {
echo $host['_source']['message']['0'];
}
Related
I have the following array:
Array (
[result] => Array (
[id] => 58fba3ebf4
[type] => A
[name] => ser.domain.com
[content] => 192.168.100.1
[proxiable] =>
[proxied] =>
[ttl] => 1
[priority] => 10
[locked] =>
[zone_id] => eb0d86828e3ac837c
[zone_name] => domain.com
[modified_on] => 2018-07-06T06:37:14.069598Z
[created_on] => 2018-07-06T06:37:14.069598Z
[meta] => Array (
[auto_added] =>
[managed_by_apps] =>
[managed_by_argo_tunnel] =>
)
)
[success] => 1
[errors] => Array ( )
[messages] => Array ( )
)
How can I just get the value from id?
$data =json_decode($response);
$id = $data->result->id;
Assuming your payload from the request was $response.
You already have a multi-dimensional array so just try like this
echo $arrays['result']['id']; // this will return id
echo $arrays['result']['type']; // this will return type
Here is solution
// $result_array() is your reponse from curl PHP
$data = $result_array();
$id = $data['result']['id'];
I've that xml structure retrieving from device
<packet>
<info action="fiscalmemory" fiscalmemorysize="1048576" recordsize="464" fiscal="1" uniqueno="ABC12345678" nip="123-456-78-90" maxrecordscount="2144" recordscount="7" maxreportscount="1830" reportscount="4" resetmaxcount="200" resetcount="0" taxratesprglimit="30" taxratesprg="1" currencychangeprglimit="4" currencychangeprg="0" fiscalstartdate="dd-mm-yyyy hh:dd:ss" fiscalstopdate="dd-mm-yyyy hh:dd:ss" currencyname="PLN" />
<ptu name="A" bres="Nobi">123.23</ptu>
<ptu name="B">123.23</ptu>
<ptu name="D">8</ptu>
<sale>999.23</sale>
</packet>
simpleXml does't see ptu's attributes
$array = simplexml_load_string($xml);
print_r($array);
It prints
SimpleXMLElement Object
(
[info] => SimpleXMLElement Object
(
[#attributes] => Array
(
[action] => fiscalmemory
[fiscalmemorysize] => 1048576
[recordsize] => 464
[fiscal] => 1
[uniqueno] => ABC12345678
[nip] => 123-456-78-90
[maxrecordscount] => 2144
[recordscount] => 7
[maxreportscount] => 1830
[reportscount] => 4
[resetmaxcount] => 200
[resetcount] => 0
[taxratesprglimit] => 30
[taxratesprg] => 1
[currencychangeprglimit] => 4
[currencychangeprg] => 0
[fiscalstartdate] => dd-mm-yyyy hh:dd:ss
[fiscalstopdate] => dd-mm-yyyy hh:dd:ss
[currencyname] => PLN
)
)
[ptu] => Array
(
[0] => 123.23
[1] => 123.23
[2] => 8
)
[sale] => 999.23
)
As we can see ptu doesn't contain attributes :/
I also tried parse it with recursive function because children also may contain chilren but without success :/
Could anybody point to me why SimpleXML doesn't take ptu's attributes and also share any parsing function?
Thanks in advance.
edited
As regards Nigel Ren I made that function
function parseXMLtoArray($xml){
$x = simplexml_load_string($xml);
$result = [];
function parse($xml, &$res){
$res['name'] = $xml->getName();
$res['value'] = $xml->__toString();
foreach ($xml->attributes() as $k => $v){
$res['attr'][$k] = $v->__toString();
}
foreach($xml->children() as $child){
parse($child, $res['children'][]);
}
}
parse($x, $result);
return $result;
}
$resArray = parseXMLtoArray($rawXml);
print_r($resArray);
this returns such array
Array
(
[name] => packet
[value] =>
[attr] => Array
(
[crc] => BKJFKHKD54
)
[children] => Array
(
[0] => Array
(
[name] => info
[value] =>
[attr] => Array
(
[action] => fiscalmemory
[fiscalmemorysize] => 1048576
[recordsize] => 464
[fiscal] => 1
[uniqueno] => ABC12345678
[nip] => 123-456-78-90
[maxrecordscount] => 2144
[recordscount] => 7
[maxreportscount] => 1830
[reportscount] => 4
[resetmaxcount] => 200
[resetcount] => 0
[taxratesprglimit] => 30
[taxratesprg] => 1
[currencychangeprglimit] => 4
[currencychangeprg] => 0
[fiscalstartdate] => dd-mm-yyyy hh:dd:ss
[fiscalstopdate] => dd-mm-yyyy hh:dd:ss
[currencyname] => PLN
)
)
[1] => Array
(
[name] => ptu
[value] => 123.23
[attr] => Array
(
[name] => A
)
)
[2] => Array
(
[name] => ptu
[value] => 123.23
[attr] => Array
(
[name] => B
)
)
[3] => Array
(
[name] => ptu
[value] => 8
[attr] => Array
(
[name] => D
)
)
[4] => Array
(
[name] => sale
[value] => 999.23
)
)
)
Is this correct?
Thanks again Nigel
When loading with SimpleXML, using print_r() gives only an idea of the content and doesn't have the full content. If you want to see the full content then use ->asXML()...
$array = simplexml_load_string($xml);
echo $array->asXML();
To check the attributes of <ptu> element, (this just gives the first one)...
echo $array->ptu[0]['name'];
This uses ->ptu to get the <ptu> element and takes the first one [0] and then takes the name attribute using ['name'].
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.
}
i`ve an array with more arrays inside.
But how can i get a specific value from it ?
For example how can i output the first name "John" ?
I tried this but it doesn't work.
foreach($arr as $result) {
echo $result['CLIENTS']['FIRSTNAME'];
}
This is the < pre> output of the array
Array
(
[WHMCSAPI] => Array
(
[ACTION] => getclients
[RESULT] => success
[TOTALRESULTS] => 12
[STARTNUMBER] => 0
[NUMRETURNED] => 12
[CLIENTS] => Array
(
[CLIENT] => Array
(
[ID] => 14
[FIRSTNAME] => John
[LASTNAME] => Doe
[COMPANYNAME] => Muster Company
[EMAIL] => info#mustermann.de
[DATECREATED] => 2014-04-13
[GROUPID] => 0
[STATUS] => Active
)
)
)
)
The other answers & comments are almost right, but they didn't notice that you're iterating over the array.
Here's your code:
foreach($arr as $result) {
echo $result['CLIENTS']['FIRSTNAME'];
}
Inside the loop (this is important), $result is equal to:
Array
(
[ACTION] => getclients
[RESULT] => success
[TOTALRESULTS] => 12
[STARTNUMBER] => 0
[NUMRETURNED] => 12
[CLIENTS] => Array
(
[CLIENT] => Array
(
...
So to access the client name inside the loop, use this:
$firstname = $result["CLIENTS"]["CLIENT"]["FIRSTNAME"];
You're missing a couple levels of the array. Try $result['WHMCSAPI']['CLIENTS']['CLIENT']['FIRSTNAME'].
I have the following array structure, this is the array passed in to $response
Array
(
[inbox] => Array
(
[0] => Array
(
[location] => 3
[ID] => 8ba84195fe79a89af1a4b5bd8c280621
[smsc_number] => +44******
[sent] => 2013-02-25 14:57:20
[coding] => Default GSM alphabet (no compression)
[remote_number] => +447****
[status] => Read
[body] => Yeppp
)
)
[outbox] => Array
(
[0] => Array
(
[location] => 2
[ID] => d22c4368fadd64e98fab64acb6b8fa34
[reference_number] => 1
[class] => 1
[coding] => Default GSM alphabet (no compression)
[remote_number] => *****
[status] => Sent
[body] => Test
)
[1] => Array
(
[location] => 0
[ID] => f0c05e8dd2578d16d73bf5dbcf2ec3e6
[class] => 1
[coding] => Default GSM alphabet (no compression)
[remote_number] => 0****
[status] => UnSent
[body] => fdgg ddfgfdg fd
)
[2] => Array
(
[location] => 1
[ID] => d7537acb1b3994ecc11369bac46c4bb6
[class] => 1
[coding] => Default GSM alphabet (no compression)
[remote_number] => 0****3
[status] => UnSent
[body] => fdgg ddfgfdg fd
)
)
)
I'm only interested in the body of the inbox array. I thought I could just do two loops to get this, or just do $response[0] but it doesn't seem to work. Heres what I have:
$response = $sms->Get();
foreach ($response[0] as $value) {
foreach ($response as $value1) {
echo($value1['body']);
}
}
I'm obviously doing something very stupid - Any help would be really appreciated
Try this
foreach ($response['inbox'] as $inb) {
echo($inb['body']); }