PHP
php arrays
Submitted by ivan on Sun, 03/08/2008 - 20:55The following:
$columns = array(); foreach($form as $f) { $columns += array($f['form_field_id'] => $f['label']); }
Will create.
Array
(
[] =>
[J] => J
[1] => Title
[3] => Recruiter
[2] => Description
[4] => Job_Type
[5] => Segment
[6] => Owner
)And this will create:
$columns = array(); foreach($form as $f) { $columns[] = array($f['form_field_id'] => $f['label']); }
Helpful php array functions
Submitted by ivan on Sun, 03/08/2008 - 20:55Just when you think you know php. You find these helpful array function.
in_array()
http://au2.php.net/in_array
array_key_exists()
http://au.php.net/array_key_exists
more array functions:
http://au.php.net/manual/en/ref.array.php
$value = array( [0] => 6 [1] => 5 [2] => 4 ); $k = 4; $checked = false; if(is_array($value)) { if(in_array($k, $value)) { $checked = true; } else { $checked = false; } }
If and foreach in drupal templates
Submitted by ivan on Sun, 03/08/2008 - 20:51Examples of using if and foreach in drupal or general php templates:
<?php if($test == 1): ?> <div>do something</div> <?php endif; ?> <?php if($test == 1): ?> <div>do something</div> <?php else: ?> <div>do something else</div> <?php endif; ?> <?php foreach($items as $item): ?> <?php echo $item->getTitle(); ?> <?php endforeach; ?>

