I have an array like:
Array
(
[0] => Array
(
[title] => Title 1
[value] => Value 1
[id] => 1428735262
)
[1] => Array
(
[title] => Title 2
[value] => Value 2
[id] => 2428735262
)
[2] => Array
(
[title] => Title 3
[value] => Value 3
[id] => 3428735262
)
)
With mustache I am able to iterate overall of them using:
{{#values}}
<h1>{{title}}</h1>
{{{value}}}
{{/values}}
But what if I only want to show specific value, for example only second element of the array (one with Title 2)?
I tried like:
{{#values}}
{{#2428735262}}
<h1>{{title}}</h1>
{{{value}}}
{{/2428735262}}
{{/values}}
and:
{{#values.2428735262}}
<h1>{{title}}</h1>
{{{value}}}
{{/values.2428735262}}
But none worked. Does anyone know how to get specific item from array ?
PS. I am using PHP version of mustache. Thanks
You can use this:
{{values.1.value}}
Read more at:
https://github.com/bobthecow/mustache.php/wiki/Variable-Resolution
Related
how to get all values array but set all keys the same name
ex :
this is my array
but i need remove this key .. I have used array_values() .. but not working
enter image description here
Seems like you want to grab all the arrays inside the item keys? Using array_column and specifying the item as the key will return the arrays inside each item key in a new array. If that is what you're after? The question was kind of hard to understand IMHO.
array_column($array, 'item');
This will return an array on the following structure.
Array
(
[0] => Array
(
[name] => ttt
[price] => 100
[quantity] => 1
)
[1] => Array
(
[name] => sss
[price] => 100
[quantity] => 1
)
[2] => Array
(
[name] => vvv
[price] => 100
[quantity] => 1
)
)
Here's a demo showing it working.
i'm using this class for accessing odoo through PHP XMLRPC.
Everything works fine almost. I can't search many2one fields just by placing the id of the referenced record. I guess there should be a special way to code the many2one id in the search.
EG: searching in product.supplierinfo by the product_tmpl_id using this code i get emtpy array returned:
$rpc->searchread(array(array("product_tmpl_id","=","3673")),"product.supplierinfo");
Searching for the record by id i get this result:
$rpc->read(array(1),"", "product.supplierinfo");
Array
(
[0] => Array
(
[create_uid] => Array
(
[0] => xxxxx
[1] => xxxxx xxxxx
)
[product_code] =>
[create_date] => 2016-06-22 11:08:00
[name] => Array
(
[0] => 1438
[1] => Provider one
)
[product_uom] => Array
(
[0] => 1
[1] => Unit(s)
)
[sequence] => 1
[product_name] =>
[__last_update] => 2016-06-22 11:42:28
[company_id] => Array
(
[0] => 1
[1] => Company Name
)
[write_uid] => Array
(
[0] => xxxx
[1] => xxxxxx xxxxx
)
[delay] => 1
[write_date] => 2016-06-22 11:42:28
[pricelist_ids] => Array
(
)
[display_name] => Provider One
[min_qty] => 0
[qty] => 0
[product_tmpl_id] => Array
(
[0] => 3673
[1] => Product Name
)
[id] => 1
)
)
How should i code the id of a many2one field?
Any help will be appreciated.
Finally i had the solution. As #czoellner suggested was a problem was the type of the ID was a string and must be an integer. So, this code worked fine.
$rpc->searchread(array(array("product_tmpl_id","=",3673)),"product.supplierinfo");
On the other hand must consider the issue between the id of the product.product and the id of the product.template because they are differents. The table product.supplierinfo uses the id of product.template instead product.product. That causes for searching through the table produc.supplierinfo is necessary to find first the product_tmpl_id of the product to refer to the product. So for finding a all the providers of a product:
#search it by id
$prod_odoo = $rpc->read(array(1),"product.product",array());
#every search returns a 2 dimension array
$prod_suppliers = $rpc->searchread(array(array("product_tmpl_id","=",(int)$prod_odoo[0]["product_tmpl_id"][0])),"product.supplierinfo");
Hope this helps.
I've got a bunch of content pulled out of several databases and compiled into a nested array like this:
Array
(
[0] => Array
(
[id] => 3
[published] => 1433940002
[content] => This is some content
)
[1] => Array
(
[id] => 52
[published] => 1433940001
[content] => This is some more content
)
[2] => Array
(
[id] => 16
[published] => 1433940003
[content] => This is some more content
)
)
Since I cannot sort the content whilst it is retrieved (because it is done using several queries from as many databases) I want to dig down a level into the array and sort by the "published" date whilst maintaining the depth of the array so I end up with...
Array
(
[0] => Array
(
[id] => 52
[published] => 1433940001
[content] => This is some more content
)
[1] => Array
(
[id] => 3
[published] => 1433940002
[content] => This is some content
)
[2] => Array
(
[id] => 16
[published] => 1433940003
[content] => This is some more content
)
)
I'm pretty sure array_multisort() is the way to go but I can't think where to start! Could somebody be so kind as to give me a pointer?
Thanks to #Rizier123's policing of my question I figured it out myself. For anyone else finding this thread here's my solution using the example array I supplied above.
//-- get disorganised content from databases here
usort($array, "publishcmp");
//-- output organised content here
function publishcmp($a , $b){
if ($a['published'] == $b['published']) {
return 0;
}
return ($a['published'] < $b['published']) ? -1 : 1;
}
Pretty much taken verbatim from php.net
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' ve got a XML, which I parsed into a PHP array. It looks like this:
Array
(
[Album] => Array
(
[id] => 12
[name] => My album
[id_parent] => 0
[row] => Array
(
[Album] => Array
(
[row] => Array
(
[id] => 14
[name] => Another album
[id_parent] => 12
[Photos] => Array
(
[row] => Array
(
[0] => Array
(
[id] => 1078
[name] => My first photo
[Album] => Array
(
[row] => Array
(
[name] =>Another album
[id] => 15
[Photos] => Array
(
[row] => Array
(
[0] => Array
(
[id] => 1069
[name] => Summer photo
[checked] => [checked]
)
and so on.
How can I parse it into a HTML list (in nested ul's), to get something like
My album
-Another album
--My first photo
---Another album
----Summer photo
---[end Another album]
--[end My first photo]
-[end Another album]
[end My album]
Albums and photos are just samples. i need to find a way how to parse it to HTML.
EDIT:
Okay, I add something what I really meant - "checked" node. I simply need to put the parent node into tags.
I would use a quick XSLT transform to do it.
If you post a sample of the base XML I can point you in the right direction, but otherwise, check out the examples at these two links. With a little modification they can do what you want.
http://www.w3schools.com/xsl/xsl_for_each.asp
http://www.w3schools.com/xsl/tryxslt.asp?xmlfile=cdcatalog&xsltfile=cdcatalog