Obtain php keys and values from array

In my morning’s coding session i’ve been thrilled on a problem: obtain keys from a php array(i’m still an amateur programmer!!). Here it’s the final code:

“content” is the array;

foreach($node->content as $key => $field) {
print $key;
print $field[‘#value’];  }

In this way i’ve discovered how Drupal manage the content of a node.

Other snippets useful to debug on Drupal:

var_dump($node); //get the active variables on a node

print_r($myarray);//print elements of an array

For Ubercart module on Drupal(achieve the $content variables for theming a node):

print $node->content[‘product_image’][“#value”];
print $node->content[‘add_to_cart’][“#value”];
$imagePath = $node->field_image_cache[‘0’][‘filepath’];
print $imagePath;
print $node->content[‘sell_price’][“#value”];
print $node->content[‘display_price’][“#value”];
print $node->content[‘model’][“#value”];

Useful links used for this post:



~ di diegotech su agosto 21, 2008.

2 Risposte to “Obtain php keys and values from array”

  1. When developing in Drupal, I use the function drupal_set_message() on a page load to dump out variables like so:

    drupal_set_message(”. print_r($var, TRUE) .”);

    This will preserve the spacing the print_r() function provides by dropping it in a preformatted HTML element. Works like a charm! (I believe if you’re using the devel module, you can also use the dpm() function.)

    And fyi, if you just want to get a list of all the keys in an associative array, the PHP function array_keys() is there.

  2. Whoops! I should’ve figured my HTML tags would be stripped out:

    drupal_set_message(‘<pre>’. print_r($var, TRUE) .'</pre>’);


Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:

Logo WordPress.com

Stai commentando usando il tuo account WordPress.com. Chiudi sessione /  Modifica )

Google+ photo

Stai commentando usando il tuo account Google+. Chiudi sessione /  Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione /  Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione /  Modifica )


Connessione a %s...

%d blogger hanno fatto clic su Mi Piace per questo: