Viewed   90 times

I use several associative arrays in my PHP application and I'm using PHP documentor to comment my sources. I never really did specify comments for the arrays in an array, but now I need to do that and don't know how.

$array = array('id' => 'test', 'class' => 'tester', 'options' => array('option1' => 1, 'option2' => 2))

How do I comment this array in the correct way for @var and @param comments? I could do this like this, but I don't know if this is correct:

@param string $array['id']
@param string $array['class']
@param int $array['options']['option1']

But how to do this for the @var part?

 Answers

5

You can't document each key, but you can tell phpDocumentor what type it is.

You could do something like this:

/**
 * Form the array like this:
 * <code>
 * $array = array(
 *   'id'      => 'foo',          // the id
 *   'class'   => 'myClass',     // the class
 * );
 * 
 * </code>
 *
 * @var array[string]string 
 */
$array;
Tuesday, October 18, 2022
4

It is safe but I'd recommend against it. If you put your error reporting up to E_NOTICES you'll see your code producing a lot of them, masking any real errors (such as a mistyped variable name).

What you should really be doing is:

if (!isset($arr[$key]))
    $arr[$key] = 0;

This won't raise a notice (but be very careful not to mis-type $arr inside isset()).

Sunday, December 18, 2022
 
cperez
 
2

Can't you just do:

$resulting_array = $array2 + $array1;

?

Sunday, November 20, 2022
 
5

You're half way there (though you were sorting backwards for membkey based on your example):

function order_by_member_key($a, $b)
{
  if ($a['membkey'] == $b['membkey'])
  {
    // membkey is the same, sort by head
    if ($a['head'] == $b['head']) return 0;
    return $a['head'] == 'y' ? -1 : 1;
  }

  // sort the higher membkey first:
  return $a['membkey'] < $b['membkey'] ? 1 : -1;
}
usort($details, "order_by_member_key");
Friday, September 16, 2022
2

I'm using your $content variable:

$preg1 = preg_match_all('#<name>([^<]+)#', $content, $name_arr);
$preg2 = preg_match_all('#<value>([^<]+)#', $content, $val_arr);
$array = array_combine($name_arr[1], $val_arr[1]);
Wednesday, December 7, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :