Why is my array_keys() not outputting any keys??

Posted on 2011-04-20
Medium Priority
Last Modified: 2012-05-11
Hi, I am trying to send keys with their values but its only sending the values.

var_dump($attributes)  returns:

array(5) { ["id"]=> NULL ["filename"]=> string(12) "56504579.jpg"
["type"]=> string(10) "image/jpeg"
["size"]=> int(46234) ["caption"]=> string(3) "box" }

You can see I am trying to upload images and give them a caption:


Anyone know how I can get the key (caption) ??

protected function attributes() {
	// return an array of attribute keys and their values
	$attributes = array();
	foreach(self::$db_fields as $field) {
		if(property_exists($this, $field)) {
			$attributes[$field] = $this->$field; // use dynamic variable
			if($attributes == !NULL) {
				echo var_dump($attributes);
	return $attributes; 

protected function sanitized_attributes() {
	global $database;
	$clean_attributes = array();
	// sanitize the values before submitting
	// Note: does not alter the actual value of each attribute
	foreach($this->attributes() as $key => $value) {
		$clean_attributes[key] = $database->clean_strings($value);
	return $clean_attributes; 

protected function create() {
	global $database;
	$attributes = $this->sanitized_attributes();
	$adjust = "ALTER TABLE users AUTO_INCREMENT = 1";
	$sql = "INSERT INTO " . self::$table . " (";
	$sql .= join(", ", array_keys($attributes));
	$sql .= ") VALUES ('";
	$sql .= join("', '", array_values($attributes));
	$sql .= "')";
	$noGaps = $database->query($adjust);
	if($database->query($sql)) {
		$this->id = $database->insert_id(); // Update the variable
		return true;
	else {
		return false;

Open in new window

Question by:FairyBusiness
  • 3
  • 3
LVL 39

Expert Comment

by:Aaron Tomosky
ID: 35438030
Foreach ($attributes as $key=>$value{

Author Comment

ID: 35438041
It gave me this error message:

Notice: Undefined variable: attributes in /hermes/web09c/b2950/moo.auroriellacom/includes/images.php on line 156

Warning: Invalid argument supplied for foreach() in /hermes/web09c/b2950/moo.auroriellacom/includes/images.php on line 156
LVL 39

Expert Comment

by:Aaron Tomosky
ID: 35438092
Foreach ($attributes as $key=>$value){
//you have to put somethinghere
Echo $key;
Echo '</br>';
echo $value;
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.


Author Comment

ID: 35438108
I did this:

foreach($attributes as $key => $value) {
      $clean_attributes[key] = $database->clean_strings($value);

will that not have the key too??
LVL 39

Accepted Solution

Aaron Tomosky earned 2000 total points
ID: 35438141
If you put $key instead of key

Author Closing Comment

ID: 35438156
Can't believe I didn't catch that! Thanks!!!

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses
Course of the Month16 days, 14 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question