Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
146 views
in Technique[技术] by (71.8m points)

Create Array in PHP from SQL Query by swapping a key for a value

I'm creating a dataset for an Echarts Bar Chart
(working example : https://echarts.apache.org/v4/examples/en/editor.html?c=dataset-simple1)
I need to fit the parameters of the dataset for it to display correctly.

I currently have this for my array:

0: Object { Technician: "BOBBY", Code: "2" }
1: Object { Technician: "BOBBY", Code: "2" }
2: Object { Technician: "BOBBY", Code: "1" }
3: Object { Technician: "JOHNNY", Code: "2" }
4: Object { Technician: "JOHNNY", Code: "3" }
5: Object { Technician: "SCOTTIE", Code: "1" }
6: Object { Technician: "SCOTTIE", Code: "2" }

How can I replace the key 'Code' with its value (e.g. 'QUICK ESTIMATE'), and in addition include any other codes performed by the same technician in the same row ? So that it looks like this:

0: Object { Technician: "BOBBY", QUICK ESTIMATE: "2", FULL ESTIMATE: "2", SALE: "1" }
1: Object { Technician: "JOHNNY", FULL ESTIMATE: "2", SALE: "3" }
2: Object { Technician: "SCOTTIE", QUICK ESTIMATE: "1", FULL ESTIMATE: "2" }

I created the first array with the following code and SQL query:

(Result of SQL Query:)
SQL Result Table

//============================ARRAY for Bar Chart - Project Count for each Tech===================
$sql = mysqli_query($conn, "SQL Query");
            
$return_arr = array();

while ($row = mysqli_fetch_array($sql)) {
    $row_array['Technician'] = $row['Technician'];
    $row_array['Code'] = $row['Count'];

    array_push($return_arr,$row_array);
}


echo json_encode($return_arr);
question from:https://stackoverflow.com/questions/66057062/create-array-in-php-from-sql-query-by-swapping-a-key-for-a-value

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...