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

Posted on 2011-04-20
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
    LVL 38

    Expert Comment

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

    Author Comment

    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 38

    Expert Comment

    by:Aaron Tomosky
    Foreach ($attributes as $key=>$value){
    //you have to put somethinghere
    Echo $key;
    Echo '</br>';
    echo $value;

    Author Comment

    I did this:

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

    will that not have the key too??
    LVL 38

    Accepted Solution

    If you put $key instead of key

    Author Closing Comment

    Can't believe I didn't catch that! Thanks!!!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Suggested Solutions

    Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
    Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
    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…
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

    760 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

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now