array_push后数组为空

时间:2018-03-15 23:32:26

标签: php mysql arrays database mysqli

数组$response在array_push命令后为空,我不知道原因。我测试了数组$product是否已填充且是。

if (mysqli_num_rows($result) > 0) {
    $response = array();
    $result = mysqli_fetch_array($result,MYSQLI_ASSOC); 

    $product = array();
    $product[name] = $result['name'];
    $product[inn] = $result['inn'];
    $product[analgetikagroup] = $result['analgetikagroup'];
    $product[wholevel] = $result['wholevel'];
    $product[dailydose] = $result['dailydose'];
    $product[contraindication] = $result['contraindication'];
    $product[sideeffect] = $result['sideeffect'];
    $product[gastricprotection] = $result['gastricprotection'];
    $response[success] = 1;
    $response[product] = array();
    array_push($response[product],$product);

    echo json_encode($response);

1 个答案:

答案 0 :(得分:0)

它不是空的 - 也许你不会进入"如果"。如果我删除" if"它的工作原理

php >
php >     $product = array();
php >     $product[name] = $result['name'];
php >     $product[inn] = $result['inn'];
php >     $product[analgetikagroup] = $result['analgetikagroup'];
php >     $product[wholevel] = $result['wholevel'];
php >     $product[dailydose] = $result['dailydose'];
php >     $product[contraindication] = $result['contraindication'];
php >     $product[sideeffect] = $result['sideeffect'];
php >     $product[gastricprotection] = $result['gastricprotection'];
php >     $response[success] = 1;
php >     $response[product] = array();
php >     array_push($response[product],$product);
php >
php >
php >
php > echo json_encode($response);
{"success":1,"product":[{"name":null,"inn":null,"analgetikagroup":null,"wholevel":null,"dailydose":null,"contraindication":null,"sideeffect":null,"gastricprotection":null}]}
php >