Select Different Columns as a single result - MongoDB - PHP - php

How do I select 'peerIP' and 'client_ip' as IP address. (DATA given below)
I have gone this far.
$cursor = $collection->find();
$cursor->fields(array("payload.peerIP" => true, "payload.client_ip" => true, "payload.remote_host" => true, "payload.source" => true));
foreach ($cursor as $document) {
print_r($document['payload']);
}
which gives me:
Array ( [remote_host] => 00.00.00.000 )
Array ( [peerIP] => 000.00.000.00 )
.......
Array ( [client_ip] => 00.000.00.00 )
I am trying to achieve something like this:
Array ( [peerIP] => 00.00.00.000 )
Array ( [peerIP] => 000.00.000.00 )
.......
Array ( [peerIP] => 00.000.00.00 )
Any help? Is this even possible ?
DATA:
Array (
[session] => e283f2defa254228bb20527aeb334bf7
[peerIP] => 000.000.00.00
[commands] => Array ( )
[loggedin] => [startTime] => 2015-07-29T11:45:46.685927
[ttylog] => [hostPort] => 22 [peerPort] => 52963
[version] => SSH-2.0-libssh2_1.6.0
[urls] => Array ( )
[hostIP] => 127.0.0.1
[credentials] => Array (
[0] => Array (
[0] => root [1] => root ) )
[endTime] => 2015-07-29T11:45:49.976508
[unknownCommands] => Array ( ) )
Array ( [client_ip] => 000.00.000.00
[dist] => 23 [server_port] => 21
[timestamp] => 2015/07/31 02:41:04
[client_port] => 61447
[raw_sig] => 4:41+23:0:1400:63,0:mss,ws,sok,ts,eol+0::0
[params] => none
[server_ip] => 00.00.00.00
[subject] => cli [os] => ??? [mod] => syn )
PS: The values posted above are sys logs from different nodes, the outputs have different field names..

Related

PHP - loop through messages in array

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

Parse XML to Array with SimpleXML

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

how to read values from an assosiative array using php?

This is my sample associative array:
Array (
[0] => Array (
[Name] => ALCOIN
[BasePlugin] => HTTP
[Version] => 1
[Description] => Plugin for ALCO_IN operations
[ImagePath] => ./resources
[xip] => http://www.example.org/XIP
[xsi] => http://www.w3.org/2001/XMLSchema-instance
[schemaLocation] => http://www.example.org/XIP XIP.xsd
)
[1] => Array (
[xip:Action] => Array (
[#attributes] => Array (
[Name] => OfferActivationByOfferID
[Version] => 1.0
[ImagePath] => ./resources
)
)
)
)
In that array I need to get the BasePlugin value and the name attribute value.
It seems you might need this:
$basePlugin = $yourArray[0]['BasePlugin'];
$attributes = $yourArray[1]['xip:Action']['#attributes'];
Assuming you are assigned to $yourVar like:
$yourVar = Array (
[0] => Array (
[Name] => ALCOIN
[BasePlugin] => HTTP
[Version] => 1
[Description] => Plugin for ALCO_IN operations
[ImagePath] => ./resources
[xip] => http://www.example.org/XIP
[xsi] => http://www.w3.org/2001/XMLSchema-instance
[schemaLocation] => http://www.example.org/XIP XIP.xsd
)
[1] => Array (
[xip:Action] => Array (
[#attributes] => Array (
[Name] => OfferActivationByOfferID
[Version] => 1.0
[ImagePath] => ./resources
)
)
)
)
You would use:
echo $yourVar[0]["BasePlugin"];
echo $yourVar[1]["xip:Action"]["#attributes"]["Name"];
echo $yourVar[1]["xip:Action"]["#attributes"]["Version"];
echo $yourVar[1]["xip:Action"]["#attributes"]["ImagePath"];

Get value with column names in cassandra

Hi I am using php with cassandra database. I am trying to get username and password from cassandra database. It return the below result.
Cassandra\Rows Object(
[columns:protected] => Array
(
[0] => Cassandra\ColumnSpec Object
(
[keyspace:protected] => team1_groupboxx
[tablename:protected] => gb_adminusers
[name:protected] => au_username
[type:protected] => Cassandra\TypeSpec Object
(
[type:protected] => 13
[customTypename:protected] =>
[keyType:protected] =>
[valueType:protected] =>
)
)
[1] => Cassandra\ColumnSpec Object
(
[keyspace:protected] => team1_groupboxx
[tablename:protected] => gb_adminusers
[name:protected] => au_password
[type:protected] => Cassandra\TypeSpec Object
(
[type:protected] => 13
[customTypename:protected] =>
[keyType:protected] =>
[valueType:protected] =>
)
)
)
[rowCount:protected] => 1
[columnCount:protected] => 2
[current:protected] => 0
[rows:protected] => Array
(
[0] => Array
(
[0] => k2badmin
[1] => f2c452187d4af19966187e15f1e944e1
)
)
)
But I want to get the query result in below format.
Array (
[au_username] => admin
[au_password] => f2c452187d4af19966187e15f1e944er
)
where au_username and au_password are the column names of my table.

XML to array in Cakephp then save

I'm trying to change XML to array using toArray in XML class in Cakephp framework.
Below is the array output:
[state] => Array
(
[0] => Array
(
[#code] => ACT
[post_code] => Array
(
[0] => Array
(
[#code] => 2600
[locality] => Array
(
[0] => Array
(
[#name] => DEAKIN
[dwelling_type] => Array
(
[#code] => H
[typical_value] => Array
(
[#rank] => 3341/3697
[#] => 831000
)
[dom] => Array
(
[#score] => 1
[#rank] => 454/5673
[#] => 56
)
[discount] => Array
(
[#score] => 0
[#rank] => 779/5673
[#] => 5%
)
[acr] => Array
(
[#score] => -1
[#rank] => 914/5531
[#] => 59%
)
[renters] => Array
(
[#score] => 0
[#rank] => 5131/5627
[#] => 42%
)
[vacancy] => Array
(
[#score] => 1
[#rank] => 4714/5673
[#] => 2.61%
)
[yield] => Array
(
[#score] => -1
[#rank] => 678/3697
[#] => 3.69%
)
[som] => Array
(
[#score] => 1
[#rank] => 3915/5144
[#] => 2.08%
)
[search_dsr] => Array
(
[#score] => -3
[#rank] => 3578/4009
[#] => 4.9
)
[dsr] => Array
(
[#rank] => 3121/5673
[#] => 23
)
[sr] => Array
(
[#rank] => 2552/5673
[#] => 5.8
)
)
)
[1] => Array
(
[#name] => YARRALUMLA
[dwelling_type] => Array
(
[0] => Array
(
Now the problem is how can we actually transform this so that I can get the ones I need extracted then save them?
I need #name, typical value, dom, discount, acr etc...
Is using core library class Set an option for this? If so which one would you recommend?
Try something like this, it's much easier to get hold of value:
$objXml = $yourXmlData;
$arrXml = Set::reverse($objXml);
debug($arrXml);
Hope it helps

Categories