How to merge 2 arrays alternatively..?

Ive to merge 2 arrays value alternatively…

**Below is my PHP Code**
     $new = array();
     $arr1 = array(2015-09-24 09:10:00, 2015-09-24 09:20:00, 2015-09-24 09:30:00, 2015-09-24 09:40:00, 
     2015-09-24 09:50:00);
     $arr2 = array(8083,9158,9647,5565,6631);
     $new = array();
     for ($i=0; $i<count($arr1); $i++) {
           $new[] = $arr1[$i];
           $new[] = $arr2[$i];
      }
     var_dump($new); 

in the above code array 1 contains timestamp and array 2 contains values generated during that period of time…I want to generate arra3 which contains time followed by the value…
Ive written the code But its not working…Its just showing empty array when I try to print…

resulting array should be in the following format…

 $arr3 = array(2015-09-24 09:10:00, 8083, ........);

Your current code will never print even an empty array, due to syntax error.
While getting that fixed, it works.

Looks like you are running not the code you posted here, making your question of little to no sense.

Taking your “showing empty array” for granted, it is obvious that $arr1 is empty as well.

@colshrapnel…whats the syntax error…Ive executed above code it showed me array size as zero…
Its considering each timestamp as individual array and not all as an single array…

Post the actual code you are running or don’t waste everyone’s time.

Below is the complete code

  if(isset($_POST["dateipone"],$_POST["dateiptwo"],$_POST["ino"]))
 {
   $start = $_POST["dateipone"];
   $end = $_POST["dateiptwo"];
   $inverter = $_POST["ino"];
   $query = "SELECT timestamp, gridpowertotal FROM inverterlog where inverter = '" . $inverter . "' AND timestamp BETWEEN '" . $start . "' AND '" . $end . "'";
   
   if ($result = mysqli_prepare($con, $query))
   {
	   mysqli_stmt_execute($result);
	   mysqli_stmt_bind_result($result, $arr1, $arr2);
	   
	   while(mysqli_stmt_fetch($result))
	   {
		   //echo  nl2br("$arr1 \n $arr2 \n");
		   //$length = count($arr1);
		   //var_dump($arr1);
		   $array_combined = array();
		   for($i=0 ; $i<count($arr1);$i++)
		   {
			   $array_combined[] = $arr1[$i];
			   $array_combined[] = $arr2[$i];
		   }
		   var_dump($array_combined);
		     
	      }
   
        mysqli_stmt_close($result);
   }
  
  mysqli_close($con);
    }

timestamp is in the previously mentioned format…

Good.
Now somebody will be able to help you to sort this mess out

@colshrapnel

Below is the output im getting when I execute var_dump($array_combned)…

  array(2) { [0]=> string(1) "2" [1]=> NULL } array(2) { [0]=> string(1) "2" [1]=> NULL } array(2) { [0]=> string(1) "2" [1]=> NULL } array(2) { [0]=> string(1) "2" [1]=> NULL } array(2) { [0]=> string(1) "2" [1]=> NULL }

Assuming that both arrays have the same length:

I just make on esimple example:

$arr1 = array(1, 3, 5);
$arr2 = array(2, 4, 6);

$new = array();
for ($i=0; $i<count($arr1); $i++) {
   $new[] = $arr1[$i];
   $new[] = $arr2[$i];
}
var_dump($new);

It’s just sample of example.

Assuming he has this exact code already, your answer makes no sense.

OK, there are a number of problems here.

First, you bind the results to two flat variables, not to arrays. So your loop through the count of $arr1 is wrong, as $arr1 isn’t an array, and neither is $arr2.

Second, every time you retrieve a new row from the result set, you create a new array called $array_combined, so you blank out whatever happened last time you went around the loop. Move this line

$array_combined = array();

to before you start the while() loop. Lose the for loop as it has no point, and change these lines

$array_combined[] = $arr1[$i];
$array_combined[] = $arr2[$i];

to

$array_combined[] = $arr1;
$array_combined[] = $arr2;

As you’ve gone to the trouble of using mysqli_prepare() on your query, you might as well use a bound parameter instead of just sticking the variable in the query string as you do.

Note that is the output you get when you execute var_dump() five times - you dump the array inside the loop, and it shows the same thing (two elements only) each time because as I said earlier, you clear the array every time you get a new row from the result set.

1 Like

@droopsnoot …Sry same code i didnt notice the while …

Ive updated my while loop

Below is my updated code

      $arr1= array();
      while(mysqli_stmt_fetch($result))
      {
     array_push($arr1, "$timestamp");
         var_dump($arr1);
      }

Im getting output in the following manner

    array(1) { [0]=> string(19) "2015-09-24 09:10:00" } array(2) { [0]=> string(19) "2015-09-24 09:10:00" [1]=> string(19) "2015-09-24 09:20:00" } array(3) { [0]=> string(19) "2015-09-24 09:10:00" [1]=> string(19) "2015-09-24 09:20:00" [2]=> string(19) "2015-09-24 09:30:00" } array(4) { [0]=> string(19) "2015-09-24 09:10:00" [1]=> string(19) "2015-09-24 09:20:00" [2]=> string(19) "2015-09-24 09:30:00" [3]=> string(19) "2015-09-24 09:40:00" } array(5) { [0]=> string(19) "2015-09-24 09:10:00" [1]=> string(19) "2015-09-24 09:20:00" [2]=> string(19) "2015-09-24 09:30:00" [3]=> string(19) "2015-09-24 09:40:00" [4]=> string(19) "2015-09-24 09:50:00" }

