donglvlao8367 2017-07-31 13:33
浏览 16
已采纳

将数据库中的数据存储在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"
)
  • 写回答

3条回答 默认 最新

  • dstbtam8732 2017-07-31 13:37
    关注

    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 
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里