开发者

I've spent hours trying to get these array keys into variables but everything I try is not working

I hav开发者_运维问答e an array of all possible combinations of values, a bit like working out what monetary values I could make with only certain coins. Now I have an array built but much of the useful data are keys and not values.

A small snippet is below: Each root key is an array with keys total, denomination and quantity. Each of the quantities multiplied by the denominations total the total. While I've been able to access the total easily enough I just can't get a handle on the denominations and the quantities.

It's my plan to output to separate radio buttons like so:

foreach($array as $arr)
{
    echo '<input type="radio" name="name" value="'.$arr[$total].'">';
    foreach($arr[denom] as $index => $d)
    {
        echo $d[qty][$index].' x '.$d[denom][$index].' = '.($qty[$index]*$denom[$index]).'<br>';
    }
}

Here's the array I have, any help would be much appreciate, I'm usually great at this bot it's driving me crazy

Array
(
    [2] => Array
        (
            [total] => 105
            [denom] => Array
                (
                    [0] => 105
                )

            [qty] => Array
                (
                    [0] => 1
                )

        )

    [3] => Array
        (
            [total] => 210
            [denom] => Array
                (
                    [0] => 105
                )

            [qty] => Array
                (
                    [0] => 2
                )

        )

    [4] => Array
        (
            [total] => 300
            [denom] => Array
                (
                    [0] => 300
                )

            [qty] => Array
                (
                    [0] => 1
                )

        )

    [5] => Array
        (
            [total] => 405
            [denom] => Array
                (
                    [0] => 300
                    [1] => 105
                )

            [qty] => Array
                (
                    [0] => 1
                    [1] => 1
                )

        )

    [6] => Array
        (
            [total] => 500
            [denom] => Array
                (
                    [0] => 500
                )

            [qty] => Array
                (
                    [0] => 1
                )

        )

    [7] => Array
        (
            [total] => 605
            [denom] => Array
                (
                    [0] => 500
                    [1] => 105
                )

            [qty] => Array
                (
                    [0] => 1
                    [1] => 1
                )

        )


Constant non-numeric array indices should be written like any other string, i.e. encapsulated in quotes.

foreach($array as $arr) {
    echo '<input type="radio" name="name" value="'.$arr['total'].'">';
    foreach($arr['denom'] as $index => $d){
        for ($j = 0;$j < count($denom);$j++) {
            echo $d['qty'][$j].' x '.$d['denom'][$j].' = ';
            echo ($d['qty'][$j]*$d['denom'][$j]) . '<br>';
        }
    }
}


First format your array like this in my code, add 6, 7 array elements. I modify for loop too.

<?php
$array = array(2 => array('total' => 105, 'denom' => array(0 => 105), 'qty' => array(0 => 1)),
                             3 => array('total' => 210, 'denom' => array(0 => 105), 'qty' => array(0 => 2)),
                             4 => array('total' => 300, 'denom' => array(0 => 300), 'qty' => array(0 => 1)),
                             5 => array('total' => 405, 'denom' => array(0 => 300, 1 => 105), 'qty' => array(0 => 1, 1 => 1)),
                             );

foreach($array as $arr)
{
//var_dump($arr);
    echo '<input type="radio" name="name" value="'.$arr['total'].'">';
    foreach($arr['denom'] as $index => $d)
    {
        echo $arr['qty'][$index].' x '.$d.' = '.($arr['qty'][$index]*$d).'<br>';
    }
}

?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