php array not saved in object

Christopher Chen

I do not understand fully why this PHP code is not saving the data in the arrays. when I echo out the array during the while loop, the values are set but after I exit the loop, the values in the array are missing. The other variables did contain their values after leaving the array.

<?php
    class VIEWARRAY {
        public $year;
        public $month;
        public $day = array();
        public $views = array();
    }

    $viewdatabase = array();

    $connection = MySQL_Connection($host, Session_Get("username"), Session_Get("password"), $database);

    $dayviewindex = 0; $monthyearindex = 0; $previousmonth = "";
    $information = MySQL_Script($connection, "SELECT * FROM viewdatabase");
    while ($index = mysqli_fetch_array($information)) {
        if ($index["month"] != $previousmonth) {    
        $viewdatabase[$monthyearindex] = new VIEWARRAY;
        $viewdatabase[$monthyearindex]->year = $index["year"];
        $viewdatabase[$monthyearindex]->month = $index["month"];

            $dayviewindex = 0;
        $monthyearindex++;
        $previousmonth = $index["month"];
        }

        $viewdatabase[$monthyearindex]->day[$dayviewindex] = $index["day"];
        $viewdatabase[$monthyearindex]->views[$dayviewindex] = $index["views"];
        $dayviewindex++;
    }

    MySQL_Disconnect($connection);

    //testing area
    echo "->" . $viewdatabase[0]->year . " + " . $viewdatabase[0]->month . "<br />"; //prints out ->2013 + 8
    echo "------>" . $viewdatabase[0]->day[2] . " + " . $viewdatabase[0]->views[2] . "<br />"; //prints out ------> + 
    echo count($viewdatabase[0]->views) . "<br />"; //prints out 0
    echo count($viewdatabase[1]->views) . "<br />"; //prints out 0

    ?>

I made sure that I was able to connect to my database just fine and that my database did return information. This is how my database is setup

year     month     day     views
2013     8         25      1
2013     8         26      1
2013     8         27      1
2013     9         3       1
Sergiu Paraschiv

At first run, after if ($index["month"] != $previousmonth), $monthyearindex will be 1. That means you'll next try to access $viewdatabase[1] but it's NULL. On the next iteration you won't enter the if, $monthyearindex will be 1, and $viewdatabase[1] will still be NULL. So you never actually get to set anything in day and views.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Sorting array object in php

분류에서Dev

Merge a php array and a php object

분류에서Dev

Accessing a PHP array object(stdClass) values

분류에서Dev

Turning my php array into [object Object] json/JavaScript

분류에서Dev

How do I filter an array with only valid object in php

분류에서Dev

php array of objects can't get object property (Object of class stdClass could not be converted to string)

분류에서Dev

How to access attributes of php object inside a array of php objects from javascript

분류에서Dev

Object array to generic array

분류에서Dev

How to convert a UUID that saved in an array of bytes into a string (c++)

분류에서Dev

PHP MYSQL -> Display dates saved as 'date' type from MYSQL Database

분류에서Dev

이 PHP 변수 (object-> array-> object)에 어떻게 액세스합니까?

분류에서Dev

Multidimensional array to array in PHP

분류에서Dev

Traversing Through Object + Array + Object

분류에서Dev

How to get array object in object

분류에서Dev

Accessing object in array recreating object?

분류에서Dev

PHP Accessing Values quickly an Arrray holding objects which one element of the object has an array

분류에서Dev

Cannot use object of type Illuminate\Support\Facades\Config as array in .../framework/src/Illuminate/Database/DatabaseManager.php

분류에서Dev

PHP : (array) $ object를 통해 객체를 배열로 직렬화

분류에서Dev

object_to_array 및 인코딩-PHP에서 js opbject로

분류에서Dev

Array position by object value

분류에서Dev

Add PSVariable object to array

분류에서Dev

Looping an object with an string array

분류에서Dev

Update specific object in array

분류에서Dev

can not access the array of an object

분류에서Dev

JS fill Object as an array

분류에서Dev

Pass an object into an array in JavaScript

분류에서Dev

Remove object from array

분류에서Dev

Object and array with self referencing

분류에서Dev

function to exclude array object

Related 관련 기사

  1. 1

    Sorting array object in php

  2. 2

    Merge a php array and a php object

  3. 3

    Accessing a PHP array object(stdClass) values

  4. 4

    Turning my php array into [object Object] json/JavaScript

  5. 5

    How do I filter an array with only valid object in php

  6. 6

    php array of objects can't get object property (Object of class stdClass could not be converted to string)

  7. 7

    How to access attributes of php object inside a array of php objects from javascript

  8. 8

    Object array to generic array

  9. 9

    How to convert a UUID that saved in an array of bytes into a string (c++)

  10. 10

    PHP MYSQL -> Display dates saved as 'date' type from MYSQL Database

  11. 11

    이 PHP 변수 (object-> array-> object)에 어떻게 액세스합니까?

  12. 12

    Multidimensional array to array in PHP

  13. 13

    Traversing Through Object + Array + Object

  14. 14

    How to get array object in object

  15. 15

    Accessing object in array recreating object?

  16. 16

    PHP Accessing Values quickly an Arrray holding objects which one element of the object has an array

  17. 17

    Cannot use object of type Illuminate\Support\Facades\Config as array in .../framework/src/Illuminate/Database/DatabaseManager.php

  18. 18

    PHP : (array) $ object를 통해 객체를 배열로 직렬화

  19. 19

    object_to_array 및 인코딩-PHP에서 js opbject로

  20. 20

    Array position by object value

  21. 21

    Add PSVariable object to array

  22. 22

    Looping an object with an string array

  23. 23

    Update specific object in array

  24. 24

    can not access the array of an object

  25. 25

    JS fill Object as an array

  26. 26

    Pass an object into an array in JavaScript

  27. 27

    Remove object from array

  28. 28

    Object and array with self referencing

  29. 29

    function to exclude array object

뜨겁다태그

보관