padana Posted September 17, 2021 Share Posted September 17, 2021 <?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>" Quote Link to comment https://forums.phpfreaks.com/topic/313760-how-to-access-the-array-via-the-function-php/ Share on other sites More sharing options...
ginerjm Posted September 17, 2021 Share Posted September 17, 2021 (edited) 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 September 17, 2021 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/313760-how-to-access-the-array-via-the-function-php/#findComment-1590021 Share on other sites More sharing options...
padana Posted September 17, 2021 Author Share Posted September 17, 2021 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' Quote Link to comment https://forums.phpfreaks.com/topic/313760-how-to-access-the-array-via-the-function-php/#findComment-1590022 Share on other sites More sharing options...
ginerjm Posted September 17, 2021 Share Posted September 17, 2021 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 Quote Link to comment https://forums.phpfreaks.com/topic/313760-how-to-access-the-array-via-the-function-php/#findComment-1590024 Share on other sites More sharing options...
padana Posted September 17, 2021 Author Share Posted September 17, 2021 here for expression1 I found the result: $var1 = $expression['children'][0]['value']; $var2 = $expression['children'][1]['value']; $var3 = $expression['children'][2]['value']; return $var1 + $var2 + $var3; result: 600 Quote Link to comment https://forums.phpfreaks.com/topic/313760-how-to-access-the-array-via-the-function-php/#findComment-1590027 Share on other sites More sharing options...
padana Posted September 17, 2021 Author Share Posted September 17, 2021 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 ] ] ] ] ] ] ]; Quote Link to comment https://forums.phpfreaks.com/topic/313760-how-to-access-the-array-via-the-function-php/#findComment-1590028 Share on other sites More sharing options...
ginerjm Posted September 17, 2021 Share Posted September 17, 2021 You have a different type of array in #2. Have to modify your code to be more flexible. Good luck. I haven't the time to do your homework for you. Quote Link to comment https://forums.phpfreaks.com/topic/313760-how-to-access-the-array-via-the-function-php/#findComment-1590030 Share on other sites More sharing options...
Barand Posted September 17, 2021 Share Posted September 17, 2021 $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 .... showing the path for echo $expression2['children'][1]['children'][1]['children'][1]['value']; // 45 Quote Link to comment https://forums.phpfreaks.com/topic/313760-how-to-access-the-array-via-the-function-php/#findComment-1590034 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.