Search code examples
phploopssortingforeacharray-merge

Array merge from foreach loop in PHP


I'm making a website that uses foreach loop for the output. The output is a bunch of arrays, but it's not quite right. I used the foreach twice because it requires 2 arrays for the output. I got the arrays I want but I can't merge them. I need them to be merged because I want to sort the array based on a value.

This is the code that I use:

$counter_saw_wp = 0;
foreach($v as $key => $value){
    foreach($v_wp as $k => $val){
        $v_saw_wp[$k]["phone_id"] = $val["phone_id"];
        $v_saw_wp[$k]["phone_name"] = $val["phone_name"];
        $v_saw_wp[$k]["phone_ram"] = $val["phone_ram"];
        $v_saw_wp[$k]["phone_memory"] = $val["phone_memory"];
        $v_saw_wp[$k]["phone_img"] = $val["phone_img"];
        $v_saw_wp[$k]["v_saw"] = $value ["v"];
        $v_saw_wp[$k]["v_wp"] = $val["v_wp"];
        $v_saw_wp[$k]["v_saw_wp"] = (($value["v"] + $val["v_wp"])/2);
    }

    $v_total_saw_wp = array();
    $v_total_saw_wp[] = array_merge($v_total_saw_wp, $v_saw_wp[$counter_saw_wp]);

    $counter_saw_wp++;
    
    var_dump($v_total_saw_wp); 
}

And this is the output when I var_dump the variable:

array(1) {
  [0]=>
  array(8) {
    ["phone_id"]=>
    string(2) "26"
    ["phone_name"]=>
    string(19) "Samsung Galaxy S23+"
    ["phone_ram"]=>
    string(1) "8"
    ["phone_memory"]=>
    string(3) "256"
    ["phone_img"]=>
    string(11) "s23plus.png"
    ["v_saw"]=>
    float(45)
    ["v_wp"]=>
    float(2.7588120104614227)
    ["v_saw_wp"]=>
    float(23.879406005230713)
  }
}
array(1) {
  [0]=>
  array(8) {
    ["phone_id"]=>
    string(2) "27"
    ["phone_name"]=>
    string(24) "Samsung Galaxy S23 Ultra"
    ["phone_ram"]=>
    string(2) "12"
    ["phone_memory"]=>
    string(3) "256"
    ["phone_img"]=>
    string(12) "s23ultra.png"
    ["v_saw"]=>
    float(41.25)
    ["v_wp"]=>
    float(2.5434238896587567)
    ["v_saw_wp"]=>
    float(21.896711944829377)
  }
}
array(1) {
  [0]=>
  array(8) {
    ["phone_id"]=>
    string(2) "28"
    ["phone_name"]=>
    string(24) "Samsung Galaxy S23 Ultra"
    ["phone_ram"]=>
    string(2) "12"
    ["phone_memory"]=>
    string(3) "512"
    ["phone_img"]=>
    string(12) "s23ultra.png"
    ["v_saw"]=>
    int(40)
    ["v_wp"]=>
    float(2.475319476936885)
    ["v_saw_wp"]=>
    float(21.23765973846844)
  }
}

But if I use $v_total_saw_wp = $v_saw_wp[$counter_saw_wp]; instead of

$v_total_saw_wp = array();
$v_total_saw_wp[] = array_merge($v_total_saw_wp, $v_saw_wp[$counter_saw_wp]);

This is the output I get:

array(8) {
  ["phone_id"]=>
  string(2) "26"
  ["phone_name"]=>
  string(19) "Samsung Galaxy S23+"
  ["phone_ram"]=>
  string(1) "8"
  ["phone_memory"]=>
  string(3) "256"
  ["phone_img"]=>
  string(11) "s23plus.png"
  ["v_saw"]=>
  float(45)
  ["v_wp"]=>
  float(2.7588120104614227)
  ["v_saw_wp"]=>
  float(23.879406005230713)
}
array(8) {
  ["phone_id"]=>
  string(2) "27"
  ["phone_name"]=>
  string(24) "Samsung Galaxy S23 Ultra"
  ["phone_ram"]=>
  string(2) "12"
  ["phone_memory"]=>
  string(3) "256"
  ["phone_img"]=>
  string(12) "s23ultra.png"
  ["v_saw"]=>
  float(41.25)
  ["v_wp"]=>
  float(2.5434238896587567)
  ["v_saw_wp"]=>
  float(21.896711944829377)
}
array(8) {
  ["phone_id"]=>
  string(2) "28"
  ["phone_name"]=>
  string(24) "Samsung Galaxy S23 Ultra"
  ["phone_ram"]=>
  string(2) "12"
  ["phone_memory"]=>
  string(3) "512"
  ["phone_img"]=>
  string(12) "s23ultra.png"
  ["v_saw"]=>
  int(40)
  ["v_wp"]=>
  float(2.475319476936885)
  ["v_saw_wp"]=>
  float(21.23765973846844)
}

