How to get the $key from a multidimentional array using php? - php

Below is a simple array that I created:
$colors = array(
"parent1" =>array(
"item1"=>"red",
"item2"=>"green",
"item3"=>"blue",
"item4"=>"yellow"
),
"parent2" =>array(
"item1"=>"red",
"item2"=>"green",
"item3"=>"blue",
"item4"=>"yellow"
)
);
What I need to get is the key of my level 1 arrays which are string "parent1" and "parent2".
Currently I'm using foreach with while loop to get the key
foreach ($colors as $valuep) {
while (list($key, $value) = each($colors)) {
echo "$key<br />";
}
}
but I'm only able to get the "parent2" string from using the above method and not "parent1".

You're so close.
foreah($colors as $key => $val)
{
echo $key . "<br/>";
}

Use the key like so:
foreach ($colors as $key => $value) {
echo $key.'<br>';
}

To print out the keys:
foreach ($colors as $key => $value) {
echo $key . '<br />';
}
You can also get all of the keys from an array by using the array_keys() method, for example:
$keys = array_keys($colors);
foreach ($keys as $key) {
echo $key . '<br />';
}

Related

iterate associate array in php does not print out values

I'm trying out the following code for arrays in php, I create a associate array, print out the values and add one more to the array - print out again. This works, but if I try the foreach ($MovieCollection as $key => $value) it does not print out the values. Why does it not do that?
$myArray = array("Star Wars", "The Shining");
foreach ($myArray as $val)
{
echo("Movie: " . $val ."<br>");
}
$MovieCollection = array();
$MovieCollection[] = array('title' => 'Star Wars', 'description' =>'classic');
foreach ($MovieCollection as $film )
{
echo($film['title'] .": " . $film['description'] ."<br>");
}
$MovieCollection[] = array('title' => 'The shinning', 'description' =>'creepy');
foreach ($MovieCollection as $film )
{
echo($film['title'] .": " . $film['description'] ."<br>");
}
echo("<br><br>");
// This does not print the values?
foreach ($MovieCollection as $key => $value)
{
echo($key .": " . $value ."<br>");
}
That is because in this part $MovieCollection is an array of arrays and if you want to echo the $value which is an array, you will do an Array to string conversion which does not work.
What you might do is use another foreach to show the values per array:
foreach ($MovieCollection as $value) {
foreach ($value as $k => $v) {
echo($k .": " . $v ."<br>");
}
}
See a Php demo

PHP read value form multidimensional array

its easy for sure..
i have code like this:
$indeks = 0;
foreach ($list as $k => $v)
{
$data['fname'] = $customer->firstname;
$data['lname'] = $customer->lastname;
$data['code'] = $code['code'];
$tablica[$indeks] = $data;
$indeks++;
and i want to read only 'code' value for each array.
i try:
foreach($tablica as $k => $v){
foreach ($v as $key => $value ) {
echo $value
}
}
but i get all arrays values.
when i try
foreach($tablica as $k => $v){
foreach ($v['code'] as $key => $value ) {
echo $value
}
}
i have nothing...
thx for help
You can use array_column function to get all values of column, for example:
foreach (array_column($tablica, 'code') as $value) {
echo $value;
}
I think a For loop should help
for($i=0;$i<count($tablica);$i++){
echo $tablica[$i]['code'];
}
or get all Codes into an Array
$code = array();
for($i=0;$i<count($tablica);$i++){
$code[$i] = $tablica[$i]['code'];
}
You don't need nested loops.
foreach ($tablica as $value) {
echo $value['code'];
}
DEMO

php associative array: no array build with key from variable name

this code works as expected
$fruits = array("d" => "Zitrone", "b" => "Banane", "c" => "Apfel");
$fruits["a"] = "Orange";
//print_r($fruits);
ksort($fruits);
foreach ($fruits as $key => $val) {
echo "$key = $val\n";
}
ok, now I would like to do the same programmatically:
$collection = array();
foreach ($xml->abschnitte[0]->abschnitt as $sec) {
//echo $sec['strecke']; // this works, the strings are printed out
$collection[$sec['strecke']] = $sec['id'];
}
//print_r($collection); // nothing to see here
//ksort($collection);
foreach ($collection as $key => $val) {
echo $key . " = " . $val . "\n";
}
it seems that there no collection will be build. but there must be a way to build the key up from variables. what do i miss? thanks in advance, mischl

Traverse $_POST Array to show field Names

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);

PHP array has only one entry,how to get its key?

$arr['thisiskey'] = 1;
Like above,how to get "thisiskey" programmatically?
Use array_keys:
$keys = array_keys($array);
var_dump($keys[0]);
The simplest solution:
key($arr);
key returns current key of the array
Iteratively:
foreach ($array as $key => $value){
print $key . ' = ' . $value .'\n';
}
foreach ($arr as $key => $value) {
echo $key . PHP_EOL;
}
list($key, ) = each($arr);
print $key;

Categories