0

I have spent a serious amount of time looking at arrays arrays_fill_keys and many other alternatives to try and solve what seemingly is a standard request:

I have a DB table. I am going to return several columns from it and most likely all rows. On return I am looking to insert each row as an object into an array. I have been able to do that. But I want to return each column as a key in that object. I have been partially been able to do that. But my code seems to insert each row as an array with another array, as opposed to object in array.

Code Below:

if(isset($_POST["BasicSearchExecuted"]) && !empty($_POST["BasicSearchExecuted"])){
    $basic_search_table_name = array($_POST["TableName"]);
    $basic_search_column_names_sql = "SELECT TABLE_NAME, CATEGORY_NAME, COLUMN_NAME, DATA_TYPE, CHARACTER_LENGTH FROM TableColumnsAndCategories WHERE TABLE_NAME=?";
    $basic_search_table_name = str_replace('View', '', $basic_search_table_name);
    $basic_search_column_names = sqlsrv_query($database_connection, $basic_search_column_names_sql, $basic_search_table_name);
    $basic_search_column_array = array();
    while($basic_search_column_names_option = sqlsrv_fetch_object($basic_search_column_names)){
        $basic_search_column_array[] = array("ColumnName" => $basic_search_column_names_option->COLUMN_NAME, "DataType" => $basic_search_column_names_option->DATA_TYPE, "CategoryName" => $basic_search_column_names_option->CATEGORY_NAME);       
    }
    print_r($basic_search_column_array);
}

This is returning the following:

Array
(
    [0] => Array
        (
            [ColumnName] => HardwareAssetID
            [DataType] => uniqueidentifier
            [CategoryName] => Default
        )

    [1] => Array
        (
            [ColumnName] => HardwareAssetAssetName
            [DataType] => varchar
            [CategoryName] => Basic
        )

    [2] => Array
        (
            [ColumnName] => HardwareAssetAssetStatusID
            [DataType] => tinyint
            [CategoryName] => Basic
        )

    [3] => Array
        (
            [ColumnName] => HardwareAssetAssetTag
            [DataType] => varchar
            [CategoryName] => Basic
        )

    [4] => Array
        (
            [ColumnName] => HardwareAssetSerialNumber
            [DataType] => varchar
            [CategoryName] => Basic
        )

    [5] => Array
        (
            [ColumnName] => HardwareAssetManufacturerID
            [DataType] => uniqueidentifier
            [CategoryName] => Basic
        )
)

What i instead want to return is something like this, or at least something that can be interpreted and utilised like the following in JS:

    Array= [{

        ColumnName: "HardwareAssetID",
        DataType: "uniqueidentifier",
        CategoryName: "Default",
        },
        {
        ColumnName: "HardwareAssetID",
        DataType: "uniqueidentifier",
        CategoryName: "Default",
        },
        {
        ColumnName: "HardwareAssetID",
        DataType: "uniqueidentifier",
        CategoryName: "Default",
        }
    }]

Any help or advice is appreciated.

2 Answers 2

1

Would the built-in json_encode function work?

Just replace print_r($basic_search_column_array); with echo json_encode($basic_search_column_array);.

Sign up to request clarification or add additional context in comments.

2 Comments

thank you very much, as easy as that. wasnt aware of the function
There are some keywords with every language which help when searching Google. json and encode are two good ones :)
0

Have you read the documentation of print_r()? It says: "Prints human-readable information about a variable". The purpose if print_r() is to help the developer understand the value and the structure of a variable, not to pass the data to another program or to store it for later use.

Use json_encode() if you want to encode the data as JSON to pass it to another software (a JS script that runs in a browser, for example).

Comments

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.