使用数组

时间:2017-03-21 07:31:44

标签: php mysql

目前,当通过print_r();

输出时,我有一个类似于以下内容的数组
$arr = Array
(
    [14] => Array
        (
            [url] => AllConsuming.net
            [da] => 48
            [pa] => 54.4
            [moz_rank] => 5.1
            [linksln] => 2123
            [equity] => 1318
            [alexa_rank] => 1725106
            [a_links] => 197
            [a_cnt] => GB
            [a_cnt_rank] => 123484
            [sem_rush_domain] => notfound
            [sr_rank] => 0
            [sr_kwords] => 0
            [sr_traffic] => 0
            [sr_costs] => 0
            [sr_ulinks] => 514
            [sr_hlinks] => 4154
            [sr_dlinks] => 12732
            [cf] => 34
            [tf] => 21
            [ebl] => 108777
            [ref_domains] => 1113
        )
[15] => Array
    (
        [url] => TravelIntelligence.net
        [da] => 31.7
        [pa] => 33.3
        [moz_rank] => 2.9
        [linksln] => 1
        [equity] => 1
        [alexa_rank] => 4265239
        [a_links] => 98
        [a_cnt] => N/A
        [a_cnt_rank] => N/A
        [sem_rush_domain] => notfound
        [sr_rank] => 0
        [sr_kwords] => 0
        [sr_traffic] => 0
        [sr_costs] => 0
        [sr_ulinks] => 1
        [sr_hlinks] => 3
        [sr_dlinks] => 3542
        [cf] => 22
        [tf] => 19
        [ebl] => 6368
        [ref_domains] => 531
    )

Mysql表如下所示:

Mysql table

我想将这些数据更新到MySQL表中,每个元素值都属于其各自的字段。数组的键,即14,15是MySQL表的主键。

我希望有些天才可以帮助我解决这个问题。

谢谢。

3 个答案:

答案 0 :(得分:0)

您可以使用$arr迭代foreach来构建预准备语句,如下所示:

/** @var \PDO $dbHandle */

foreach ($arr as $id => $row) {

    $row['out_id'] = $id;

    $setInstructions = array_map(function ($key) {
        return "'$key' = :$key";
    }, array_keys($row));

    $columnNamesWithSemicolon = array_map(function ($key) {
        return ":$key";
    }, array_keys($row));

    $result = $dbHandle->prepare("UPDATE your_table SET " . implode(',', $setInstructions) . " WHERE out_id = :out_id");

    $result->execute(array_combine($columnNamesWithSemicolon, array_values($row)));
}

答案 1 :(得分:0)

foreach($arr as $key=>$val)
{
    $paramquery = array();
    $paramquery['primarykey'] = $key;  
    $paramquery['goes on your column name'] = $val_every_row['every key matched column'];
    ------ goes on your column and fill it with your column data  


    INSERT OR UPDATE HERE
    $this->model->whateverfunction($paramquery);

}

它非常简单,如果你以json的形式发布数据会更容易。但这是它的大局观

答案 2 :(得分:-1)

@Pooja你可以使用以下概念:

 <?php
$arr = array(
        "14" => array(
                "url" => "AllConsuming.net",
                "da" => 48,
                "pa" => 54.4,
                "moz_rank" => 5.1,
                "linksln" => 2123,
                "equity" => 1318,
                "alexa_rank" => 1725106,
                "a_links" => 197,
                "a_cnt" => "GB",
                "a_cnt_rank" => 123484,
                "sem_rush_domain" => "notfound",
                "sr_rank" => 0,
                "sr_kwords" => 0,
                "sr_traffic" => 0,
                "sr_costs" => 0,
                "sr_ulinks" => 514,
                "sr_hlinks" => 4154,
                "sr_dlinks" => 12732,
                "cf" => 34,
                "tf" => 21,
                "ebl" => 108777,
                "ref_domains" => 1113,
            ),
        "15" => array(
            "url" => "TravelIntelligence.net",
            "da" => 31.7,
            "pa" => 33.3,
            "moz_rank" => 2.9,
            "linksln" => 1,
            "equity" => 1,
            "alexa_rank" => 4265239,
            "a_links" => 98,
            "a_cnt" => "N/A",
            "a_cnt_rank" => "N/A",
            "sem_rush_domain" => "notfound",
            "sr_rank" => 0,
            "sr_kwords" => 0,
            "sr_traffic" => 0,
            "sr_costs" => 0,
            "sr_ulinks" => 1,
            "sr_hlinks" => 3,
            "sr_dlinks" => 3542,
            "cf" => 22,
            "tf" => 19,
            "ebl" => 6368,
            "ref_domains" => 531
        )
    );

$yourTableColumnArray = array("url", "cost", "categroy_1", "categroy_2", "categroy_3", "language", "f_n", "publishers", "da", "tf", "sr_traffic", "pa", "moz_rank", "linksln", "equity", "alexa_rank", "a_links", "a_cnt", "a_cnt_rank");

foreach($arr as $key => $value){
    $id = $key;
    $columnAndValue = array();
    foreach($value as $k => $val){
        if(in_array($val, $yourTableColumnArray)){
            $columnAndValue[$k] = $val;
        }
    }
    if(!empty($columnAndValue)){
        //your query like below:
        $query = "UPDATE yourTableName SET";
        foreach($columnAndValue as $ky => $vl){
            $query .= " ".$ky."=".$vl;
            end($columnAndValue);
            if(key($columnAndValue) != $ky){
              $query .= ",";
            }

        }
        $query .= " where out_id = $id"; 
        echo $query."<br>"; //just to know, the actual query formed for the current array(for 14 or 15)

        // now code here to execute your query with $query variable

    }
    $columnAndValue = array();
}
  

输出

对于数组键= 14,查询将是:

UPDATE yourTableName SET sr_rank = 0,sr_kwords = 0,sr_traffic = 0,sr_costs = 0 where out_id = 14

对于数组键= 15,查询将是:

UPDATE yourTableName SET sr_rank = 0,sr_kwords = 0,sr_traffic = 0,sr_costs = 0 where out_id = 15