0

Can I use arrays as array values? I have lots of rows in a HTML table: only first column and other columns data array is changing.

I've tried following code but it seems not working.

$period['areas']['AREA I'] = "Area 1";
$period['areas']['AREA II'] = "Area 2";
// and so on...

$customerCountTotal = array();
$customerCountTotal[1]['AREA I'] = 700;
$customerCountTotal[1]['AREA II'] = 500;
$customerCountActive[1]['AREA I'] = 300;
// and so on...

for ($counter = 1; $counter <= 7; $counter++) {

    $rowArray = array("Customers" => $customerCountTotal, "&ndash; active customers" => $customerCountActive /* etc. */);

    foreach ($rowArray as $key => $value) {
        echo "<tr>";
        echo "<th>" . $key . "</th>";
        echo "<td style='text-align: right;'>" . $key . "</th>";

        if(isset($period['areas'])) {
            foreach ($period['areas'] as $key2 => $value2) {
                echo "<td style='text-align: right;'>".$value[$counter][$key2]."</td>";
            }
        }

        echo "</tr>";
    }
}

There might be a better way to do this. Thanks in advance.

3
  • 3
    Uh, what? Could you clarify? I don't get the "Using arrays as array values" Commented Mar 21, 2016 at 12:55
  • hi, can you write a example of table that you need! Commented Mar 21, 2016 at 13:09
  • <td style='text-align: right;'>" . $key . "</th> open td and close th Commented Mar 21, 2016 at 13:10

2 Answers 2

1

In PHP, any value or type can be stored in an array, including other arrays.

Arrays that are simply values stored against an index simply have one dimension to them, but what we're talking about is creating a nested array, also known as a multidimensional array - an array with other arrays as values.

For reference, the following array:

$array = array(
    array("Top left", "Top centre", "Top right"),
    array("Left", "Middle", "Right"),
    array("Bottom left", "Bottom centre", "Bottom right"),
);

is exactly the same as this:

$array = array();
$array []= array("Top left", "Top centre", "Top right"),
$array []= array("Left", "Middle", "Right"),
$array []= array("Bottom left", "Bottom centre", "Bottom right"),

Two great resources for learning more are:

  1. Using Multidimensional Arrays
  2. The PHP manual on arrays has a great section about multidimensional arrays.
Sign up to request clarification or add additional context in comments.

1 Comment

They're not multi-dimensional - they are nested. It is possible to represent multi-dimensional arrays as nested arrays, but not vice versa. The former (by definition) is indexed by ordinal numbers only, further an array element exists for any possible set of coordinates within the range limits of each dimension in a multi-dimensional array (not true for nested arrays). I'm quite aware that there is reference to multi-dimesnional arrays in the PHP manual; its wrong too.
0

Here I'd say that you are putting multidimensionnal arrays as array members, which is possible.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.