Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print the keys of an array

Tags:

arrays

php

I could not figure out how to pass a variable number of variables into a function. I thought passing in an array and using the array keys for the variables names could replace the need to pass extra variables into the function, and it worked (I'm sure there is a better way to accomplish this, suggestions welcome). However, I can't seem to get the keys out of the array inside the function.

The array:

  $parameters[day] = 1;   $parameters[month] = 8;   $parameters[year] = 2010;  

Inside the function:

foreach(key($parameters) as $key) {    print($key);    print("<br>"); } 

The code inside the function retuns a warning: Invalid argument supplied for foreach(). How can I pull the keys out of the array?

like image 473
JMC Avatar asked Aug 17 '10 22:08

JMC


People also ask

How do you find the key of an array?

The array_keys() function is used to get all the keys or a subset of the keys of an array. Note: If the optional search_key_value is specified, then only the keys for that value are returned. Otherwise, all the keys from the array are returned.

How do you print the key in an associative array?

Answer: Use the PHP array_keys() function You can use the PHP array_keys() function to get all the keys out of an associative array.

What is array_keys () used for?

The array_keys() function returns all the keys of an array. It returns an array of all the keys in array.

What is key and value in array?

What are a key and value in an array? Keys are indexes and values are elements of an associative array. Associative arrays are basically objects in JavaScript where indexes are replaced by user-defined keys. They do not have a length property like a normal array and cannot be traversed using a normal for loop.


2 Answers

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>"; 
like image 141
Garrett Avatar answered Oct 14 '22 22:10

Garrett


Passing an associative array to a function is a reasonable way to pass in a variable number of parameters.

foreach ($parameters as $key => $value) {   echo $key . ' = ' . $value . '<br>'; } 

Alternatively you could pass in an instance of stdClass (casting the argument to an object). But an array does the job.

I assume your array keys aren't constants, in which case they should be quoted strings:

$parameters['day'] = 1; $parameters['month'] = 8; $parameters['year'] = 2010;  
like image 43
MrWhite Avatar answered Oct 14 '22 22:10

MrWhite