I'm pulling data from an XML file, converting it to json and then an array to be able to handle it.
The issue is because of the structure of the XML, it's like inception. Arrays inside of arrays inside of more arrays.
Array
(
[0] => ...
[3] => Array
(
[records] => Array
(
[record] => Array
(
[id] => 506
[sequenceNumber] => 1
[values] => Array
(
[value] => Array
(
[0] => Array
(
[picklistOptionId] => -1
[refId] => 230
[value] => **VALUE**
)
...
I want to be able to call the "**VALUE**" by the refId of 230. What would be the best way?
$arr[3]['records']['record']['values']['value'][0]['value']
There are downsides to having arrays of this many dimensions. e.g., they are less maintainable and (in some cases) slower.
Related
I'm calling an three dimension array from an API, however after different calls to the API the data back is slightly different, sometimes the array keys change. For example the first array may relate to type one in one case, whereas in another case it relates to type two. It's laid out like this
Array
(
[id] =>
[stats] => Array
(
[0] => Array
(
[type] =>
[option] =>
[modifyDate] =>
As stated before sometimes it relates to different types, is there a way of getting an array based on what is inside of it, for example if the "type" in the first array equals type one then assign that to the variable Type1?
Perhaps in a better example, in scenario 1 it shows this:
Array
(
[summonerId] => 39562006
[playerStatSummaries] => Array
(
[0] => Array
(
[playerStatSummaryType] => AramUnranked5x5
[wins] => 4
[modifyDate] => 1481110651000
[aggregatedStats] => Array
(
[totalChampionKills] => 48
[totalTurretsKilled] => 2
[totalAssists] => 171
)
)
whereas in scenario 2 it shows this
Array
(
[summonerId] => 34951469
[playerStatSummaries] => Array
(
[0] => Array
(
[playerStatSummaryType] => CAP5x5
[wins] => 16
[modifyDate] => 1481117277000
[aggregatedStats] => Array
(
[totalChampionKills] => 325
[totalMinionKills] => 1996
[totalTurretsKilled] => 26
[totalNeutralMinionsKilled] => 1048
[totalAssists] => 298
)
)
After some trial and error myself i think a foreloop will be good as it could iterate each array and output chosen keys from the array however i'm still unsure on how to do this, any suggestions?
My advice is similar to this answer: https://stackoverflow.com/a/42708457/2943403
Unfortunately, I cannot give more detailed information without small relevant samples of the related input arrays and a desired output array.
// $new=array(...);
// $old=array(...);
foreach($new as $new_key=>$new_subarray){
foreach(array_diff_key($old,range(0,$new_key)) as $old_subarray){ // no dupe loops
// perform checks between $new_subarray and $old_subarray
}
}
I have an array which is very complicated to generate.
Hence, would like to generate it once and then store as a string in a database.
Then, whenever it is needed, it can be simply retrieved from the DB as a string and declared as an array. Is there an easy way to do such a thing on PHP without using the eval() function.
So the string in the db might be something like
Array
(
[0] => Array
(
['none'] => Array
(
['none'] => Array
(
['Page'] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
)
)
)
[1] => Array
(
['Volume 1'] => Array
(
[] => Array
(
['Page'] => Array
(
[27] => 18
[28] => 19
[29] => 20
)
)
)
['Volume 2'] => Array
(
[] => Array
(
['Page'] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
)
)
)
)
)
You can use the json_encode and json_decode functions. They will serialize an array into a JSON string or deserialize a valid JSON string to an array respectively.
Using serialize/unserialize functions might not always be a good idea, because they come with security issues. Code might get executed upon deserialization which you had not intended. If no outside users have write access to the serialized objects, it should be fine though.
Yup, use serialize and unserialize functions.
I don't know what is going on (maybe it because it is the 12th consecutive hour working).
I always used this notation but now it just doesn't work (I already checked several time that the data is passed to this template I'm talking about).
Data is not iterated and the result is empty.
Mustache code:
{{#ad}}
setInput("{{key}}", "{{value}}");
{{/ad}}
I tried also:
{{#ad}}
setInput("{{key}}", "{{value}}");
{{/ad}}
The data passed is the following:
Array
(
[ad] => Array
(
[0] => Array
(
[key] => id
[value] => 1
)
[1] => Array
(
[key] => created_on
[value] => 1371464401
)
[2] => Array
(
[key] => updated_on
[value] =>
)
[3] => Array
(
[key] => dealer_id
[value] => 1
)
)
)
Solved: be careful to not pass hashes instead of "plain" arrays!! Even if it seemed to be a common array because of the indexing (0 => "a", 1 => "b") it was actually an hash! So just return the malicious data within an array_values($data) to fix it!
I like to compare two arrays without using in_array as both of these arrays are extremely large (50,000 plus). I like to generate a new array of all the ones that are missing from the first array.
What would be the fastest most efficient solution I would use?
First Array
Multidimensional array generated from SQL Query
Array (
[0] => Array (
[id] => 17228219
[name] => ...
)
[1] => Array (
[id] => 17228220
[name] => ...
)
[2] => Array (
[id] => 17228221
[name] => ...
)
[3] => Array (
[id] => 17228222
[name] => ...
)
[4] => Array (
[id] => 17228223
[name] => ...
)
[5] => Array (
[id] => 17228224
[name] => ...
)
)
Second Array
Generated from Simple XML
Array (
[0] => SimpleXMLElement Object (
[0] => 17228219
)
[1] => SimpleXMLElement Object (
[0] => 17228221
)
[2] => SimpleXMLElement Object (
[0] => 17228222
)
[3] => SimpleXMLElement Object (
[0] => 17228224
)
)
New Array
Create an array with missing IDs
Array (
[0] => Array (
[id] => 17228220
[name] => ...
)
[1] => Array (
[id] => 17228223
[name] => ...
)
)
you can make it a little faster by implementing an AVL Tree for example, then it will do it in
O(N*Log(N)), you can find many implementations of trees in php
that will be a little faster then the double 'for' (N^2),
also, you can sort the arrays and move every iteration one step on both arrays, this way you can find the difference, but this is also O(N*Log(N)), its hard to believe it can be faster than this.
p.s.
if its already sorted (like in the code you posted), then you can do it in O(N) with the second way
From the algorithm-point of view the fastest would be item-wise (mergesort like) compare and complement detection by one pass with two sorted arrays... with time complexity O(N logN) + O(MlogM) + O(M + N) ~
O(N log N)...
AVL Tree is an overkill...
Using the 'id' as the array key for both sets would make for a much faster PHP based algorithm as V-X suggests.
However, the most efficient solution is to leave your reference set in the database and add the XML records to it, detecting collisions / non-collisions either on insert or on a subsequent SELECT with join, particularly if the reference set is larger than the comparison set.
You don't say what you intend doing with the un-matched data - which has some bearing on the approach.
I have an array as such
Array
(
[1] => Array
(
[0] => PrettyName1
[1] => PrettyName2
[2] => PrettyName3
)
[2] => Array
(
[0] => UglyURL1
[1] => UglyURL2
[2] => UglyURL3
)
)
I want to be able to put these into an array for a code-igniter template, well when I push and merge arrays I end up messing the whole thing up. Can someone show me the proper way to merge these arrays? I need something like
Array
(
PrettyName1 => UglyURL1
PrettyName2 => UglyURL2
PrettyName3 => UglyURL3
)
Maybe something like this array_combine($ar[1], $ar[2]) ?