Mustache not iterating over array - php

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!

Related

Pull a certain value from within a series of arrays

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.

json_encode does not work with strings as indexes

When I have an array like this :
Array (
[0] => Array ( [0] => 1 [1] => 12 [2] => Essener [3] => 1 )
[1] => Array ( [0] => 2 [1] => 12 [2] => Dinkel Spezial [3] => 0.2 )
[2] => Array ( [0] => 1 [1] => 1 [2] => Essener [3] => 1 )
)
and I use json_encode and echo it, I get this:
[["1","12","Essener","1"],["2","12","Dinkel Spezial","0.2"],["1","1","Essener","1"]]
which is good for me.
Now I have an array with stdClass Objects, which I wasn't able to transform into JSON with json_encode. When I echo it, it just doesn't show anything.
Then I transformed this array with objects to an array like this (with get_object_vars()):
Array (
[0] => Array (
[item_id] => 1
[item_name] => Essener
)
[1] => Array (
[item_id] => 2
[item_name] => Dinkel Spezial
)
[2] => Array (
[item_id] => 3
[item_name] => Saatenbrot
)
)
and when I use json_encode and echo it still doesn't show anything. Can anyone tell me what I am doing wrong or what I need to do to get a JSON array?
I need this json array to send data to an IOS App.
From the documentation:
Note:
In the event of a failure to encode, json_last_error() can be used to determine the exact nature of the error.
So you can try to detect the exact error by yourself. From your information I can't see any error.
Furthermore I don't think, it doesn't return anything. Try to var_dump() the result of json_encode(). I assume it returns false, which means an error occured.
So if anybody wondered what was wrong,
the problem was that i had "ä,ü,ö,ß" in my array and i needed to convert these to unicode and then everything worked just fine.

PHP Restore an Array from a String

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.

Complicated PHP transpose / pivot

I have the following array :
Array
(
[0] => Array
(
[Name] => first_data
[building] => A
[apt] => 16
)
[1] => Array
(
[Name] => first_data
[building] => B
[apt] => 16
)
[2] => Array
(
[Name] => second_data
[building] => A
[apt] => 17
)
[3] => Array
(
[Name] => second_data
[building] => B
[apt] => 18
)
and I need it to be returned as :
Array
(
[0] => Array
(
[Name] => first_data
[A] => 16
[B] => 16
)
[1] => Array
(
[Name] => second_data
[A] => 17
[B] => 18
)
Any ideas?
BTW the first array has hundreds of entries (not only first_data, but second and etc...) plus it has more than A and B.
Thanks in advance.
Not exactly what you want, but if you instead index the new array by the name, you can do this very easily. If the index number is some kind of ID, you can just create a field for it
foreach ( $oldarray as $index => $piece )
{
$newarray[$piece['Name']] = array($piece['building'] => $piece['apt'])
}
This will give you
Array
(
['first_data'] => Array
(
['A'] => 16,
['B'] => 16
)
['second_data'] => Array
(
['A'] => 17,
['B'] => 18
)
)
Since you have two entries with the same new, when you hit the 2nd loop, it will simply add the other building name. If you can work with this layout, then your solution is very easy, it will take more steps to do it exactly as you showed. If you absolutely have to do it the way you showed, you need extra code to loop through the new array, find the building name, add the key in the correct place, but this will be slower if you have a large amount of data.
In my opinion, the way I presented it is a far easier way to look around the array too. If you wanted to know the apt value for A in "second_data" you can just do
$newarray['second_data']['A']
with your array layout, it would require a loop to search the array for "second_data" because you have no idea where it is.

Issue getting info out of array

I'm trying to get info out of this information:
Array (
[result] => success
[totalresults] => 1
[startnumber] => 0
[numreturned] => 1
[tickets] => Array (
[ticket] => Array (
[0] => Array (
[id] => 7
[tid] => 782755
[deptid] => 1
[userid] => 39
[name] => Mark Lønquist
[email] => mark.loenquist#outlook.com
[cc] =>
[c] => 79rzVBeJ
[date] => 2013-04-25 16:14:24
[subject] => test
[status] => Open
[priority] => Medium
[admin] =>
[attachment] =>
[lastreply] => 2013-04-25 16:14:24
[flag] => 0
[service] =>
)
)
)
)
The results are printed using:
print_r($results);
Usually, I've been able to do a simple:
$var = $results['something'];
To get it out, but it wont work with this :( Any help is appreciated.
After reformatting the array you pasted, it becomes clear that some elements are nested several levels deep. (It's a "multidimensional array"; see example #6 in the docs.) In those cases, you have to add additional brackets containing each successive key to reach the depth you want. For example, a sample from your $results array:
Array (
[result] => success
[totalresults] => 1
...
[tickets] => Array (
[ticket] => Array (
[0] => Array (
[id] => 7
[tid] => 782755
...
)
)
)
)
You simply need to do $results['totalresults'] to access "totalresults", but to get "tid" you would need to use $results['tickets']['ticket'][0]['tid'].
If you want to get "tid" from all of the tickets when there are multiple, you will have to iterate (loop) over the array of tickets. Probably something like this (untested, but should be close enough for you to figure out):
foreach ($results['tickets']['ticket'] as $ticket) {
echo $ticket['tid'];
}
To see what the problem is with your print_r() you may add error_reporting(E_ALL); to the top of your code.
Note that if you want to retrieve the value for a key such as 'totalresults' then $results['totalresults'] would be sufficient.
However, if you want to get a key from one of the nested arrays such as email then you would have to use $results['result']['tickets']['ticket'][0]['email'].

Categories