B
B
bO_oblik2011-09-08 03:48:52
PHP
bO_oblik, 2011-09-08 03:48:52

The simplest addition?

Hello. I've run into this problem for the millionth time and can't think of anything.
I want to put it like this:

<font color="black"><a href="http://s-c.me/23046/s">Copy Source</a> | <a href="http://s-c.me/23046/h">Copy HTML</a><ol>
<li><font color="#696969">//Высчитываем суммы</font></li>
<li><font color="#cc6633">$types_sum</font> = <font color="#0000ff">array</font>();</li>
<li> </li>
<li><font color="#0000ff">foreach</font>(<font color="#cc6633">$types</font>[$type] <font color="#0000ff">as</font> <font color="#cc6633">$project</font>)</li>
<li>{</li>
<li>    <font color="#0000ff">foreach</font>(<font color="#cc6633">$project</font> <font color="#0000ff">as</font> <font color="#cc6633">$source</font> =&gt; <font color="#cc6633">$stages</font>)</li>
<li>    {</li>
<li>        <font color="#0000ff">foreach</font>(<font color="#cc6633">$stages</font> <font color="#0000ff">as</font> <font color="#cc6633">$stage</font> =&gt; <font color="#cc6633">$time</font>)</li>
<li>        {</li>
<li>            <font color="#cc6633">$types_sum</font>[<font color="#cc6633">$source</font>][<font color="#cc6633">$stage</font>] += <font color="#cc6633">$time</font>;</li>
<li>        }</li>
<li>    }</li>
<li>} </li>
</ol></font>

And it always turns out like this:
<font color="black"><a href="http://s-c.me/23047/s">Copy Source</a> | <a href="http://s-c.me/23047/h">Copy HTML</a><ol>
<li><font color="#696969">//Высчитываем суммы</font></li>
<li><font color="#cc6633">$types_sum</font> = <font color="#0000ff">array</font>();</li>
<li> </li>
<li><font color="#0000ff">foreach</font>(<font color="#cc6633">$types</font>[$type] <font color="#0000ff">as</font> <font color="#cc6633">$project</font>)</li>
<li>{</li>
<li>    <font color="#0000ff">foreach</font>(<font color="#cc6633">$project</font> <font color="#0000ff">as</font> <font color="#cc6633">$source</font> =&gt; <font color="#cc6633">$stages</font>)</li>
<li>    {</li>
<li>        <font color="#0000ff">foreach</font>(<font color="#cc6633">$stages</font> <font color="#0000ff">as</font> <font color="#cc6633">$stage</font> =&gt; <font color="#cc6633">$time</font>)</li>
<li>        {</li>
<li>            <font color="#0000ff">if</font>(<font color="#0000ff">isset</font>(<font color="#cc6633">$types_sum</font>[<font color="#cc6633">$source</font>][<font color="#cc6633">$stage</font>]))</li>
<li>            {</li>
<li>            <font color="#cc6633">$types_sum</font>[<font color="#cc6633">$source</font>][<font color="#cc6633">$stage</font>] += <font color="#cc6633">$time</font>;</li>
<li>            }</li>
<li>            <font color="#0000ff">else</font></li>
<li>            {</li>
<li>            <font color="#cc6633">$types_sum</font>[<font color="#cc6633">$source</font>][<font color="#cc6633">$stage</font>] = <font color="#cc6633">$time</font>;</li>
<li>            }</li>
<li>        }</li>
<li>    }</li>
<li>} </li>
</ol></font>

Maybe there is a trick to avoid such ugliness :)

Answer the question

In order to leave comments, you need to log in

7 answer(s)
L
leonid239, 2011-09-08
@leonid239

Try changing the error warning level: en2.php.net/manual/en/errorfunc.configuration.php#ini.error-reporting

O
Ogra, 2011-09-08
@Ogra

$base = isset($types_sum[$source][$stage])? $types_sum[$source][$stage]: 0;

A
Anatoly, 2011-09-08
@taliban

I think this is the maximum =)

if( !isset($types_sum[$source][$stage]) )
    $types_sum[$source][$stage] = 0;
$types_sum[$source][$stage] += $time;

A
anitspam, 2011-09-08
@anitspam

initializing a variable (or array element) before use is not a problem. so let it be as it is.
(although you can smartly suggest changing the architecture).

I
Ilya Plotnikov, 2011-09-08
@ilyaplot

function append($src, $val){

A
Andrey Yantsen, 2011-09-08
@zvirusz

As an option:

<?php
//Высчитываем суммы
$types_sum = array();

foreach($types[$type] as $project)
{
        foreach($project as $source => $stages)
        {
                $stages_sum = array_fill_keys(array_keys($stages), 0);
                foreach($stages as $stage => $time)
                {
                        $stages_sum[$stage] += $time;
                }
                $types_sum[$source] = $stages_sum;
        }
}

A
Andrew, 2011-09-09
@Morfi

$var = &$types_sum[$source][$stage];
$var += $time;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question