so currently I have a PHP function that prints out the applications configuration:
function cconfig() {
global $config;
// If the user wants debug information shown:
if (SHOWDEBUG == true) {
// If there is config information to show
if (isset($config)) {
cout("Variable $config is set.", "debug");
foreach ($config as $current) {
echo $current."<br/>";
}
} else {
cout("Variable $config isn't set.", "debug");
}
}
}
The output only shows the values of the array that it's at $current. For example:
Value1
Value2
Value3
How can I edit this function so instead of just showing the value at a given key, it also shows the key?
ConfigEntry1 = Value1
ConfigEntry2 = Value2
ConfigEntry3 = Value3
foreach ($config as $key => $current) {
echo "$key = $current<br />";
}
Expose the key like so:
foreach ($config as $k => $v) {
echo $k. ' ' . $v ."<br/>";
}
The two forms a foreach can take, from the manual:
foreach (array_expression as $value)
statement
foreach (array_expression as $key => $value)
statement
The first form loops over the array
given by array_expression. On each
loop, the value of the current element
is assigned to $value and the internal
array pointer is advanced by one (so
on the next loop, you'll be looking at
the next element).
The second form does the same thing, except that the current element's key
will be assigned to the variable $key
on each loop.
Change:
foreach ($config as $current) {
echo $current."<br/>";
}
to
foreach ($config as $key => $val) {
echo $key." = " . $val . "<br/>";
}
Related
so i have an object:
$user->name->first = "Bob";
$user->name->last = "Smith";
$user->address->street = "1234 anywhere st.";
$user->address->city = "Chicago";
$user->address->state = "Texas";
how can i iterate through this object without knowing the "name,address" property?
I want to be able to do
foreach ($user as $key -> $value)
{}
now i can do this:
foreach ($user as $key -> $value)
{
foreach ($value as $k => $v)
{
echo $k . "," . $v . "\n";
}
}
and i get a nice little list of
first,Bob
last,Smith
street,1234 anywhere st.
city,Chicago
state,Texas
but how can i get the property name or address to print?
i.e.
name,first,Bob
name,last,Smith
address,street,1234 anywhere st.
address,city,Chicago
address,state,Texas
The value you want is in $key already from the first foreach loop.
foreach ($user as $key => $value)
{
foreach ($value as $k => $v)
{
echo "$key,$k,$v\n";
}
}
IDEOne.com demo
below is a php object which is retrieving some values from mysql db through a php method
$query = "SELECT imgpath from images";
$oMySQL->ExecuteSQL($query);
Now when i use this
$result=$oMySQL->ExecuteSQL($query);
it prints "Array"
How to iterate through the array
Regards Jane
A simple foreach loop.
foreach ($result as $key => $val) {
foreach ($val as $label => $item) {
echo $label . " - " . $item;
}
}
$key will hold the associative name the array element, and $val will hold the value of the element.
You mean it print array when you do echo $result?
Then:
foreach($result as $index => $value) {
echo $index . '=' $value;
}
Is there a way to traverse an array such as $_POST to see the field names and not just the values. To see the values I do something like this.
foreach ($_POST as $value){
echo $value;
}
This will show me the values - but I would like to also display the names in that array. If my $_POST value was something like $_POST['something'] and it stored 55; I want to output "something".
I have a few select fields that I need this for.
You mean like this?
foreach ( $_POST as $key => $value )
{
echo "$key : $value <br>";
}
you can also use array_keys if you just want an array of the keys to iterate over.
You can also use array_walk if you want to use a callback to iterate:
function test_walk( &$value, $key )
{
...do stuff...
}
array_walk( $arr, 'test_walk' );
foreach ($_POST as $key => $value) {
echo $key; // Field name
}
Or use array_keys to fetch all the keys from an array.
foreach ($_POST as $key => $value){
echo $key.': '.$value.'<br />';
}
If you just want the keys:
foreach (array_keys($_POST) as $key)
{
echo $key;
}
Or...
foreach ($_POST as $key => $value)
{
echo $key;
}
If you want both keys and values:
foreach ($_POST as $key => $value)
{
echo $key, ': ', $value;
}
For just the keys:
$array = array_keys($_POST);
Output them with:
var_dump($array);
-or-
print_r($array);
I would like to iterate through an xml document to get its values. See the given code
foreach ($xml->children() as $key1=>$value1 /*($xml->children() as $second_gen)*/ ) {
echo ' 1 ' .$key1.' '.$value1.'<br>';
foreach ($second_gen as $key2=>$value2) {
echo ' ___2 ' .$key2.' '.$value2.'<br>';
}
}
So what I want to do is to make $second_gen equals to the children of the each current iteration of the loop. I was able to do this by putting it in the foreach, but this prevented me from using key/value. So is there any solution to get both?
Thanks!
The value in a foreach loop equals to the value itself. So if you leave out the $key => part or not doesn't change the $value:
foreach ( $xml->children() as $key1 => $value1 )
{
foreach ( $value1->children() as $key2 = $value2 )
{
// ...
}
}
I have a POST in PHP for which I won't always know the names of the variable fields I will be processing.
I have a function that will loop through the values (however I would also like to capture the variable name that goes with it.)
foreach ($_POST as $entry)
{
print $entry . "<br>";
}
Once I figure out how to grab the variable names, I also need to figure out how I can make the function smart enough to detect and loop through arrays for a variable if they are present (i.e. if I have some checkbox values.)
If you just want to print the entire $_POST array to verify your data is being sent correctly, use print_r:
print_r($_POST);
To recursively print the contents of an array:
printArray($_POST);
function printArray($array){
foreach ($array as $key => $value){
echo "$key => $value";
if(is_array($value)){ //If $value is an array, print it as well!
printArray($value);
}
}
}
Apply some padding to nested arrays:
printArray($_POST);
/*
* $pad='' gives $pad a default value, meaning we don't have
* to pass printArray a value for it if we don't want to if we're
* happy with the given default value (no padding)
*/
function printArray($array, $pad=''){
foreach ($array as $key => $value){
echo $pad . "$key => $value";
if(is_array($value)){
printArray($value, $pad.' ');
}
}
}
is_array returns true if the given variable is an array.
You can also use array_keys which will return all the string names.
You can have the foreach loop show the index along with the value:
foreach ($_POST as $key => $entry)
{
print $key . ": " . $entry . "<br>";
}
As to the array checking, use the is_array() function:
foreach ($_POST as $key => $entry)
{
if (is_array($entry)) {
foreach($entry as $value) {
print $key . ": " . $value . "<br>";
}
} else {
print $key . ": " . $entry . "<br>";
}
}
It's much better to use:
if (${'_'.$_SERVER['REQUEST_METHOD']}) {
$kv = array();
foreach (${'_'.$_SERVER['REQUEST_METHOD']} as $key => $value) {
$kv[] = "$key=$value";
}
}
If you want to detect array fields use a code like this:
foreach ($_POST as $key => $entry)
{
if (is_array($entry)){
print $key . ": " . implode(',',$entry) . "<br>";
}
else {
print $key . ": " . $entry . "<br>";
}
}