Jump to content

Recommended Posts

<?php

function evaluate($expression)
{
    // TODO : add rendering code here

   
}

// 100 + 200 + 300
$expression1 = [
    "type" => "add",
    'children' => [
        [
            "type" => "number",
            "value" => 100
        ],
        [
            "type" => "number",
            "value" => 200
        ],
        [
            "type" => "number",
            "value" => 300
        ]
    ]
];

// 100 + 2 * (45 +5)
$expression2 = [
    "type" => "add",
    'children' => [
        [
            "type" => "number",
            "value" => 100
        ],
        [
            "type" => "multiply",
            "children" => [
                [
                    "type" => "number",
                    "value" => 2
                ],
                [
                    "type" => "add",
                    "children" => [
                        [
                            "type" => "number",
                            "value" => 5
                        ],
                        [
                            "type" => "number",
                            "value" => 45
                        ]
                    ]
                ]
            ]
        ]
    ]
];



// 1 + 100 / 1000
$expression3 = [
    "type" => "add",
    'children' => [
        [
            "type" => "number",
            "value" => 1
        ],
        [
            "type" => "fraction",
            "top" =>
            [
                "type" => "number",
                "value" => 100
            ],
            "bottom" =>
            [
                "type" => "number",
                "value" => 1000
            ]
        ]
    ]
];

echo "Expression 1 evaluates to: " . evaluate($expression1) . " <br>";

echo "Expression 2 evaluates to: " . evaluate($expression2) . " <br>"
 
echo "Expression 3 evaluates to: " . evaluate($expression3) . " <br>"

 

Yes?  What is your question?  And - is this some kind of homework?

My guess is you access an array with php code.  Have you written any yet this semester?

PS - are you studying recursion at this time?

 

Edited by ginerjm

yes it's a duty I am blocked. Because I want to get the value back but he gives me that

 foreach ($expression as $i) {
        $var1 = var_dump($i[0]["value"]);
        $var2 = var_dump($i[1]["value"]);
        $var2 = var_dump($i[2]["value"]);

result: Warning: Illegal string offset 'value'

Blocked?   Look closely at your expression contents.  Then figure out what level of that multi-dimension array you want to access.  You are currently assuming that $i has numeric indices.  No.  It has 2 named elements - 'type' and 'children'.   Work with that knowledge

But for the expression I can't access the table in a table: 

$expression2 = [
    "type" => "add",
    'children' => [
        [
            "type" => "number",
            "value" => 100
        ],
        [
            "type" => "multiply",
            "children" => [
                [
                    "type" => "number",
                    "value" => 2
                ],
                [
                    "type" => "add",
                    "children" => [
                        [
                            "type" => "number",
                            "value" => 5
                        ],
                        [
                            "type" => "number",
                            "value" => 45
                        ]
                    ]
                ]
            ]
        ]
    ]
];

 

$expression2 = [
    "type" => "add",
    'children' => [
        [
            "type" => "number",
            "value" => 100                                     // $expression2['children'][0]['value']
        ],
        [
            "type" => "multiply",
            "children" => [
                [
                    "type" => "number",
                    "value" => 2                              // $expression2['children'][1]['children'][0]['value']
                ],
                [
                    "type" => "add",
                    "children" => [
                        [
                            "type" => "number",
                            "value" => 5                      // $expression2['children'][1]['children'][1]['children'][0]['value']
                        ],
                        [
                            "type" => "number",
                            "value" => 45                     // $expression2['children'][1]['children'][1]['children'][1]['value']
                        ]
                    ]
                ]
            ]
        ]
    ]
];

It's easier if you do a print_r() of the array so you see all the indexes

echo '<pre>$expression2 = ' . print_r($expression2, 1) . '</pre>';

then follow the indexes down the tree ....

image.thumb.png.86de330d4336dbeef695fc5fae1f6bb1.png

showing the path for

echo $expression2['children'][1]['children'][1]['children'][1]['value'];                   // 45

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.