Home >Backend Development >PHP Problem >php result to array
In PHP language, it is often necessary to convert query results (result) into array (array) format to facilitate data processing and display. In this article, I will introduce commonly used methods to convert result into an array, and provide sample code and operation steps.
1. Use the fetch_assoc() method to convert the result into an array
In the PHP language, using the fetch_assoc() method to convert the result into an array is one of the most commonly used methods. The fetch_assoc() method can convert the query result into an associative array, each element is a key-value pair, the key represents the column name in the query result, and the value represents the corresponding column value. The following is a code example for converting result into an array:
//执行查询操作 $result = mysqli_query($conn, "SELECT name, age, gender FROM users"); //将查询结果转换为数组格式 $array = array(); while($row = mysqli_fetch_assoc($result)){ $array[] = $row; }
In the above code, a query operation is first performed and the query result is assigned to the variable $result. Next, define an empty array $array, and use a while loop to traverse each row of records in the query results. In the while loop, use the fetch_assoc() method to convert the current row record into an associative array $row, and add the array to the defined empty array. Finally, the obtained $array array is the converted array of query results we need.
2. Use the fetch_array() method to convert the result into an array
In addition to using the fetch_assoc() method to convert the result into an array, we can also use the fetch_array() method. The fetch_array() method can convert the query result into a mixed array. Each element is a key-value pair or index value pair. The key represents the position in the query result, and the value represents the corresponding column value. The following is a code example for converting result into an array:
//执行查询操作 $result = mysqli_query($conn, "SELECT name, age, gender FROM users"); //将查询结果转换为数组格式 $array = array(); while($row = mysqli_fetch_array($result)){ $array[] = $row; }
In the above code, it is similar to the code that uses the fetch_assoc() method to convert result into an array. First, a query operation is performed and the query result is assigned to a variable. $result. Next, define an empty array $array, and use a while loop to traverse each row of records in the query results. In the while loop, use the fetch_array() method to convert the current row record into a mixed array $row, and add the array to the defined empty array. Finally, the obtained $array array is the converted array of query results we need.
3. Use the fetch_row() method to convert the result into an array
In addition to using the fetch_assoc() method and the fetch_array() method to convert the result into an array, we can also use the fetch_row() method. The fetch_row() method only returns the numerical value in the query result (that is, it does not include column names) and converts the query result into an index array. The following is a code example for converting result to an array:
//执行查询操作 $result = mysqli_query($conn, "SELECT name, age, gender FROM users"); //将查询结果转换为数组格式 $array = array(); while($row = mysqli_fetch_row($result)){ $array[] = $row; }
In the above code, it is similar to the code that uses the fetch_assoc() method and the fetch_array() method to convert the result into an array. A query operation is first executed, and The query results are assigned to the variable $result. Next, define an empty array $array, and use a while loop to traverse each row of records in the query results. In the while loop, use the fetch_row() method to convert the current row record into an index array $row, and add the array to the defined empty array. Finally, the obtained $array array is the converted array of query results we need.
The above are the common methods and operation steps for converting query results (result) into arrays in PHP language. In actual development, appropriate methods can be selected for data processing and display based on specific business needs.
The above is the detailed content of php result to array. For more information, please follow other related articles on the PHP Chinese website!