将数据库中的数据存储在1d数组中而不是2d
I want to store a column of my database table in an array so I can compare it with an array I have. I am selecting only one column from the table and I am storing all the rows. I've managed to do so but in a 2d array but when comparing the 2d array to my 1d array i get an error. So please help me to convert it into a 1d array or from the begining if I can store the data in a 1d array.
$array = array();
$serviceId = "SELECT service_id FROM servicesorders WHERE order_id = '$orderId'";
$resultId = mysqli_query($connection, $serviceId) or die(mysqli_error($connection));
while ($serviceIdfinal = mysqli_fetch_assoc($resultId)) {
$array[] = $serviceIdfinal; //the array I used to store the data
}
var_dump($array);
$isCheckedstring = $_POST['show'];
$isCheckedarray = str_split($isCheckedstring, 4); // the array I want to compare the stored data with
var_dump($isCheckedarray);
the var_dump of the two arrays are as follows:
array(
[0]=> array(
["service_id"]=> "1"
)
[1]=> array(
["service_id"]=> "7"
)
[2]=> array(
["service_id"]=> "11"
)
)
And
array(
[0]=>"0011"
[1]=>"0012"
)
我想将数据库表的一列存储在一个数组中,这样我就可以将它与我拥有的数组进行比较。 我只从表中选择一列,然后存储所有行。 我已经设法这样做但是在二维数组中但是当将二维数组与我的一维数组进行比较时,我得到一个错误。 所以请帮助我将其转换为1d数组或从开始转换,如果我可以将数据存储在1d数组中。 p>
$ array = array();
$ serviceId =“SELECT service_id FROM servicesorders WHERE order_id ='$ orderId'”;
$ resultId = mysqli_query($ connection, $ serviceId)或die(mysqli_error($ connection));
while($ serviceIdfinal = mysqli_fetch_assoc($ resultId)){
$ array [] = $ serviceIdfinal; //我用来存储数据的数组
}
var_dump($ array);
$ isCheckedstring = $ _POST ['show'];
$ isCheckedarray = str_split($ isCheckedstring,4); //数组我想比较存储的数据和
var_dump($ isCheckedarray);
code> pre>
两个数组的var_dump如下: p>
array(
[0] => array(
[“service_id”] =>“1”
)
[1] => array(
[“service_id”] =>“7”
)
[2] =>数组(
[“service_id”] =>“11”
)
)
code>
p>
array(
[0] =>“0011”
[1] =>“0012”\ n)
code> pre>
div>
You are using mysqli_fetch_assoc
so you need to fetch the associative column.
You need to change
while ($serviceIdfinal = mysqli_fetch_assoc($resultId)) {
$array[] = $serviceIdfinal; //the array I used to store the data
}
to
while ($serviceIdfinal = mysqli_fetch_assoc($resultId)) {
$array[] = $serviceIdfinal['service_id']; //the array I used to store the data
}
When your database returns an array, it's returning an array of rows, each of which is an associative array. It doesn't matter that each row has one element within it, it's still an array of arrays.
You're going to have to convert the $array
from one form to another with a foreach loop:
$newArray = [];
foreach($array as $row) {
// This line here is where you'll do any needed string modifications
// such as padding with leading zeroes, etc.
$newArray[] = $row["service_id"];
}
var_dump($newArray)
This should net you:
array(
[0]=> "1",
[1]=> "7",
[2]=> "11"
)
EDIT: Or you should do it in the while loop you're already using when you're fetching it, as Milan pointed out in his answer.
You can try to use PDO instead of mysqli.
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$sql = $conn->query("your query");
$sql->fetch(PDO::FETCH_ASSOC);
That's it :)