PHP
2
Вклад в тег
fwrite($fp, json_encode($arr) );
if(isset($file)){
$arr = array();
}
$arr[count($arr)+1]["name"] = $name;
$arr[count($arr)]["email"] = $email;
echo json_encode($arr);
$arr[] = [
'name' => $name,
'email' => $email
];
<?php
$arr = [
[
'result' => 'a',
'components' => [
2,
41
],
'formula' => '2 + 41',
'answer' => 43
],
[
'result' => 'd',
'components' => [
'a',
1
],
'formula' => 'a - 1',
'answer' => ''
]
];
$resultArr = doReplacement($arr);
var_dump($resultArr);
function doReplacement(array $arr)
{
foreach ($arr as &$item)
{
$components = &$item['components'];
$formula = &$item['formula'];
foreach ($components as &$component)
{
if (!is_numeric($component))
{
$value = findComponentValue($arr, $component);
$formula = str_replace($component, $value, $formula);
$component = $value;
}
}
if (empty($item['answer']))
{
// calculate formula
}
}
return $arr;
}
function findComponentValue(array $arr, $component)
{
$value = 0;
foreach ($arr as $item)
{
$result = $item['result'];
if ($result == $component)
{
$value = intval($item['answer']);
}
}
return $value;
}