Mustache (PHP) Outputting associative array keys - php

In Mustache can I print out the name of an associative array key instead of its value?
i.e. So instead of this:
$cars= array(
'name'=>'ferrari', 'color'=>'red',
'name'=>'lambo', 'color'=>'yellow'
);
....
{{#cars}}
{{name}} is {{color}}
{{/cars}}
I would prefer to have a data source with a smaller footprint:
$cars= array('ferrari'=>'red', 'lambo'=>'yellow');
....
{{#cars}}
{{array_key_here}} is {{.}}
{{/cars}}
Is it possible?

I'm sure the OP has already moved on, but to anyone stumbling upon this post, I'd just like to point out that the reason this is not possible is because there is no predictable means of referencing anything in that array.
Think of a key in terms of a map, and you have more elaboration.

Use array_keys(). Or if you want to reverse index => value to value => index you can use array_flip().

Related

PHP - Remove associative array element and assign it to a variable in one action?

I'm trying to find out if there's a way of removing an array element and at the same time storing that value in a variable.
i.e.
$array = [
'foo' => 'a',
'bar' => 'b'
];
// Perform the following with one action?
$var = $array['foo'];
unset($array['foo']);
Edit: I mean if it can be done without a custom function.
There is but it's slow and ugly.
$var = array_splice($array, array_search('foo', array_keys($array)), 1)['foo'];
I'd stick with the 2-liner.
It can be done under certain circumstances.
There are two functions which do what sort of what you want.
array_pop($stack);
array_shift($stack);
But array_pop gets and removes only the last element and array_push the first of the array which can lead to unexxpected behaviour of your code if you are using an associative array.
However if you can restructure your array to fit for these functions without increasing the complexity (which would make this whole thing pointless) it can be done.
The two links for the functions are:
array_pop
array_shift
An entire reference of the array function can be found here. Maybe you find something that serves you the way you need it to:
Array functions reference

Function to assign sequence keys to an array

I have an array of unique colors which is selected from a table. But keys of that particular array is not in sequence order due to some calculations. Now I wish to assign a sequence numbers to that array... Is any function to change keys of array..
Thanks...
Array(
[81]=>yellow
[86]=>gray
[93]=>wine
[103]=>marigold
[125]=>maroon
[134]=>pewter
[142]=>forestgreen
[151]=>grey
)
i wish to change this array to
Array(
[1]=>yellow
[2]=>gray
[3]=>wine
[4]=>marigold
[5]=>maroon
[6]=>pewter
[7]=>forestgreen
[8]=>grey)
If you want to sort your array by some calculation, you can use usort(), which sorts an array by using a callback function. In this callback function, you can compare two elements of the array and decide (by any means you need) which one goes first. Read through the examples on the page I linked to learn more!
use: sort($color); check the examples from this link: sort

Can you use array_push() to push an array onto another array?

Is this the correct way of pushing an array into another array? Also, do all array pushes require 2 arguments?
$edge = array( "nodeTo" => "$to");
array_push( $node["adjacencies"], $edge);
The documentation is quite clear on this:
Note: If you use array_push() to add one element to the array it's better to use $array[] = because in that way there is no overhead of calling a function
The function definition lists a two-argument requirement. You need the pushee and something to push at least.
array_push is really designed to be used to push/append multiple elements simultaneously.
array_push will add whatever you give it as a new element on the end of the target array.
So your example will add a new array as the last element of $node["adjacencies"] which will be your node connection array. For your example I believe you would like to use
$node["adjacencies"] += $edge
to correctly compose an adjacency map

Declaring static arrays in php

I am new to php. I was wondering how I could declare a static array in php. Here is what I would do in C. How is the corresponding php code for it?
char a[][] = { (1,1), (1,2), (1,3), (2,1), (2,2), (2,3), (3,1), (3,2), (3,3) };
From what I read it has to be something like this -
$a = array( 1 => array(1,1), 2 => array(1,2), ... );
Is this correct? If so it sucks :) I hope I am wrong.
Thanks,
- Pav
You've already found the way to do it natively.
Another option would be to declare your data as JSON (a very concise and human-friendly format). This could be either in a separate file bundled with your app, or directly in your code in a string. Then parse the JSON at runtime. Since PHP isn't exactly known for speed, this may or may not make your noticeably app slower to start.
you have it already figured out in your question.
One thing I would add is that you do not need to explicitly define the keys if you are intending to use a zero based array, this is assumed and can be done like so...
$a = array(array(1,1),array(1,2), ... );
You can also use what is called associative arrays which use string keys and you define them the same way you do in your example, except use strings instead of numbers...
$ass_array = array( 'array_1' => array(1,1), 'array_2' => array(1,2), ... );
you would then call your associative array like this...
$ass_array['array_1'];
Also, if you want to append single items to an array (for example in a loop to load an array)...
$ass_array[] = $item;
Further to jondavidjohn's anwser, you could just write a quick script to grab your list of values and generate the array statement for you.
No need to care how verbose the syntax is then. If the task is long and repetitious enough to care, don't do it by hand. :)

PHP: can one determine the parent array's name and key from a reference to an array element?

let's assume we have an array like this
$arr=array(array('a'=>1,'b'=>2),array('c'=>3,'d'=>4));
and a reference to one of its elements
$element=&$arr[1]['c'];
My question is is it possible to get back to the original array using the reference alone?
That is to get back to the parent array in some way without knowing it by name... This would be useful to me in a more complex scenario.
No, it's certainly not possible. Being a "reference" (as PHP calls it; it's actually a copy inhibitor) doesn't help at all in that matter. You'll have to store the original array together with the element.
$elArrPair = array(
"container" => $arr,
"element" => &$arr[1]['c'],
);
This way you can change the element with $elArrPair["element"] = $newValue and still be able to access the container.
You cannot get from $element to $arr. You can use in_array() of course, but nothing about $element contains a reference to $arr.
You copy the contect from one variable to another, nothing else, there is no connection between the two variables.

Categories