Add data from an array obtained from a WhereIn laravel 5.6

2

Good morning friends, I have this question of how to add these elements taken from several queries The result that shows me is this:

[{"s1":15},{"s2":12},{"s3":5},{"s4":7}]

What I need is to add them and then pass them to a view.

This is my driver

//Se consulta cada respuesta con funcion al ID del usuario
        $s1 = DB::table('preguntas as p')
        ->join('respuestas as r', function($on){
            $on->on('p.id_preguntas','=','r.id_preguntas');
        })
        ->join('usuario as u', function($join){
            $join->on('p.id_usu', '=', 'u.id_usu');
        })
        ->select(DB::raw('SUM(respuesta1) as s1'))
        ->where('u.id_usu','=',"1")
        ->whereIn('p.preguntas',['A','G','M'])
        ->first();
        //respuesta 2

        $s2 = DB::table('preguntas as p')
        ->join('respuestas as r', function($on){
            $on->on('p.id_preguntas','=','r.id_preguntas');
        })
        ->join('usuario as u', function($join){
            $join->on('p.id_usu', '=', 'u.id_usu');
        })

        ->where('u.id_usu','=',"1")
        ->whereIn('p.preguntas',['B','H','N'])
       // ->where('p.preguntas','=','A')
        ->select(DB::raw('SUM(respuesta2) as s2')) 
        ->first();   
        //repuesta3
         $s3 = DB::table('preguntas as p')
        ->join('respuestas as r', function($on){
            $on->on('p.id_preguntas','=','r.id_preguntas');
        })
        ->join('usuario as u', function($join){
            $join->on('p.id_usu', '=', 'u.id_usu');
        })

        ->where('u.id_usu','=',"1")
        ->whereIn('p.preguntas',['C','I','O'])
       // ->where('p.preguntas','=','A')
        ->select(DB::raw('SUM(respuesta5) as s3')) 
        ->first();   
        //respuesta4
        $s4 = DB::table('preguntas as p')
        ->join('respuestas as r', function($on){
            $on->on('p.id_preguntas','=','r.id_preguntas');
        })
        ->join('usuario as u', function($join){
            $join->on('p.id_usu', '=', 'u.id_usu');
        })

        ->where('u.id_usu','=',"1")
        ->whereIn('p.preguntas',['D','J','P'])
       // ->where('p.preguntas','=','A')
        ->select(DB::raw('SUM(respuesta4) as s4')) 
        ->first();   

        $arr = array($s1,$s2,$s3,$s4);
        //    list($a[0],$a[1],$a[2],$a[3]) = $s;
       return $arr;
        //  return $s1;

Try several ways with arrays but I have not been able to realize this sum, not if for this type of array definition it is different

I have seen that many in the arrays have it different from this one.

thanks for the help

EDIT ---

To what I refer, I am basically doing a survey system (small) but what I want is that these values that I throw are those that the user has already entered, and this same need to add, that is 15+12+5+7 and keep it in X variable, that's why in $arr = array($s1,$s2,$s3,$s4); is what gave results of all the queries already placed above and as a result this gives me [{"s1":15},{"s2":12},{"s3":5},{"s4":7}] I need help in this part to add these results (eye that are not static results)

    
asked by Dohko19 04.09.2018 в 01:46
source

2 answers

1

Since you have an array of objects and considering that you are working on PHP, you can browse and bring each object. Also get the keys dynamically through the function key() to know the corresponding value and thus accumulate in the variable $sum .

$sum = 0;

foreach ($array as $key => $object) {
    $k = key($object);
    $sum += $object->{$k};
}

echo $sum;

Thank you very much for your recommendation.

    
answered by 04.09.2018 / 03:37
source
2

You can use Object.values to get the values and then add them

var array = [{
  "s1": 15
}, {
  "s2": 12
}, {
  "s3": 5
}, {
  "s4": 7
}];
var suma = 0;
array.forEach(function(current) {
  suma += Object.values(current)[0];
})
console.log('Suma: ', suma);
    
answered by 04.09.2018 в 01:57