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
Related
I have text file of array values
for example
[26-11-2018 04:07:26]: Array
(
[0] => api.flights_published_search
[1] => Array
(
[0] => Array
(
[ticket_type] => single
[departure_airport_id] => THR
[arrival_airport_id] => MHD
[departure_date] => 21/12/2018
[direct_flight_only] => 0
[adults] => 1
[children] => 1
[child_ages] => Array
(
[1] => 11
)
[infants] => 1
[cabin_class] => Y
)
)
)
[26-11-2018 04:07:27]: Array
(
[status] => 1
[pscId] => 0
[localInventory] => Array
(
[cal] => 0
[resp] => Array
(
)
)
[error] => Array
(
[status] =>
[error] => Error Code: 3037, Error: No availability on chosen flights.
)
)
I want to convert into as it is in array through php coding . so can anyone suggegst best way to convert this text to php array
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'].
Can you please explain how to change one array value to second value.
Please find below example.
First array
Array
(
[20239802] => one test
[20239801] => two testttttt
)
Second array
Array (
[content] => Array (
[0] => Array (
[content_pack_id] => 10002
[content_pack_name] => 100 Days Of Love-FLA
[image_path] => Array ( [0] => pack_image_10002_width. )
[content_image_path] => Array ( [imgjpeg80] => http://content.jpg )
[content_id] => 20239802
[track] => Lede Inthati Santhosham
[duration] => 0
)
[1] => Array (
[content_pack_id] => 10003
[content_pack_name] => 1001 fdfdf
[image_path] => Array ( [0] => pack_image_10002_width. )
[content_image_path] => Array ( [imgjpeg80] => http://content.jpg )
[content_id] => 20239801
[track] => Lede Inthati Santhosham
[duration] => 0
)
)
[autoshuffle_pack] => no
)
We need to replace [track] value in second array if match first array [20239802] with second array [content_id]
Need out put:-
Array
(
[content] => Array
(
[0] => Array
(
[content_pack_id] => 10002
[content_pack_name] => 100 Days Of Love-FLA
[image_path] => Array
(
[0] => pack_image_10002_width.
)
[content_image_path] => Array
(
[imgjpeg80] => http://content.jpg
)
[content_id] => 20239802
[track] => one test
[duration] => 0
)
[1] => Array
(
[content_pack_id] => 10003
[content_pack_name] => 1001 fdfdf
[image_path] => Array
(
[0] => pack_image_10002_width.
)
[content_image_path] => Array
(
[imgjpeg80] => http://content.jpg
)
[content_id] => 20239801
[track] => two testttttt
[duration] => 0
)
)
[autoshuffle_pack] => no
)
Check [track] value change in my need out put
as per depend first array.
Array
(
[20239802] => one test
[20239801] => two testttttt
)
with second array
[content_id] => 20239801
[track] => two testttttt
Is this you want to do :
foreach($second_array as $key => $second_row) {
$content_id = $second_row['content_id'];
if(isset($first_array[$content_id]) && $first_array[$content_id] != '') {
$second_array['track'] = $first_array[$content_id];
}
}
You can do you own function.
Foreach records in your first array:
Loop though the second and search for key == content_id
If it matched, set the value of array2[$index][track] to array1[key]
If not just continue
You can also use array_search() function to find the match faster, for I'll let you take a look at the PHP documentation
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..
I've an associative array called $data as follows:
Array
(
[op] => edit
[pt_id] => 4
[form_submitted] => yes
[pt_doc_title] => Array
(
[1] => Test Document
[2] => New Joining
[3] => Hallo Jolly
)
[pt_doc_id] => Array
(
[0] => 6
[1] => 7
)
[submit] => Update
)
In order to keep all the package type documents data together I've manipulated the above array as follows:
foreach ($data['pt_doc_title'] as $key => $title) {
$id = isset($data['pt_doc_id'][$key-1]) ? $data['pt_doc_id'][$key-1] : null;
$data['pt_documents_data'][] = array(
'pt_doc_title' => $title,
'pt_doc_id' => $id
);
}
unset($data['pt_doc_title'], $data['pt_doc_id']);
After manipulation I'm getting following array $data as follows:
Array
(
[op] => edit
[pt_id] => 4
[form_submitted] => yes
[submit] => Update
[pt_documents_data] => Array
(
[0] => Array
(
[pt_doc_title] => Test Document
[pt_doc_id] => 6
)
[1] => Array
(
[pt_doc_title] => New Joining
[pt_doc_id] => 7
)
[2] => Array
(
[pt_doc_title] => Hallo Jolly
[pt_doc_id] =>
)
)
)
My issue is I'm haivng another array called $_FILES as follows and I want to merge one of it's key(name) into above array in a same manner.
Array
(
[document_file_name_1] => Array
(
[name] =>
[type] =>
[tmp_name] =>
[error] => 4
[size] => 0
)
[document_file_name_2] => Array
(
[name] =>
[type] =>
[tmp_name] =>
[error] => 4
[size] => 0
)
[document_file_name_3] => Array
(
[name] => FAQ.doc
[type] => application/msword
[tmp_name] => /tmp/phpFiBYKB
[error] => 0
[size] => 35840
)
)
That is if there exists a value under [name] then the final array should be as follows. As there is a value present only in last array element of array $_FILES
Array
(
[op] => edit
[pt_id] => 4
[form_submitted] => yes
[submit] => Update
[pt_documents_data] => Array
(
[0] => Array
(
[pt_doc_title] => Test Document
[pt_doc_id] => 6
[pt_doc_file_iname] =>
)
[1] => Array
(
[pt_doc_title] => New Joining
[pt_doc_id] => 7
[pt_doc_file_iname] =>
)
[2] => Array
(
[pt_doc_title] => Hallo Jolly
[pt_doc_id] =>
[pt_doc_file_iname] => FAQ.doc
)
)
)
Can anyone please help me in creation of such final array?
Simplest way would be to walk the $_FILES array and extract the id from the last segment of the field name... then use that -1 as the basis for adding the file to your result array.
Something like this should get you there (untested):
foreach($_FILES as $k => $file){
$index_to_update = trim(substr($k, strrpos($k, "_")+1))-1;
$res["pt_document_data"][$index_to_update]["pt_doc_file_iname"] = isset($file["name"])?$file["name"]:"";
}
That is assuming $res is the array that is the parent of the pt_document_data element.