有一条线
$all_prices = array($data_platforms__integration_cost_agreed[0]->integration_cost);
她通过var_dump($all_prices)输出这个
array(1) {
[0]=>
string(5) "34634"
}
array(1) {
[0]=>
string(6) "200000"
}
我以显示数字 34634 和 200000 的方式访问数据库 - 这些是价格
$data_platforms__integration_cost_agreed = DataPlatformsForm::find()
->select('integration_cost')
->where(['id_blogger' => $bloggers->id])
->all();
问题
如何将这些数字 34634 和 200000 相加?
控制器中的方法
public function actionIndex()
{
$bloggers_db = BloggerForm::find()->orderBy('id desc')->all();
// Вытаскиваем клиента который привязан к проекту
$node_project_klient = NodeProjectKlient::find()->Where(['id_project' => Yii::$app->request->get("project")])->one();
$data_platforms_db = DataPlatformsForm::find()->all();
// Выводим всех клиентов которые привязаные к менеджеру
$manager_node_klient_db = User::find()
->leftJoin('node_klient_manager', '`node_klient_manager`.`id_klient` = `user`.`id`')
->where(['node_klient_manager.id_manager' => Yii::$app->user->identity->id])
->orderBy('id desc')
->all();
// Выводим всех блогеров по проекту (нейтральные)
$bloggers_neutral_projects_db = BloggerForm::find()
->leftJoin('node_project_blogger', '`node_project_blogger`.`id_blogger` = `bloggers`.`id`')
->Where([
'node_project_blogger.id_project' => Yii::$app->request->get('project'),
'node_project_blogger.long_list' => 0,
'node_project_blogger.agreed' => 0,
])
->orderBy('id desc')
->all();
// Выводим всех блогеров по проекту (отклонённые)
$bloggers_rejected_projects_db = BloggerForm::find()
->leftJoin('node_project_blogger', '`node_project_blogger`.`id_blogger` = `bloggers`.`id`')
->Where([
'node_project_blogger.id_project' => Yii::$app->request->get('project'),
'node_project_blogger.long_list' => 2,
'node_project_blogger.agreed' => 0,
])
->orderBy('id desc')
->all();
// Выводим всех блогеров по проекту (согласованные)
$bloggers_approved_projects_db = BloggerForm::find()
->leftJoin('node_project_blogger', '`node_project_blogger`.`id_blogger` = `bloggers`.`id`')
->Where([
'node_project_blogger.id_project' => Yii::$app->request->get('project'),
'node_project_blogger.long_list' => 1,
'node_project_blogger.agreed' => 0,
])
->orderBy('id desc')
->all();
// Выводим блогеров из таблицы связей для обновлений статусов
$node_project_blogger_db = NodeProjectBlogger::find()->where(['id_project' => Yii::$app->request->get('project')])->all();
if ($this->request->isPost)
{
// Обработка статусов блогеров по id
foreach ($node_project_blogger_db as $node_project_blogger)
{
// ========================== LOG CREATE ================================= //
$str_log = '================================================';
$array = array(
'Controller' => 'Manager',
'Method' => 'Index',
'id_project' => Yii::$app->request->get('project'),
'id_blogger' => $node_project_blogger->id_blogger,
'long_list' => $this->request->post('status_'.$node_project_blogger->id_blogger)
);
$log = date('Y-m-d H:i:s') . ' ' . print_r($array, true);
file_put_contents('../logs/funnel_manager.txt', PHP_EOL . $str_log . PHP_EOL . $log . PHP_EOL . $str_log, FILE_APPEND);
// ======================== END LOG CREATE =============================== //
// Массовое редактирование статусов
// BloggerForm::updateAll(['long_list_status_manager' => 0], ['id' => array('11','30','31')]);
NodeProjectBlogger::updateAll(
['long_list' => $this->request->post('status_'.$node_project_blogger->id_blogger)],
[
'id_project' => Yii::$app->request->get('project'),
'id_blogger' => $node_project_blogger->id_blogger,
//'id_blogger' => array('1','2','6')
]);
// ------ ИЗМЕНЕНИЕ БЮДЖЕТА ------ //
// Биндим всех блоггеров
foreach ($bloggers_db as $bloggers)
{
// Если статус "Согласованные"
if ($this->request->post('status_'.$bloggers->id) == 1)
{
// Выводим сумму интеграции
$data_platforms__integration_cost_agreed = DataPlatformsForm::find()
->select('integration_cost')
->where(['id_blogger' => $bloggers->id])
->all();
// Все цены блогеров в массиве
//$all_prices = array('id_blogger_'.$bloggers->id => $data_platforms__integration_cost_agreed[0]->integration_cost);
$all_prices = array($data_platforms__integration_cost_agreed[0]->integration_cost);
$summ = 0;
foreach($all_prices as $price)
{
$summ += floatval($price[0]);
}
echo $summ;
// echo '<pre>';
// var_dump($all_prices);
// echo '</pre>';
}
}
die;
}
return $this->redirect(['?project='.Yii::$app->request->get('project')]);
}
return $this->render('index', [
'bloggers_db' => $bloggers_db,
'bloggers_neutral_projects_db' => $bloggers_neutral_projects_db,
'bloggers_approved_projects_db' => $bloggers_approved_projects_db,
'bloggers_rejected_projects_db' => $bloggers_rejected_projects_db,
]);
}
我将假设有
$all_prices
一个数组,其中有两个嵌套数组,其中,在零索引下,有以字符串格式指示的价格那么总和可以计算如下:
如果要计算的价格数量超过两个,则可以循环执行:
注意#1:
表达
允许您将左侧指定的变量的值增加表达式右侧指定的值
它相当于以下内容:
笔记2:
该函数
intval()
将字符串转换为整数值。如果打算不仅有整数 - 请改用它
floatval()
- 它将字符串转换为浮点值更新