Print the keys of an array
You can use PHP's array_keys function to grab the keys, like so:
foreach(array_keys($parameters) as $paramName)
echo $paramName . "<br>";
Or, you can run through the array using a special foreach
which allows you to separate the key and value for every element, like so:
foreach($parameters as $paramName => $value)
echo $paramName . "<br>";
Also, make sure that you are using a "string"
(with quotes) or integer (like 1337
) as your key, like so:
$parameters["day"] = 1;
$parameters["month"] = 8;
$parameters["year"] = 2010;
OR if you want to get fancier:
$parameters = array(
"day" => 1,
"month" => 8,
"year" => 2010
);
Your code should look like:
$parameters = array(
"day" => 1,
"month" => 8,
"year" => 2010
);
foreach($parameters as $paramName => $paramValue)
echo $paramName . "<br>";
How to print array element with keys & values in PHP?
In the nested foreach
you have to iterate over the $value
which holds the array.
foreach ( $marks as $key => $value) {
foreach ( $value as $key2 => $value2 ) {
// -------^^^^^^-------
echo $key . " : " . $key2 . " - " . $value2 . "<br>";
}
};
How to print key and value of an array of objects
Try something like this:
const array = [{
key: '1',
value: 'value-1'
}, {
key: '2',
value: 'value-2'
}];
const text = array.map(x => `<span class="${x.key}">${x.value}</span><br>`).join("");
const element = document.querySelector('#log');
element.innerHTML = text;
<div id="log"></div>
PHP: How do I print the key from array only once and every value for each item?
So, I suppose, your data looks kind of like this?
$result = (object)[];
$result->devices = [
['deviceId' => 1, 'description' => 'desc1', 'status' => 'status14'],
['deviceId' => 2, 'description' => 'desc2', 'status' => 'status15'],
['deviceId' => 3, 'description' => 'desc3', 'status' => 'status16'],
['deviceId' => 4, 'description' => 'desc2', 'status' => 'status17'],
];
Then to create a CSV file from it I would go like this:
$fp = fopen('csv.csv', 'w+');
foreach($result->devices as $i => $device)
{
// First row? Write headers (keys) first!
if($i == 0) fputcsv($fp, array_keys($device));
// Write data rows
fputcsv($fp, array_values($device));
}
fclose($fp);
This way, you will get this:
deviceID,description,status
1,desc1,status14
2,desc2,status15
3,desc3,status16
4,desc4,status17
Is this what you need?
Print with key and value from array of objects
Given the array you have, you simply need to loop through it:
var arr = [
{"id":1,"name":"admin","password":"admin","role":"Admin"},
{"id":2,"name":"user","password":"user","role":"User"},
{"id":3,"name":"superadmin","password":"superadmin","role":"superadmin"}
]
arr.forEach(function(obj) {
console.log('name: ' + obj.name);
console.log('password: ' + obj.password);
})
in twig how to print out keys and values of an array
Your filters variable is an array of arrays, so you need to do something like:
{% for filter in filters %}
{% for key, value in filter %}
{{ key }} : {{ value }}
{% endfor %}
{% endfor %}
How to print out the keys of an array like $_POST in PHP?
var_dump($_POST);
or
print_r($_POST);
You might insert a pre tag before and after for the clearer output in your browser:
echo '<pre>';
var_dump($_POST);
echo '</pre>';
And I suggest to use Xdebug. It provides an enchanted var_dump that works without pre's as well.
Bash - to print keys and values from a variable
test
is a normal variable and doesn't store any reference to the array. In your case writing $!test
is the same as writing ${someUndefinedVariable}test
(see ✱). The undefined variable will expand to the empty string. test
is a literal string.
To print the keys and values, you have to iterate over the keys and retrieve the corresponding values manually:
declare -A array
array[a1]=123
array[b1]=456
for key in "${!array[@]}"; do
echo "key=$key, value=${array[$key]}"
done
By the way, I'm suprised your command even ran without an error; a closing "
is missing. You cannot nest quotation marks. After the first "
the second "
will end quotation:
|quoted| |quoted |started quote without end -->
| | | | |
"Hello "$!test "$test" Hi"
| | | |
|unquoted |unquoted
✱ $!
is actually a special variable that contains the process number of the last background command. Since you did not start any background commands in your session $!
is empty.
Print array key without value
You can get the size key like this:
for(let i=0; i<elements.length;i++)
{
console.log("***" + elements[i].name + "***");
console.log(Object.keys(elements)[4] + ":" + elements[i].size);
}
But this will work when the size key is always on the 4th index as it is in your elements object
Related Topics
How to Detect an Incoming Request (With PHP Script) from a Cname Subdomain
Is There a PHP Sandbox, Something Like Jsfiddle Is to Js
How to Integrate Nodejs + Socket.Io and PHP
Traits in PHP - Any Real World Examples/Best Practices
PHP Regex to Check Date Is in Yyyy-Mm-Dd Format
Checking If Array Is Multidimensional or Not
PHP Messing with HTML Charset Encoding
Ssl Operation Failed with Code 1: Dh Key Too Small
PHP Emitting 500 on Errors - Where Is This Documented
MySQL Db Question Marks Instead of Hebrew Characters..
Regex Backreference to Match Different Values
What's the Best Practice to Set HTML Attribute via PHP
Print Time in 15-Minute Increments Between Two Times in the Same Day
Leverage Browser Caching for 3Rd Party Js