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
Related
I am new to PHP and Arrays, I am trying to get the values from an array. But no matter how I'm trying to do it, I can't get the value. What am I doing wrong?
The Array:
Array ( [playerinfo] => Array ( [rank] => Godfather [cash] => € 8,520,530 [weapon] => M-16 (29000) [health] => Array ( [width] => 100 [color] => green ) [wealth] => Too rich to be true [protection] => Bulletproof Humvee [plf] => Huge [plane] => Concorde [crew] => None [pbf] => Large [ship] => None ) [character] => Array ( [crime] => Array ( [0] => 120 [1] => 69 ) [gta] => Array ( [0] => 400 [1] => 70 ) [drugs] => Array ( [0] => 120 [1] => 2528 ) [airport] => Array ( [0] => 2700 [1] => 2529 ) [oc] => Array ( [0] => 86400 [1] => 1442364 ) [tr] => Array ( [0] => 10800 [1] => 1640016011 ) [plf] => Array ( [0] => 7200 [1] => 6712 ) [kill] => Array ( [0] => 3600 [1] => 1640019611 ) ) )
The way I tried to get the info:
$AccData = json_decode($MobinfoString, true);
echo $AccData['playerinfo']['rank'].'<br/>';
echo $AccData['playerinfo']['cash'].'<br/>';
foreach ($AccData as $playerinfo) {
echo $playerinfo['playerinfo']['rank'].'<br/>';
echo $playerinfo['character']['gta'].'<br/>';
}
EDIT:
The json string
{"playerinfo":{"rank":"Boss","cash":"€ 5,923,712","weapon":"M-16 (4500)","health":{"width":"100","color":"green"},"wealth":"Too rich to be true","protection":"Bulletproof Humvee","plf":"Huge","plane":"Concorde","crew":"None","pbf":"Large","ship":"None"},"character":{"crime":[120,122],"gta":[400,369],"drugs":[120,2582],"airport":[2700,2582],"oc":[86400,1640020450],"tr":[10800,1640016850],"plf":[7200,3935],"kill":[3600,1640020450]}}
Anyone knows how to do this ? For example I need the Concorde from plane in a variable and the time values from gta in a variable. And some more from this string.
So your first Part is okay and you rank you can just display like the first part as well
$AccData = json_decode($MobinfoString, true);
echo $AccData['playerinfo']['rank'].'<br/>';
echo $AccData['playerinfo']['cash'].'<br/>';
echo $AccData['playerinfo']['rank'].'<br/>';
echo $AccData['playerinfo']['plane'].'<br/>';
echo $AccData['character']['gta'][0].'<br/>';
but the character is on the same level as playerinfo so you need to access it from AccData. also gta is an array like health, so you have to specify which value you want to show, the first so 0 or second which is 1
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
Array
(
[response] => Array
(
[success] => 1
[result] => Array
(
[success] => Array
(
[0] => Array
(
[id] => 1958856
[device_id] => 7565
[message] => ....
[status] => pending
[send_at] => 1416564932
[queued_at] => 0
[sent_at] => 0
[delivered_at] => 0
[expires_at] => 1416568532
[canceled_at] => 0
[failed_at] => 0
[received_at] => 0
[error] =>
[created_at] => 1416564932
[contact] => Array
(
[id] => 312505
[name] => 123
[number] => 123
)
)
)
[fails] => Array
(
)
)
)
[status] => 200
i need to get [id] => 1958856.
$array['response']['result']['success'][0]['id'];
this should get to it
Additional details from iWontStop:
How it works
This is called a multi-dimensional array. They're found in many languages (most useful ones I'd say).
To access an item within the Array, for php it uses []. You can then place a string, number or variable containing one of the two within the [] like so:
$variable['key'];
If an item within an Array is also an Array you simply repeat the process of the [] like so:
$variable['key']['keytwo'];
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.
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