For each iteration its creating new array and inserting the value…How can i prevent this and try to create only one array and insert the value…

What you previously posted as “the complete code” is no longer the code you have?

while(mysqli_stmt_fetch($result))
{
  //echo  nl2br("$arr1 \n $arr2 \n");
  //$length = count($arr1);
  //var_dump($arr1);
  $array_combined = array();
  for($i=0 ; $i<count($arr1);$i++)
  {

@Mittineague…Sry same code i didnt notice the while …

Ive updated my code

Below is my updated code

      $arr1= array();
      while(mysqli_stmt_fetch($result))
      {
     array_push($arr1, "$timestamp");
         var_dump($arr1);
      }

Im getting output in the following manner

    array(1) { [0]=> string(19) "2015-09-24 09:10:00" } array(2) { [0]=> string(19) "2015-09-24 09:10:00" [1]=> string(19) "2015-09-24 09:20:00" } array(3) { [0]=> string(19) "2015-09-24 09:10:00" [1]=> string(19) "2015-09-24 09:20:00" [2]=> string(19) "2015-09-24 09:30:00" } array(4) { [0]=> string(19) "2015-09-24 09:10:00" [1]=> string(19) "2015-09-24 09:20:00" [2]=> string(19) "2015-09-24 09:30:00" [3]=> string(19) "2015-09-24 09:40:00" } array(5) { [0]=> string(19) "2015-09-24 09:10:00" [1]=> string(19) "2015-09-24 09:20:00" [2]=> string(19) "2015-09-24 09:30:00" [3]=> string(19) "2015-09-24 09:40:00" [4]=> string(19) "2015-09-24 09:50:00" }

For each iteration its creating new array and inserting the value…How can i prevent this and try to create only one array and insert the value…

You are already doing that. Just stop var_dumping every intermediate result, but dump it once after the loop.

@colsharpnel…Ive moved it outside the loop …Thank you…its inserting the same value twice…

array(1) { [0]=> string(19) "2015-09-24 09:10:00" } array(2) { [0]=> string(19) "2015-09-24 09:10:00" [1]=> string(19) "2015-09-24 09:20:00" } array(3) { [0]=> string(19) "2015-09-24 09:10:00" [1]=> string(19) "2015-09-24 09:20:00" [2]=> string(19) "2015-09-24 09:30:00" } array(4) { [0]=> string(19) "2015-09-24 09:10:00" [1]=> string(19) "2015-09-24 09:20:00" [2]=> string(19) "2015-09-24 09:30:00" [3]=> string(19) "2015-09-24 09:40:00" } array(5) { [0]=> string(19) "2015-09-24 09:10:00" [1]=> string(19) "2015-09-24 09:20:00" [2]=> string(19) "2015-09-24 09:30:00" [3]=> string(19) "2015-09-24 09:40:00" [4]=> string(19) "2015-09-24 09:50:00" }

How can i prevent that…??

For the goodness sake, move var_dump outside the loop!

@colshrapnel…dont be so rude man…ive already moved var_dump out side the loop…Thank you …

Look the code below

       $arr1= array();
	   $arr2=array();
	   $array_combined = array();
	   while(mysqli_stmt_fetch($result))
	   {
		   array_push($arr1, "$timestamp");
		  array_push($arr2, "$gridpowertotal");
	   }
	   for($i=0 ; $i<count($arr1);$i++)
		   {
			   $array_combined[] = $arr1[$i];
			   $array_combined[] = $arr2[$i];
		   }  
       var_dump($array_combined);
        mysqli_stmt_close($result);

And now where did your “updated while loop” go?
Posting your old code you are insulting all the good fellows who wasted their time trying to help you.

@colsharpel…their is no need to update the while loop …and no one as mentioned that…and ive not wasted any of the good fellows time…ive learnt where I had made mistake because of their help…ive posted the code because to show you that ive moved variable outside the loop…

I don’t understand why you build $arr1 and $arr2 arrays, then run a loop through them to combine them into one. Instead of

while(mysqli_stmt_fetch($result))
	   {
		   array_push($arr1, "$timestamp");
		  array_push($arr2, "$gridpowertotal");
	   }
for($i=0 ; $i<count($arr1);$i++)
		   {
			   $array_combined[] = $arr1[$i];
			   $array_combined[] = $arr2[$i];
		   }  

why don’t you just do something like

while(mysqli_stmt_fetch($result))
	   {
		   array_push($array_combined, $timestamp);
		   array_push($array_combined, $gridpowertotal);
	   }

or even back to my earlier example

while(mysqli_stmt_fetch($result))
	   {
		   $array_combined[] = $timestamp;
		   $array_combined[] = $gridpowertotal;
	   }
var_dump($array_combined);

The only reason I can think of for making two separate arrays and then merging them into a single one is if you need to use the separate arrays as well as the combined one.