0

I tried to insert some values into an array, but I don't know why I lost some values of them

This is the code I am using right now:

<pre><code>
<?php
include 'config/connections.php';
$SQL = "SELECT * FROM table"; 
$result = $db->query($SQL) or die("Couldn't execute 
query.".mysqli_error($db)); 
$numresult=$result->num_rows;
for ($i=0; $i<$numresult; $i++)
{
    $row = $result->fetch_assoc();  
    $c['id']   = 'H'.$i;
    $c['start']= date("Y-m-d");
    $a[$i]   = $c;
    for ($j=1; $j<4; $j++)
    {
        $c['id']   = 'H'.$i;
        $c['start']= date('Y-m-d', strtotime("+".$j." days"));
       array_push($a,$c);
    }
}
echo json_encode($a);
?>
</pre></code>

Output:


    [{"id":"H0","start":"2015-05-13"},
    {"id":"H1","start":"2015-05-13"},
    {"id":"H2","start":"2015-05-13"},
    {"id":"H3","start":"2015-05-13"},
    {"id":"H0","start":"2015-05-17"},
    {"id":"H1","start":"2015-05-14"},
    {"id":"H1","start":"2015-05-15"},
    {"id":"H1","start":"2015-05-16"},
    {"id":"H1","start":"2015-05-17"},
    {"id":"H2","start":"2015-05-14"},
    {"id":"H2","start":"2015-05-15"},
    {"id":"H2","start":"2015-05-16"},
    {"id":"H2","start":"2015-05-17"},
    {"id":"H3","start":"2015-05-14"},
    {"id":"H3","start":"2015-05-15"},
    {"id":"H3","start":"2015-05-16"},
    {"id":"H3","start":"2015-05-17"}]
    
I lost some values from array:
 
    {"id":"H0","start":"2015-05-14"}, 
    {"id":"H0","start":"2015-05-15"}, 
    {"id":"H0","start":"2015-05-16"}
    
Any help in this regard is greatly appreciated!Thanks!

1
  • There is an syntax error on line with $c['start']= date('Y-m-d", strtotime("+".$j." days")); It shoulld be "Y-m-d". Commented May 13, 2015 at 13:22

1 Answer 1

2

On the first loop iteration, $a[0] is created and then the second for, adds 3 elements to the $a array, so $a[1] to $a[4] are created.

On the second external for iteration, $a[1] is overwritten and the inner for adds $a[5] to $a[7].

The third external for iteration overwrites $a[2] and so on.

You can do what you want like this:

<?php
include 'config/connections.php';
$SQL = "SELECT * FROM table"; 
$result = $db->query($SQL) or die("Couldn't execute query.".mysqli_error($db)); 
$numresult=$result->num_rows;
$a = array();
for ($i=0; $i<$numresult; $i++)
{
    $row = $result->fetch_assoc();  
    for ($j=0; $j<4; $j++)
    {
        $c['id']   = 'H'.$i;
        $c['start']= date('Y-m-d', strtotime("+".$j." days"));
       array_push($a,$c);
    }
}
echo json_encode($a);
?>
Sign up to request clarification or add additional context in comments.

3 Comments

thank you, i have tried your suggestion but it still do not work
@Candra You could also keep your code and change $a[$i] = $c; in your first for loop with array_push($a, $c);
it has worked, thank you axxis you have saved my life

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.