But it's suppose to look like this: (so I can sort it)

array(3) {  
  [0]=>
  array(8) {
    ["phone_id"]=>
    string(2) "26"
    ["phone_name"]=>
    string(19) "Samsung Galaxy S23+"
    ["phone_ram"]=>
    string(1) "8"
    ["phone_memory"]=>
    string(3) "256"
    ["phone_img"]=>
    string(11) "s23plus.png"
    ["v_saw"]=>
    float(45)
    ["v_wp"]=>
    float(2.7588120104614227)
    ["v_saw_wp"]=>
    float(23.879406005230713)
  }
  [1]=>
  array(8) {
    ["phone_id"]=>
    string(2) "27"
    ["phone_name"]=>
    string(24) "Samsung Galaxy S23 Ultra"
    ["phone_ram"]=>
    string(2) "12"
    ["phone_memory"]=>
    string(3) "256"
    ["phone_img"]=>
    string(12) "s23ultra.png"
    ["v_saw"]=>
    float(41.25)
    ["v_wp"]=>
    float(2.5434238896587567)
    ["v_saw_wp"]=>
    float(21.896711944829377)
  }
  [2]=>
  array(8) {
    ["phone_id"]=>
    string(2) "28"
    ["phone_name"]=>
    string(24) "Samsung Galaxy S23 Ultra"
    ["phone_ram"]=>
    string(2) "12"
    ["phone_memory"]=>
    string(3) "512"
    ["phone_img"]=>
    string(12) "s23ultra.png"
    ["v_saw"]=>
    int(40)
    ["v_wp"]=>
    float(2.475319476936885)
    ["v_saw_wp"]=>
    float(21.23765973846844)
  }
}

I've tried using array_merge within the foreach loop but it doesn't work. It just put each array into another array (as shown above). I've also tried using array_merge outside the foreach loop but it just takes the last array from the loop.

[EDIT] I've tried using this code (An answer by Serghei Leonenco):

$counter_saw_wp = 0;
$v_total_saw_wp = array();
foreach($v as $key => $value){
    foreach($v_wp as $k => $val){
        $v_saw_wp[$k]["phone_id"] = $val["phone_id"];
        $v_saw_wp[$k]["phone_name"] = $val["phone_name"];
        $v_saw_wp[$k]["phone_ram"] = $val["phone_ram"];
        $v_saw_wp[$k]["phone_memory"] = $val["phone_memory"];
        $v_saw_wp[$k]["phone_img"] = $val["phone_img"];
        $v_saw_wp[$k]["v_saw"] = $value ["v"];
        $v_saw_wp[$k]["v_wp"] = $val["v_wp"];
        $v_saw_wp[$k]["v_saw_wp"] = (($value["v"] + $val["v_wp"])/2);
    }

    $v_total_saw_wp[] = array_merge($v_total_saw_wp, $v_saw_wp[$counter_saw_wp]);

    $counter_saw_wp++;
    
    var_dump($v_total_saw_wp); 
}

But when I var_dump the variable, the output looks like this:

