Asked  2 Years ago    Answers:  5   Viewed   84 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?



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 
Tuesday, October 18, 2022

Yes, you may trust it. All ways of interpolation a variable are covered in the documentation pretty well.

If you want to have a reason why this was done so, well, I can't help you there. But as always: PHP is old and has evolved a lot, thus introducing inconsistent syntax.

Thursday, October 13, 2022

Your current code is good for two cases:

  1. Reading all data as is, without modification
  2. Reading all data and performing a universal modification

Since what you need is a conditional modification, it seems like you would be better off creating the structure of the array manually. Doing so would add another benefit: code clarity. You should always strive for descriptive code, so building your array with descriptive associative keys would make the intent of the code clearer.

Proposed solution based off the example data (rough sketch that you should tailor to your specific needs):

function readCSV($csvFile)
    $output = [];
    $fileHandle = fopen($csvFile, 'r');
    $header = fgetcsv($fileHandle);
    while (!feof($fileHandle)) {
        $fileRow = fgetcsv($fileHandle, 1024);
        $orderId = $fileRow[0];
        // skip this row if it's empty (the first field contains no id)
        if (empty($orderId)) {
          $fileRow[3] is "Buyer name", the first field that's present in one type of row
          (the one containing common properties of the order). By checking if it's empty,
          we identify the contents of the row - not empty means order row with common
          properties, empty means item row with specific item properties.
        if (!empty($fileRow[3])) {
            // no need to repeat the id inside the array - it's already stored in the key
            $output[$orderId] = [
                'order_number' => $fileRow[1],
                'buyer_username' => $fileRow[2],
                'buyer_name' => $fileRow[3],
                // here you can continue explicitly adding any property you need
        } else {
            // add a new item entry
            $output[$orderId]['items'][] = [
                'item_number' => $fileRow[20],
                'item_title' => $fileRow[21],
                'quantity' => $fileRow[24],
                'price' => $fileRow[25],
                // here you can continue explicitly adding any property you need

    return $output;

Now all the items of your order are neatly stored as subarrays, each containing only data specific to that item, which makes it really easy to iterate:

foreach($orders[$orderId]['items'] as $item)
Wednesday, November 30, 2022

Because the arrays are multidimensional you have to extract the ids like this:

$ids1 = array();
foreach($array1 as $elem1)
    $ids1[] = $elem1['id'];

$ids2 = array();
foreach($array2 as $elem2)
    $ids2[] = $elem2['id'];

$one_not_two = array_diff($ids1,$ids2);

For your specific question, check out array_diff() with multidimensional arrays

Tuesday, December 20, 2022

You need to assign it to a variable first; like so:

$folder = "def_folder";
$inc_folders = array(
    'default' => $folder,
    'file'     => $folder . "/textfile.txt" 

Or alternatively, build the array in two steps:

$inc_folders = array(
    'default' => "def_folder"

$inc_folders['file'] = $inc_folders['default'] . "/textfile.txt";
Monday, December 19, 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 :

Browse Other Code Languages