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 基于作物生长模型下,有限水资源的最大化粮食产量的资源优化模型建立
  • ¥20 关于变压器的具体案例分析
  • ¥15 生成的QRCode圖片加上下載按鈕
  • ¥15 板材切割优化算法,数学建模,python,lingo
  • ¥15 科来模拟ARP欺骗困惑求解
  • ¥100 iOS开发关于快捷指令截屏后如何将截屏(或从截屏中提取出的文本)回传给本应用并打开指定页面
  • ¥15 unity连接Sqlserver
  • ¥15 图中这种约束条件lingo该怎么表示出来
  • ¥15 VSCode里的Prettier如何实现等式赋值后的对齐效果?
  • ¥20 keepalive配置业务服务双机单活的方法。业务服务一定是要双机单活的方式