array(3) 
    { 
        [0]=> array(8) 
            { 
                ["phone_id"]=> string(2) "26" 
                ["phone_name"]=> string(19) "Samsung Galaxy S23+" 
                ["phone_ram"]=> string(1) "8" 
                ["phone_memory"]=> string(3) "256" 
                ["phone_img"]=> string(11) "s23plus.png" 
                ["v_saw"]=> float(45) 
                ["v_wp"]=> float(2.7588120104614227) 
                ["v_saw_wp"]=> float(23.879406005230713) 
            } 
        [1]=> array(9) 
            { 
                [0]=> array(8) 
                    { 
                        ["phone_id"]=> string(2) "26" 
                        ["phone_name"]=> string(19) "Samsung Galaxy S23+" 
                        ["phone_ram"]=> string(1) "8" 
                        ["phone_memory"]=> string(3) "256" 
                        ["phone_img"]=> string(11) "s23plus.png" 
                        ["v_saw"]=> float(45) 
                        ["v_wp"]=> float(2.7588120104614227) 
                        ["v_saw_wp"]=> float(23.879406005230713) 
                    } 
                ["phone_id"]=> string(2) "27" 
                ["phone_name"]=> string(24) "Samsung Galaxy S23 Ultra" 
                ["phone_ram"]=> string(2) "12" 
                ["phone_memory"]=> string(3) "256" 
                ["phone_img"]=> string(12) "s23ultra.png" 
                ["v_saw"]=> float(41.25) 
                ["v_wp"]=> float(2.5434238896587567) 
                ["v_saw_wp"]=> float(21.896711944829377) 
            } 
        [2]=> array(10) 
            { 
                [0]=> array(8) 
                    { 
                        ["phone_id"]=> string(2) "26" 
                        ["phone_name"]=> string(19) "Samsung Galaxy S23+" 
                        ["phone_ram"]=> string(1) "8" 
                        ["phone_memory"]=> string(3) "256" 
                        ["phone_img"]=> string(11) "s23plus.png" 
                        ["v_saw"]=> float(45) 
                        ["v_wp"]=> float(2.7588120104614227) 
                        ["v_saw_wp"]=> float(23.879406005230713) 
                    } 
                [1]=> array(9) 
                    { 
                        [0]=> array(8) 
                            { 
                                ["phone_id"]=> string(2) "26" 
                                ["phone_name"]=> string(19) "Samsung Galaxy S23+" 
                                ["phone_ram"]=> string(1) "8" 
                                ["phone_memory"]=> string(3) "256" 
                                ["phone_img"]=> string(11) "s23plus.png" 
                                ["v_saw"]=> float(45) 
                                ["v_wp"]=> float(2.7588120104614227) 
                                ["v_saw_wp"]=> float(23.879406005230713) 
                            } 
                        ["phone_id"]=> string(2) "27" 
                        ["phone_name"]=> string(24) "Samsung Galaxy S23 Ultra" 
                        ["phone_ram"]=> string(2) "12" 
                        ["phone_memory"]=> string(3) "256" 
                        ["phone_img"]=> string(12) "s23ultra.png" 
                        ["v_saw"]=> float(41.25) ["v_wp"]=> float(2.5434238896587567) 
                        ["v_saw_wp"]=> float(21.896711944829377) 
                    } 
                ["phone_id"]=> string(2) "28" 
                ["phone_name"]=> string(24) "Samsung Galaxy S23 Ultra" 
                ["phone_ram"]=> string(2) "12" 
                ["phone_memory"]=> string(3) "512" 
                ["phone_img"]=> string(12) "s23ultra.png" 
                ["v_saw"]=> int(40) 
                ["v_wp"]=> float(2.475319476936885) 
                ["v_saw_wp"]=> float(21.23765973846844) 
            } 
    }

I mean it's okay if the arrays are not a lot, but the problem is I have over 200 data on the database which makes the output so many and the web even crashed when I tried it.

Does anyone know how to fix it?

[ANOTHER EDIT] I've solved it! This is the code I use:


        $counter_saw_wp = 0;
        foreach($v as $key => $value){
            foreach($v_wp as $k => $val){
                $v_saw_wp[$k]["phone_id"] = $val["phone_id"];
                $v_saw_wp[$k]["phone_name"] = $val["phone_name"];
                $v_saw_wp[$k]["phone_ram"] = $val["phone_ram"];
                $v_saw_wp[$k]["phone_memory"] = $val["phone_memory"];
                $v_saw_wp[$k]["phone_img"] = $val["phone_img"];
                $v_saw_wp[$k]["v_saw"] = $value ["v"];
                $v_saw_wp[$k]["v_wp"] = $val["v_wp"];
                $v_saw_wp[$k]["v_saw_wp"] = (($value["v"] + $val["v_wp"])/2);
            }

            $v_avg_saw_wp[] = array_merge(array(), $v_saw_wp[$counter_saw_wp]);

            $counter_saw_wp++;
        }

Solution

  • Your mistake was that you define and assign array to an array. But you should define your array outside of foreach loop:

    $v_total_saw_wp = [];
    foreach($v as $key => $value){
        foreach($v_wp as $k => $val){
           $v_total_saw_wp[$key]["phone_id"] = $val["phone_id"];
           $v_total_saw_wp[$key]["phone_name"] = $val["phone_name"];
           $v_total_saw_wp[$key]["phone_ram"] = $val["phone_ram"];
           $v_total_saw_wp[$key]["phone_memory"] = $val["phone_memory"];
           $v_total_saw_wp[$key]["phone_img"] = $val["phone_img"];
           $v_total_saw_wp[$key]["v_saw"] = $value ["v"];
           $v_total_saw_wp[$key]["v_wp"] = $val["v_wp"];
           $v_total_saw_wp[$key]["v_saw_wp"] = (($value["v"] + $val["v_wp"])/2);
        }
    }
    

    This should make that worked and get you a desired result.