Hướng dẫn how to make json object to array in php? - cách tạo đối tượng json thành mảng trong php?

Tôi đã tạo ra một lớp jsonobj thô và đơn giản để sử dụng cho mã của mình. PHP không bao gồm các chức năng JSON như JavaScript/Node Do. Bạn phải lặp lại khác nhau, nhưng có thể hữu ích.

_arrName = $arrName;
        $this->_arr[$this->_arrName] = array();

    }

    function toArray(){return $this->_arr;}
    function toString(){return json_encode($this->_arr);}

    function push($newObjectElement){
        $this->_arr[$this->_arrName][] = $newObjectElement; // array[$key]=$val;
    }

    function add($key,$val){
        $this->_arr[$this->_arrName][] = array($key=>$val);
    }
}

// create an instance of the object
$jsonObj = new jsonOBJ("locations");

// add items using one of two methods
$jsonObj->push(json_decode("{\"location\":\"TestLoc1\"}",true)); // from a JSON String
$jsonObj->push(json_decode("{\"location\":\"TestLoc2\"}",true));

$jsonObj->add("location","TestLoc3"); // from key:val pairs

echo "
" . print_r($jsonObj->toArray(),1) . "
"; echo "
" . $jsonObj->toString(); ?>

Sẽ đầu ra:

Array
(
    [locations] => Array
        (
            [0] => Array
                (
                    [location] => TestLoc1
                )

            [1] => Array
                (
                    [location] => TestLoc2
                )

            [2] => Array
                (
                    [location] => TestLoc3
                )

        )

)


{"locations":[{"location":"TestLoc1"},{"location":"TestLoc2"},{"location":"TestLoc3"}]}

Để lặp lại, chuyển đổi sang một đối tượng bình thường:

$myObj = $jsonObj->toArray();

Then:

