0

How do I add an array to another? I mean addition, not to add the elements at the end of the array. I have a while structure and I want it to add the values at each iteration.

Tried a few variants like $final_array += $final_array; but I couldn't find a solution and I don't really know how to spell it to find something on SO/Google.

Here is the code:

<?php
$i = 0;
while ($i < 10){
    $i++;
    $sql =  "SELECT * FROM parteneri WHERE nr_intrebare = '$i' AND varianta_raspuns = '$intrebare_01'";
    $result = mysqli_query($con, $sql);

    $final_array = array();

    while ($row = mysqli_fetch_array($result))
    {
        $final_array[] = $row;
    }
}

Edit:

To clear things up, I have a questionnaire that has 10 <select> fields. Here is an example:

<select name="intrebare_01">
    <option selected="true" disabled="disabled">Selecteaza o optiune...</option>
    <option value="A">Fotbal</option>
    <option value="B">Tenis</option>
    <option value="C">Basket/Handbal/Hockey/Volei</option>
    <option value="D">Alte sporturi</option>
</select> 

As you can see, each option has a value ranging from A to D.

I have a table called "parteneri" that contains the points of each of these values for each question. If you need the value in points of the question 4 answer B you can find it there.

The "parteneri" (partners) table has the following structure:

enter image description here

As you can see, there is a numeric value for each option for each of the seven partners.

What I need to do is to add the points each of these partners gathered after processing all of the 10 <select> and display the first three that got the most points.

Please ask if you have any questions that I didn't covered.

7
  • 2
    Can you please post an example of your data? Just an array with some entries, and how your $final_array should look like at the end? Commented Feb 10, 2017 at 9:31
  • have you tried .= Commented Feb 10, 2017 at 9:31
  • 1
    Your question is unclear. What do you mean by "add an array to an array", or "array + array"? If you do not want to add the elements to an array, then what else? You want several array contained in an outer array? Please edit your question and try to make clear, maybe giving an example, what it actually is you want to create. Commented Feb 10, 2017 at 9:31
  • Updated the question. Hope it answers your questions now :) Commented Feb 10, 2017 at 10:24
  • Still you failed to provide the desired result example. Commented Feb 10, 2017 at 10:25

4 Answers 4

2

As per as i understood you question that you want to add the values of each $final_array to another. Hope this will be helpful to u :)

<?php
    $i = 0;
    $array_sum=[];
     while ($i < 10){
      $i++;
       $sql =  "SELECT * FROM parteneri WHERE nr_intrebare = '$i' AND varianta_raspuns = '$intrebare_01'";
       $result = mysqli_query($con, $sql);

       $final_array = array();

      while ($row = mysqli_fetch_array($result))
      {
        $final_array = $row;
            $array_sum = array_map(function () {
                return array_sum(func_get_args());
            }, $array_sum, $final_array);
      }
     }
    ?>

Here have an example of this concept

<?php 
$c=[];
$b = array(array(1, 20, 11, 8, 3),
           array(10, 2, 5, 10, 0),
           array(10, 2, 5, 10, 0),
           array(10, 2, 5, 10, 0),
           array(10, 2, 5, 10, 0));

                foreach($b as $key => $value){
                    $c = array_map(function () {
                        return array_sum(func_get_args());
                    }, $c, $value);

                }


print_r($c);
?>

Output of this following example Array ( [0] => 41 [1] => 28 [2] => 31 [3] => 48 [4] => 3 )

Sign up to request clarification or add additional context in comments.

5 Comments

I applied your solution. It prints this array: Array ( [0] => 200 [1] => 200 [2] => 55 [3] => 55 [4] => 0 [5] => 0 [6] => 45 [7] => 45 [8] => 35 [9] => 35 [10] => 45 [11] => 45 [12] => 47 [13] => 47 [14] => 59 [15] => 59 [16] => 52 [17] => 52 [18] => 30 [19] => 30 ) I'm a little confused as I only have 10 columns in the database. Maybe I applied it wrong?
At first you should fetch your data and generate an multidimensional array as like the example array '$b'. Then you execute the following foreach. One note : You must avoid those column which don't contain numeric value .
Tried doing this but I failed, seems that I have a lot to catch up to when coming to arrays. Can you provide further explanation in the original answer? Thanks!
replace $final_array = $row; to $final_array = array_values($row);
Replaced it. print_r ($final_array) returns this: Array ( [0] => 37 [1] => 37 [2] => 10 [3] => 10 [4] => A [5] => A [6] => 10 [7] => 10 [8] => 8 [9] => 8 [10] => 7 [11] => 7 [12] => 8 [13] => 8 [14] => 5 [15] => 5 [16] => 8 [17] => 8 [18] => 0 [19] => 0 )
0

Try to use this code:

$i = 0;
$final_array = array();
while ($i < 10){
    $i++;
    $sql =  "SELECT * FROM parteneri WHERE nr_intrebare = '$i' AND varianta_raspuns = '$intrebare_01'";
    $result = mysqli_query($con, $sql);
    $j = 0;
    while ($row = mysqli_fetch_array($result))
    {
        $final_array[$i][$j] = $row;
        $j++;        
    }
}

Comments

0
<?php
$final_array = array();
$i = 0;
while ($i < 10){
    $i++;
    $sql =  "SELECT * FROM parteneri WHERE nr_intrebare = '$i' AND varianta_raspuns = '$intrebare_01'";
    $result = mysqli_query($con, $sql);

    $small_array = array();

    while ($row = mysqli_fetch_array($result))
    {
        $small_array[] = $row;
    }
      $final_array[] = $small_array;
}

Something like this you mean?

Comments

0

If you are trying to add the rows found by your query to $final_array, initialize the $final_array outside both loops.

You could also improve the run time and Security by using a prepared parameterised query here, prepare once and execute many times, so the database only has to compile, optimize and prepare an execution plan once rather than once per iteration.

<?php
    $sql =  "SELECT * 
            FROM parteneri 
            WHERE nr_intrebare = ? 
              AND varianta_raspuns = ?";

    $stmt = $con->prepare($sql);

    $final_array = array();
    $i = 0;

    while ($i < 10){
        $i++;
        $stmt->bind_param('is', $i, $intrebare_01);        
        $stmt->execute();

        while ($row = stmt->fetch_assoc()) {
            $final_array[] = $row;
        }
    }

    print_r($final_array);

And if you dont really need all the columns in your process, replace SELECT * with a SELECT of specific columns, this will also improve runtime and memory usage

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.