foreach($myObj["locations"] as $locationObj){
    echo $locationObj["location"] ."
"; }

Outputs:

Testloc1 Testloc2 Testloc3
TestLoc2
TestLoc3

Truy cập trực tiếp:

$location = $myObj["locations"][0]["location"];
$location = $myObj["locations"][1]["location"];

Một ví dụ thực tế:

// return a JSON Object (jsonOBJ) from the rows
    function ParseRowsAsJSONObject($arrName, $rowRS){
        $jsonArr = new jsonOBJ($arrName); // name of the json array

        $rows = mysqli_num_rows($rowRS);
        if($rows > 0){
            while($rows > 0){
                $rd = mysqli_fetch_assoc($rowRS);
                $jsonArr->push($rd);
                $rows--;
            }
            mysqli_free_result($rowRS);
        }
        return $jsonArr->toArray();
    }

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Cải thiện bài viết

    Lưu bài viết

    Đọc

    Bàn luận Arrays in PHP is a type of data structure that allows us to store multiple elements of similar data type under a single variable thereby saving us the effort of creating a different variable for every data. The arrays are helpful to create a list of elements of similar types, which can be accessed using their index or key. An array is created using an array() function in PHP. There are 3 types of array in PHP that are listed below:

    • Trong bài viết này, chúng ta sẽ xem cách tạo một mảng cho JSON trong PHP, và sẽ thấy việc triển khai của nó thông qua các ví dụ.
    • Mảng: Mảng trong PHP là một loại cấu trúc dữ liệu cho phép chúng tôi lưu trữ nhiều yếu tố của loại dữ liệu tương tự trong một biến duy nhất do đó tiết kiệm cho chúng tôi nỗ lực tạo một biến khác nhau cho mỗi dữ liệu. Các mảng rất hữu ích để tạo một danh sách các yếu tố của các loại tương tự, có thể được truy cập bằng chỉ mục hoặc khóa của chúng. Một mảng được tạo bằng hàm mảng () trong PHP. Có 3 loại mảng trong PHP được liệt kê dưới đây:
    • Mảng được lập chỉ mục: Nó là một mảng có khóa số. Về cơ bản, nó là một mảng trong đó mỗi khóa được liên kết với giá trị cụ thể của riêng nó.

    Mảng liên kết: Nó được sử dụng để lưu trữ các cặp giá trị khóa.

    Mảng đa chiều: Đây là một loại mảng lưu trữ một mảng khác ở mỗi chỉ mục thay vì một phần tử duy nhất. Nói cách khác, định nghĩa các mảng đa chiều là mảng mảng. & Nbsp; The below example is to convert an array into JSON. 

    PHP

      ____10

    Array
    (
        [locations] => Array
            (
                [0] => Array
                    (
                        [location] => TestLoc1
                    )
    
                [1] => Array
                    (
                        [location] => TestLoc2
                    )
    
                [2] => Array
                    (
                        [location] => TestLoc3
                    )
    
            )
    
    )
    
    
    {"locations":[{"location":"TestLoc1"},{"location":"TestLoc2"},{"location":"TestLoc3"}]}
    
    1
    Array
    (
        [locations] => Array
            (
                [0] => Array
                    (
                        [location] => TestLoc1
                    )
    
                [1] => Array
                    (
                        [location] => TestLoc2
                    )
    
                [2] => Array
                    (
                        [location] => TestLoc3
                    )
    
            )
    
    )
    
    
    {"locations":[{"location":"TestLoc1"},{"location":"TestLoc2"},{"location":"TestLoc3"}]}
    
    2
    Array
    (
        [locations] => Array
            (
                [0] => Array
                    (
                        [location] => TestLoc1
                    )
    
                [1] => Array
                    (
                        [location] => TestLoc2
                    )
    
                [2] => Array
                    (
                        [location] => TestLoc3
                    )
    
            )
    
    )
    
    
    {"locations":[{"location":"TestLoc1"},{"location":"TestLoc2"},{"location":"TestLoc3"}]}
    
    3

    Array
    (
        [locations] => Array
            (
                [0] => Array
                    (
                        [location] => TestLoc1
                    )
    
                [1] => Array
                    (
                        [location] => TestLoc2
                    )
    
                [2] => Array
                    (
                        [location] => TestLoc3
                    )
    
            )
    
    )
    
    
    {"locations":[{"location":"TestLoc1"},{"location":"TestLoc2"},{"location":"TestLoc3"}]}
    
    4
    Array
    (
        [locations] => Array
            (
                [0] => Array
                    (
                        [location] => TestLoc1
                    )
    
                [1] => Array
                    (
                        [location] => TestLoc2
                    )
    
                [2] => Array
                    (
                        [location] => TestLoc3
                    )
    
            )
    
    )
    
    
    {"locations":[{"location":"TestLoc1"},{"location":"TestLoc2"},{"location":"TestLoc3"}]}
    
    2
    Array
    (
        [locations] => Array
            (
                [0] => Array
                    (
                        [location] => TestLoc1
                    )
    
                [1] => Array
                    (
                        [location] => TestLoc2
                    )
    
                [2] => Array
                    (
                        [location] => TestLoc3
                    )
    
            )
    
    )
    
    
    {"locations":[{"location":"TestLoc1"},{"location":"TestLoc2"},{"location":"TestLoc3"}]}
    
    3

    Array
    (
        [locations] => Array
            (
                [0] => Array
                    (
                        [location] => TestLoc1
                    )
    
                [1] => Array
                    (
                        [location] => TestLoc2
                    )
    
                [2] => Array
                    (
                        [location] => TestLoc3
                    )
    
            )
    
    )
    
    
    {"locations":[{"location":"TestLoc1"},{"location":"TestLoc2"},{"location":"TestLoc3"}]}
    
    7
    {"first":{"id":1,"product_name":"Doorbell","cost":199},
    "second":{"id":2,"product_name":"Bottle","cost":99},
    "third":{"id":3,"product_name":"Washing Machine","cost":7999}}
    6
    Array
    (
        [locations] => Array
            (
                [0] => Array
                    (
                        [location] => TestLoc1
                    )
    
                [1] => Array
                    (
                        [location] => TestLoc2
                    )
    
                [2] => Array
                    (
                        [location] => TestLoc3
                    )
    
            )
    
    )
    
    
    {"locations":[{"location":"TestLoc1"},{"location":"TestLoc2"},{"location":"TestLoc3"}]}
    
    13

      

    // return a JSON Object (jsonOBJ) from the rows
        function ParseRowsAsJSONObject($arrName, $rowRS){
            $jsonArr = new jsonOBJ($arrName); // name of the json array
    
            $rows = mysqli_num_rows($rowRS);
            if($rows > 0){
                while($rows > 0){
                    $rd = mysqli_fetch_assoc($rowRS);
                    $jsonArr->push($rd);
                    $rows--;
                }
                mysqli_free_result($rowRS);
            }
            return $jsonArr->toArray();
        }
    
    9
    [{"name":"Pankaj Singh","age":"20"},
    {"name":"Arun Yadav","age":"21"},
    {"name":"Apeksha Jaiswal","age":"20"}]
    0
    Array
    (
        [locations] => Array
            (
                [0] => Array
                    (
                        [location] => TestLoc1
                    )
    
                [1] => Array
                    (
                        [location] => TestLoc2
                    )
    
                [2] => Array
                    (
                        [location] => TestLoc3
                    )
    
            )
    
    )
    
    
    {"locations":[{"location":"TestLoc1"},{"location":"TestLoc2"},{"location":"TestLoc3"}]}
    
    0
    // return a JSON Object (jsonOBJ) from the rows
        function ParseRowsAsJSONObject($arrName, $rowRS){
            $jsonArr = new jsonOBJ($arrName); // name of the json array
    
            $rows = mysqli_num_rows($rowRS);
            if($rows > 0){
                while($rows > 0){
                    $rd = mysqli_fetch_assoc($rowRS);
                    $jsonArr->push($rd);
                    $rows--;
                }
                mysqli_free_result($rowRS);
            }
            return $jsonArr->toArray();
        }
    
    7

    Array
    (
        [locations] => Array
            (
                [0] => Array
                    (
                        [location] => TestLoc1
                    )
    
                [1] => Array
                    (
                        [location] => TestLoc2
                    )
    
                [2] => Array
                    (
                        [location] => TestLoc3
                    )
    
            )
    
    )
    
    
    {"locations":[{"location":"TestLoc1"},{"location":"TestLoc2"},{"location":"TestLoc3"}]}
    
    4
    $myObj = $jsonObj->toArray();
    
    7

    Array
    (
        [locations] => Array
            (
                [0] => Array
                    (
                        [location] => TestLoc1
                    )
    
                [1] => Array
                    (
                        [location] => TestLoc2
                    )
    
                [2] => Array
                    (
                        [location] => TestLoc3
                    )
    
            )
    
    )
    
    
    {"locations":[{"location":"TestLoc1"},{"location":"TestLoc2"},{"location":"TestLoc3"}]}
    
    4
    Array
    (
        [locations] => Array
            (
                [0] => Array
                    (
                        [location] => TestLoc1
                    )
    
                [1] => Array
                    (
                        [location] => TestLoc2
                    )
    
                [2] => Array
                    (
                        [location] => TestLoc3
                    )
    
            )
    
    )
    
    
    {"locations":[{"location":"TestLoc1"},{"location":"TestLoc2"},{"location":"TestLoc3"}]}
    
    2
    Array
    (
        [locations] => Array
            (
                [0] => Array
                    (
                        [location] => TestLoc1
                    )
    
                [1] => Array
                    (
                        [location] => TestLoc2
                    )
    
                [2] => Array
                    (
                        [location] => TestLoc3
                    )
    
            )
    
    )
    
    
    {"locations":[{"location":"TestLoc1"},{"location":"TestLoc2"},{"location":"TestLoc3"}]}
    
    3

    Cải thiện bài viết

    Lưu bài viết

    Array
    (
        [locations] => Array
            (
                [0] => Array
                    (
                        [location] => TestLoc1
                    )
    
                [1] => Array
                    (
                        [location] => TestLoc2
                    )
    
                [2] => Array
                    (
                        [location] => TestLoc3
                    )
    
            )
    
    )
    
    
    {"locations":[{"location":"TestLoc1"},{"location":"TestLoc2"},{"location":"TestLoc3"}]}
    
    4
    $myObj = $jsonObj->toArray();
    
    7

    Array
    (
        [locations] => Array
            (
                [0] => Array
                    (
                        [location] => TestLoc1
                    )
    
                [1] => Array
                    (
                        [location] => TestLoc2
                    )
    
                [2] => Array
                    (
                        [location] => TestLoc3
                    )
    
            )
    
    )
    
    
    {"locations":[{"location":"TestLoc1"},{"location":"TestLoc2"},{"location":"TestLoc3"}]}
    
    4
    Array
    (
        [locations] => Array
            (
                [0] => Array
                    (
                        [location] => TestLoc1
                    )
    
                [1] => Array
                    (
                        [location] => TestLoc2
                    )
    
                [2] => Array
                    (
                        [location] => TestLoc3
                    )
    
            )
    
    )
    
    
    {"locations":[{"location":"TestLoc1"},{"location":"TestLoc2"},{"location":"TestLoc3"}]}
    
    2
    Array
    (
        [locations] => Array
            (
                [0] => Array
                    (
                        [location] => TestLoc1
                    )
    
                [1] => Array
                    (
                        [location] => TestLoc2
                    )
    
                [2] => Array
                    (
                        [location] => TestLoc3
                    )
    
            )
    
    )
    
    
    {"locations":[{"location":"TestLoc1"},{"location":"TestLoc2"},{"location":"TestLoc3"}]}
    
    3

    Đọc

      

    // return a JSON Object (jsonOBJ) from the rows
        function ParseRowsAsJSONObject($arrName, $rowRS){
            $jsonArr = new jsonOBJ($arrName); // name of the json array
    
            $rows = mysqli_num_rows($rowRS);
            if($rows > 0){
                while($rows > 0){
                    $rd = mysqli_fetch_assoc($rowRS);
                    $jsonArr->push($rd);
                    $rows--;
                }
                mysqli_free_result($rowRS);
            }
            return $jsonArr->toArray();
        }
    
    9
    [{"name":"Pankaj Singh","age":"20"},
    {"name":"Arun Yadav","age":"21"},
    {"name":"Apeksha Jaiswal","age":"20"}]
    0
    Array
    (
        [locations] => Array
            (
                [0] => Array
                    (
                        [location] => TestLoc1
                    )
    
                [1] => Array
                    (
                        [location] => TestLoc2
                    )
    
                [2] => Array
                    (
                        [location] => TestLoc3
                    )
    
            )
    
    )
    
    
    {"locations":[{"location":"TestLoc1"},{"location":"TestLoc2"},{"location":"TestLoc3"}]}
    
    0
    // return a JSON Object (jsonOBJ) from the rows
        function ParseRowsAsJSONObject($arrName, $rowRS){
            $jsonArr = new jsonOBJ($arrName); // name of the json array
    
            $rows = mysqli_num_rows($rowRS);
            if($rows > 0){
                while($rows > 0){
                    $rd = mysqli_fetch_assoc($rowRS);
                    $jsonArr->push($rd);
                    $rows--;
                }
                mysqli_free_result($rowRS);
            }
            return $jsonArr->toArray();
        }
    
    7

    Array
    (
        [locations] => Array
            (
                [0] => Array
                    (
                        [location] => TestLoc1
                    )
    
                [1] => Array
                    (
                        [location] => TestLoc2
                    )
    
                [2] => Array
                    (
                        [location] => TestLoc3
                    )
    
            )
    
    )
    
    
    {"locations":[{"location":"TestLoc1"},{"location":"TestLoc2"},{"location":"TestLoc3"}]}
    
    4
    // return a JSON Object (jsonOBJ) from the rows
        function ParseRowsAsJSONObject($arrName, $rowRS){
            $jsonArr = new jsonOBJ($arrName); // name of the json array
    
            $rows = mysqli_num_rows($rowRS);
            if($rows > 0){
                while($rows > 0){
                    $rd = mysqli_fetch_assoc($rowRS);
                    $jsonArr->push($rd);
                    $rows--;
                }
                mysqli_free_result($rowRS);
            }
            return $jsonArr->toArray();
        }
    
    5

      

    // return a JSON Object (jsonOBJ) from the rows
        function ParseRowsAsJSONObject($arrName, $rowRS){
            $jsonArr = new jsonOBJ($arrName); // name of the json array
    
            $rows = mysqli_num_rows($rowRS);
            if($rows > 0){
                while($rows > 0){
                    $rd = mysqli_fetch_assoc($rowRS);
                    $jsonArr->push($rd);
                    $rows--;
                }
                mysqli_free_result($rowRS);
            }
            return $jsonArr->toArray();
        }
    
    7

      

    // return a JSON Object (jsonOBJ) from the rows
        function ParseRowsAsJSONObject($arrName, $rowRS){
            $jsonArr = new jsonOBJ($arrName); // name of the json array
    
            $rows = mysqli_num_rows($rowRS);
            if($rows > 0){
                while($rows > 0){
                    $rd = mysqli_fetch_assoc($rowRS);
                    $jsonArr->push($rd);
                    $rows--;
                }
                mysqli_free_result($rowRS);
            }
            return $jsonArr->toArray();
        }
    
    9
    [{"name":"Pankaj Singh","age":"20"},
    {"name":"Arun Yadav","age":"21"},
    {"name":"Apeksha Jaiswal","age":"20"}]
    0
    Array
    (
        [locations] => Array
            (
                [0] => Array
                    (
                        [location] => TestLoc1
                    )
    
                [1] => Array
                    (
                        [location] => TestLoc2
                    )
    
                [2] => Array
                    (
                        [location] => TestLoc3
                    )
    
            )
    
    )
    
    
    {"locations":[{"location":"TestLoc1"},{"location":"TestLoc2"},{"location":"TestLoc3"}]}
    
    0
    // return a JSON Object (jsonOBJ) from the rows
        function ParseRowsAsJSONObject($arrName, $rowRS){
            $jsonArr = new jsonOBJ($arrName); // name of the json array
    
            $rows = mysqli_num_rows($rowRS);
            if($rows > 0){
                while($rows > 0){
                    $rd = mysqli_fetch_assoc($rowRS);
                    $jsonArr->push($rd);
                    $rows--;
                }
                mysqli_free_result($rowRS);
            }
            return $jsonArr->toArray();
        }
    
    7

    [{"name":"Pankaj Singh","age":"20"},
    {"name":"Arun Yadav","age":"21"},
    {"name":"Apeksha Jaiswal","age":"20"}]
    3

    Output:

    [{"name":"Pankaj Singh","age":"20"},
    {"name":"Arun Yadav","age":"21"},
    {"name":"Apeksha Jaiswal","age":"20"}]

    Cải thiện bài viết This example illustrates the conversion of the 2D associative array into JSON.

    PHP

      ____10

    Array
    (
        [locations] => Array
            (
                [0] => Array
                    (
                        [location] => TestLoc1
                    )
    
                [1] => Array
                    (
                        [location] => TestLoc2
                    )
    
                [2] => Array
                    (
                        [location] => TestLoc3
                    )
    
            )
    
    )
    
    
    {"locations":[{"location":"TestLoc1"},{"location":"TestLoc2"},{"location":"TestLoc3"}]}
    
    1
    Array
    (
        [locations] => Array
            (
                [0] => Array
                    (
                        [location] => TestLoc1
                    )
    
                [1] => Array
                    (
                        [location] => TestLoc2
                    )
    
                [2] => Array
                    (
                        [location] => TestLoc3
                    )
    
            )
    
    )
    
    
    {"locations":[{"location":"TestLoc1"},{"location":"TestLoc2"},{"location":"TestLoc3"}]}
    
    2
    Array
    (
        [locations] => Array
            (
                [0] => Array
                    (
                        [location] => TestLoc1
                    )
    
                [1] => Array
                    (
                        [location] => TestLoc2
                    )
    
                [2] => Array
                    (
                        [location] => TestLoc3
                    )
    
            )
    
    )
    
    
    {"locations":[{"location":"TestLoc1"},{"location":"TestLoc2"},{"location":"TestLoc3"}]}
    
    3

    Array
    (
        [locations] => Array
            (
                [0] => Array
                    (
                        [location] => TestLoc1
                    )
    
                [1] => Array
                    (
                        [location] => TestLoc2
                    )
    
                [2] => Array
                    (
                        [location] => TestLoc3
                    )
    
            )
    
    )
    
    
    {"locations":[{"location":"TestLoc1"},{"location":"TestLoc2"},{"location":"TestLoc3"}]}
    
    4
    {"first":{"id":1,"product_name":"Doorbell","cost":199},
    "second":{"id":2,"product_name":"Bottle","cost":99},
    "third":{"id":3,"product_name":"Washing Machine","cost":7999}}
    1
    {"first":{"id":1,"product_name":"Doorbell","cost":199},
    "second":{"id":2,"product_name":"Bottle","cost":99},
    "third":{"id":3,"product_name":"Washing Machine","cost":7999}}
    2
    Array
    (
        [locations] => Array
            (
                [0] => Array
                    (
                        [location] => TestLoc1
                    )
    
                [1] => Array
                    (
                        [location] => TestLoc2
                    )
    
                [2] => Array
                    (
                        [location] => TestLoc3
                    )
    
            )
    
    )
    
    
    {"locations":[{"location":"TestLoc1"},{"location":"TestLoc2"},{"location":"TestLoc3"}]}
    
    2
    Array
    (
        [locations] => Array
            (
                [0] => Array
                    (
                        [location] => TestLoc1
                    )
    
                [1] => Array
                    (
                        [location] => TestLoc2
                    )
    
                [2] => Array
                    (
                        [location] => TestLoc3
                    )
    
            )
    
    )
    
    
    {"locations":[{"location":"TestLoc1"},{"location":"TestLoc2"},{"location":"TestLoc3"}]}
    
    3

    Array
    (
        [locations] => Array
            (
                [0] => Array
                    (
                        [location] => TestLoc1
                    )
    
                [1] => Array
                    (
                        [location] => TestLoc2
                    )
    
                [2] => Array
                    (
                        [location] => TestLoc3
                    )
    
            )
    
    )
    
    
    {"locations":[{"location":"TestLoc1"},{"location":"TestLoc2"},{"location":"TestLoc3"}]}
    
    7
    {"first":{"id":1,"product_name":"Doorbell","cost":199},
    "second":{"id":2,"product_name":"Bottle","cost":99},
    "third":{"id":3,"product_name":"Washing Machine","cost":7999}}
    6
    {"first":{"id":1,"product_name":"Doorbell","cost":199},
    "second":{"id":2,"product_name":"Bottle","cost":99},
    "third":{"id":3,"product_name":"Washing Machine","cost":7999}}
    7

    Array
    (
        [locations] => Array
            (
                [0] => Array
                    (
                        [location] => TestLoc1
                    )
    
                [1] => Array
                    (
                        [location] => TestLoc2
                    )
    
                [2] => Array
                    (
                        [location] => TestLoc3
                    )
    
            )
    
    )
    
    
    {"locations":[{"location":"TestLoc1"},{"location":"TestLoc2"},{"location":"TestLoc3"}]}
    
    7
    {"first":{"id":1,"product_name":"Doorbell","cost":199},
    "second":{"id":2,"product_name":"Bottle","cost":99},
    "third":{"id":3,"product_name":"Washing Machine","cost":7999}}
    9
    {"first":{"id":1,"product_name":"Doorbell","cost":199},
    "second":{"id":2,"product_name":"Bottle","cost":99},
    "third":{"id":3,"product_name":"Washing Machine","cost":7999}}
    21
    $myObj = $jsonObj->toArray();
    
    1

    Array
    (
        [locations] => Array
            (
                [0] => Array
                    (
                        [location] => TestLoc1
                    )
    
                [1] => Array
                    (
                        [location] => TestLoc2
                    )
    
                [2] => Array
                    (
                        [location] => TestLoc3
                    )
    
            )
    
    )
    
    
    {"locations":[{"location":"TestLoc1"},{"location":"TestLoc2"},{"location":"TestLoc3"}]}
    
    745

    Array
    (
        [locations] => Array
            (
                [0] => Array
                    (
                        [location] => TestLoc1
                    )
    
                [1] => Array
                    (
                        [location] => TestLoc2
                    )
    
                [2] => Array
                    (
                        [location] => TestLoc3
                    )
    
            )
    
    )
    
    
    {"locations":[{"location":"TestLoc1"},{"location":"TestLoc2"},{"location":"TestLoc3"}]}
    
    4
    $myObj = $jsonObj->toArray();
    
    7

    Array
    (
        [locations] => Array
            (
                [0] => Array
                    (
                        [location] => TestLoc1
                    )
    
                [1] => Array
                    (
                        [location] => TestLoc2
                    )
    
                [2] => Array
                    (
                        [location] => TestLoc3
                    )
    
            )
    
    )
    
    
    {"locations":[{"location":"TestLoc1"},{"location":"TestLoc2"},{"location":"TestLoc3"}]}
    
    49
    {"first":{"id":1,"product_name":"Doorbell","cost":199},
    "second":{"id":2,"product_name":"Bottle","cost":99},
    "third":{"id":3,"product_name":"Washing Machine","cost":7999}}
    2
    Array
    (
        [locations] => Array
            (
                [0] => Array
                    (
                        [location] => TestLoc1
                    )
    
                [1] => Array
                    (
                        [location] => TestLoc2
                    )
    
                [2] => Array
                    (
                        [location] => TestLoc3
                    )
    
            )
    
    )
    
    
    {"locations":[{"location":"TestLoc1"},{"location":"TestLoc2"},{"location":"TestLoc3"}]}
    
    2
    Array
    (
        [locations] => Array
            (
                [0] => Array
                    (
                        [location] => TestLoc1
                    )
    
                [1] => Array
                    (
                        [location] => TestLoc2
                    )
    
                [2] => Array
                    (
                        [location] => TestLoc3
                    )
    
            )
    
    )
    
    
    {"locations":[{"location":"TestLoc1"},{"location":"TestLoc2"},{"location":"TestLoc3"}]}
    
    3

    Array
    (
        [locations] => Array
            (
                [0] => Array
                    (
                        [location] => TestLoc1
                    )
    
                [1] => Array
                    (
                        [location] => TestLoc2
                    )
    
                [2] => Array
                    (
                        [location] => TestLoc3
                    )
    
            )
    
    )
    
    
    {"locations":[{"location":"TestLoc1"},{"location":"TestLoc2"},{"location":"TestLoc3"}]}
    
    7
    {"first":{"id":1,"product_name":"Doorbell","cost":199},
    "second":{"id":2,"product_name":"Bottle","cost":99},
    "third":{"id":3,"product_name":"Washing Machine","cost":7999}}
    6  5

    Array
    (
        [locations] => Array
            (
                [0] => Array
                    (
                        [location] => TestLoc1
                    )
    
                [1] => Array
                    (
                        [location] => TestLoc2
                    )
    
                [2] => Array
                    (
                        [location] => TestLoc3
                    )
    
            )
    
    )
    
    
    {"locations":[{"location":"TestLoc1"},{"location":"TestLoc2"},{"location":"TestLoc3"}]}
    
    7
    {"first":{"id":1,"product_name":"Doorbell","cost":199},
    "second":{"id":2,"product_name":"Bottle","cost":99},
    "third":{"id":3,"product_name":"Washing Machine","cost":7999}}
    9
    {"first":{"id":1,"product_name":"Doorbell","cost":199},
    "second":{"id":2,"product_name":"Bottle","cost":99},
    "third":{"id":3,"product_name":"Washing Machine","cost":7999}}
    2  9
    $myObj = $jsonObj->toArray();
    
    1

    Array
    (
        [locations] => Array
            (
                [0] => Array
                    (
                        [location] => TestLoc1
                    )
    
                [1] => Array
                    (
                        [location] => TestLoc2
                    )
    
                [2] => Array
                    (
                        [location] => TestLoc3
                    )
    
            )
    
    )
    
    
    {"locations":[{"location":"TestLoc1"},{"location":"TestLoc2"},{"location":"TestLoc3"}]}
    
    74
    Array
    (
        [locations] => Array
            (
                [0] => Array
                    (
                        [location] => TestLoc1
                    )
    
                [1] => Array
                    (
                        [location] => TestLoc2
                    )
    
                [2] => Array
                    (
                        [location] => TestLoc3
                    )
    
            )
    
    )
    
    
    {"locations":[{"location":"TestLoc1"},{"location":"TestLoc2"},{"location":"TestLoc3"}]}
    
    03

    Array
    (
        [locations] => Array
            (
                [0] => Array
                    (
                        [location] => TestLoc1
                    )
    
                [1] => Array
                    (
                        [location] => TestLoc2
                    )
    
                [2] => Array
                    (
                        [location] => TestLoc3
                    )
    
            )
    
    )
    
    
    {"locations":[{"location":"TestLoc1"},{"location":"TestLoc2"},{"location":"TestLoc3"}]}
    
    4
    $myObj = $jsonObj->toArray();
    
    7

    Array
    (
        [locations] => Array
            (
                [0] => Array
                    (
                        [location] => TestLoc1
                    )
    
                [1] => Array
                    (
                        [location] => TestLoc2
                    )
    
                [2] => Array
                    (
                        [location] => TestLoc3
                    )
    
            )
    
    )
    
    
    {"locations":[{"location":"TestLoc1"},{"location":"TestLoc2"},{"location":"TestLoc3"}]}
    
    4
    Array
    (
        [locations] => Array
            (
                [0] => Array
                    (
                        [location] => TestLoc1
                    )
    
                [1] => Array
                    (
                        [location] => TestLoc2
                    )
    
                [2] => Array
                    (
                        [location] => TestLoc3
                    )
    
            )
    
    )
    
    
    {"locations":[{"location":"TestLoc1"},{"location":"TestLoc2"},{"location":"TestLoc3"}]}
    
    07
    {"first":{"id":1,"product_name":"Doorbell","cost":199},
    "second":{"id":2,"product_name":"Bottle","cost":99},
    "third":{"id":3,"product_name":"Washing Machine","cost":7999}}
    2
    Array
    (
        [locations] => Array
            (
                [0] => Array
                    (
                        [location] => TestLoc1
                    )
    
                [1] => Array
                    (
                        [location] => TestLoc2
                    )
    
                [2] => Array
                    (
                        [location] => TestLoc3
                    )
    
            )
    
    )
    
    
    {"locations":[{"location":"TestLoc1"},{"location":"TestLoc2"},{"location":"TestLoc3"}]}
    
    2
    Array
    (
        [locations] => Array
            (
                [0] => Array
                    (
                        [location] => TestLoc1
                    )
    
                [1] => Array
                    (
                        [location] => TestLoc2
                    )
    
                [2] => Array
                    (
                        [location] => TestLoc3
                    )
    
            )
    
    )
    
    
    {"locations":[{"location":"TestLoc1"},{"location":"TestLoc2"},{"location":"TestLoc3"}]}
    
    3

    Array
    (
        [locations] => Array
            (
                [0] => Array
                    (
                        [location] => TestLoc1
                    )
    
                [1] => Array
                    (
                        [location] => TestLoc2
                    )
    
                [2] => Array
                    (
                        [location] => TestLoc3
                    )
    
            )
    
    )
    
    
    {"locations":[{"location":"TestLoc1"},{"location":"TestLoc2"},{"location":"TestLoc3"}]}
    
    7
    {"first":{"id":1,"product_name":"Doorbell","cost":199},
    "second":{"id":2,"product_name":"Bottle","cost":99},
    "third":{"id":3,"product_name":"Washing Machine","cost":7999}}
    6
    Array
    (
        [locations] => Array
            (
                [0] => Array
                    (
                        [location] => TestLoc1
                    )
    
                [1] => Array
                    (
                        [location] => TestLoc2
                    )
    
                [2] => Array
                    (
                        [location] => TestLoc3
                    )
    
            )
    
    )
    
    
    {"locations":[{"location":"TestLoc1"},{"location":"TestLoc2"},{"location":"TestLoc3"}]}
    
    13

    Array
    (
        [locations] => Array
            (
                [0] => Array
                    (
                        [location] => TestLoc1
                    )
    
                [1] => Array
                    (
                        [location] => TestLoc2
                    )
    
                [2] => Array
                    (
                        [location] => TestLoc3
                    )
    
            )
    
    )
    
    
    {"locations":[{"location":"TestLoc1"},{"location":"TestLoc2"},{"location":"TestLoc3"}]}
    
    7
    {"first":{"id":1,"product_name":"Doorbell","cost":199},
    "second":{"id":2,"product_name":"Bottle","cost":99},
    "third":{"id":3,"product_name":"Washing Machine","cost":7999}}
    9
    {"first":{"id":1,"product_name":"Doorbell","cost":199},
    "second":{"id":2,"product_name":"Bottle","cost":99},
    "third":{"id":3,"product_name":"Washing Machine","cost":7999}}
    2
    Array
    (
        [locations] => Array
            (
                [0] => Array
                    (
                        [location] => TestLoc1
                    )
    
                [1] => Array
                    (
                        [location] => TestLoc2
                    )
    
                [2] => Array
                    (
                        [location] => TestLoc3
                    )
    
            )
    
    )
    
    
    {"locations":[{"location":"TestLoc1"},{"location":"TestLoc2"},{"location":"TestLoc3"}]}
    
    17
    $myObj = $jsonObj->toArray();
    
    1

    Array
    (
        [locations] => Array
            (
                [0] => Array
                    (
                        [location] => TestLoc1
                    )
    
                [1] => Array
                    (
                        [location] => TestLoc2
                    )
    
                [2] => Array
                    (
                        [location] => TestLoc3
                    )
    
            )
    
    )
    
    
    {"locations":[{"location":"TestLoc1"},{"location":"TestLoc2"},{"location":"TestLoc3"}]}
    
    74
    Array
    (
        [locations] => Array
            (
                [0] => Array
                    (
                        [location] => TestLoc1
                    )
    
                [1] => Array
                    (
                        [location] => TestLoc2
                    )
    
                [2] => Array
                    (
                        [location] => TestLoc3
                    )
    
            )
    
    )
    
    
    {"locations":[{"location":"TestLoc1"},{"location":"TestLoc2"},{"location":"TestLoc3"}]}
    
    21

    Array
    (
        [locations] => Array
            (
                [0] => Array
                    (
                        [location] => TestLoc1
                    )
    
                [1] => Array
                    (
                        [location] => TestLoc2
                    )
    
                [2] => Array
                    (
                        [location] => TestLoc3
                    )
    
            )
    
    )
    
    
    {"locations":[{"location":"TestLoc1"},{"location":"TestLoc2"},{"location":"TestLoc3"}]}
    
    4
    // return a JSON Object (jsonOBJ) from the rows
        function ParseRowsAsJSONObject($arrName, $rowRS){
            $jsonArr = new jsonOBJ($arrName); // name of the json array
    
            $rows = mysqli_num_rows($rowRS);
            if($rows > 0){
                while($rows > 0){
                    $rd = mysqli_fetch_assoc($rowRS);
                    $jsonArr->push($rd);
                    $rows--;
                }
                mysqli_free_result($rowRS);
            }
            return $jsonArr->toArray();
        }
    
    5

      

    // return a JSON Object (jsonOBJ) from the rows
        function ParseRowsAsJSONObject($arrName, $rowRS){
            $jsonArr = new jsonOBJ($arrName); // name of the json array
    
            $rows = mysqli_num_rows($rowRS);
            if($rows > 0){
                while($rows > 0){
                    $rd = mysqli_fetch_assoc($rowRS);
                    $jsonArr->push($rd);
                    $rows--;
                }
                mysqli_free_result($rowRS);
            }
            return $jsonArr->toArray();
        }
    
    7

      

    // return a JSON Object (jsonOBJ) from the rows
        function ParseRowsAsJSONObject($arrName, $rowRS){
            $jsonArr = new jsonOBJ($arrName); // name of the json array
    
            $rows = mysqli_num_rows($rowRS);
            if($rows > 0){
                while($rows > 0){
                    $rd = mysqli_fetch_assoc($rowRS);
                    $jsonArr->push($rd);
                    $rows--;
                }
                mysqli_free_result($rowRS);
            }
            return $jsonArr->toArray();
        }
    
    9
    [{"name":"Pankaj Singh","age":"20"},
    {"name":"Arun Yadav","age":"21"},
    {"name":"Apeksha Jaiswal","age":"20"}]
    0
    Array
    (
        [locations] => Array
            (
                [0] => Array
                    (
                        [location] => TestLoc1
                    )
    
                [1] => Array
                    (
                        [location] => TestLoc2
                    )
    
                [2] => Array
                    (
                        [location] => TestLoc3
                    )
    
            )
    
    )
    
    
    {"locations":[{"location":"TestLoc1"},{"location":"TestLoc2"},{"location":"TestLoc3"}]}
    
    0
    // return a JSON Object (jsonOBJ) from the rows
        function ParseRowsAsJSONObject($arrName, $rowRS){
            $jsonArr = new jsonOBJ($arrName); // name of the json array
    
            $rows = mysqli_num_rows($rowRS);
            if($rows > 0){
                while($rows > 0){
                    $rd = mysqli_fetch_assoc($rowRS);
                    $jsonArr->push($rd);
                    $rows--;
                }
                mysqli_free_result($rowRS);
            }
            return $jsonArr->toArray();
        }
    
    7

    [{"name":"Pankaj Singh","age":"20"},
    {"name":"Arun Yadav","age":"21"},
    {"name":"Apeksha Jaiswal","age":"20"}]
    3

    Output:

    {"first":{"id":1,"product_name":"Doorbell","cost":199},
    "second":{"id":2,"product_name":"Bottle","cost":99},
    "third":{"id":3,"product_name":"Washing Machine","cost":7999}}