1

I'm facing a little problem and I don't know is it's possible to achieve it or not. Let me explain :

I have an associative array like this :

Array
(
    [res_id] => 104
    [subject] => Test
    [type_id] => 503
    [format] => pdf
    [typist] => fefe
    [creation_date] => 2017-02-10 14:27:37.236711
    [modification_date] => 2017-02-10 14:27:37.236711
    [fulltext_result] => 1
    [doc_date] => 2017-02-01 00:00:00
    [docserver_id] => FASTHD_MAN
    [path] => 2017#02#0001##
    [filename] => 0008.pdf
    [fingerprint] => 
    [filesize] => 84979
    [status] => VAL
    [destination] => DSG
    [priority] => 2
    [is_multi_docservers] => N
    [is_frozen] => N
    [tablename] => res_letterbox
    [initiator] => COU
    [dest_user] => ddaull
    [locker_user_id] => fefefef
    [locker_time] => 2017-02-13 15:52:25.624521
    [confidentiality] => N
    [tnl_path] => 2017#02#0001##
    [tnl_filename] => 0008.png
)

I want to know if I can use this associative array, in order to make an INSERT TO request ? I want the first part of the array (like res_id, subject) goes to column part for the insertion. The second part of the array (like 104,Test) will go to the values

Thanks in advance for your help, hope I am clear enough..

5
  • 1
    Sure it's possible, but you need to write it. Either loop over your array to build a query, or use array_keys() and array_values() to extract what you need. Commented Feb 13, 2017 at 14:57
  • So I have to have two arrays and make a query like this, after use array_keys and array_value : INSERT INTO tablename ($arrayColumn) VALUES ($arrayValues) ? Commented Feb 13, 2017 at 15:06
  • @Nathan30 you mean insert into table ('res_id') values (104) like this ? Commented Feb 13, 2017 at 15:07
  • respective key with its value ? Commented Feb 13, 2017 at 15:08
  • @BunkerBoy More or less yes. I want only one request to insert all the values contains in my array. The values must match the respective key yes Commented Feb 13, 2017 at 15:08

1 Answer 1

1

@Nathan30 try this :

 <?php
    $arr = array(
        "res_id" => 104,
        "subject" => "Test",
        "type_id" => 503,
        "format" => "pdf",
        "typist" => "fefe",
        "creation_date" => "2017-02-10 14:27:37.236711",
        "modification_date" => "2017-02-10 14:27:37.236711",
        "fulltext_result" => 1,
        "doc_date" => "2017-02-01 00:00:00",
        "docserver_id" => "FASTHD_MAN",
        "path" => "2017#02#0001##",
        "filename" =>" 0008.pdf",
        "fingerprint" => "",
        "filesize" => 84979,
        "status" => "VAL",
        "destination" => "DSG",
        "priority" => 2,
        "is_multi_docservers" => "N",
        "is_frozen" => "N",
        "tablename" => "res_letterbox",
        "initiator" => "COU",
        "dest_user" => "ddaull",
        "locker_user_id" => "fefefef",
        "locker_time" => "2017-02-13 15:52:25.624521",
        "confidentiality" => "N",
        "tnl_path" => "2017#02#0001##",
        "tnl_filename" => "0008.png",
    );
    echo "<pre>";
    print_r($arr);
    $column = array();
    $values = array();
    foreach($arr as $key => $value){
        $column[] = $key;
        $values[] = $value;
    }

    now query will be like:

    "insert into table values(".implode(',', $column).") values (".implode(',', $values).")";
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect thanks a lot. I just have to modify $values[] = $value; to $values[] = "'" . $value . "'"; but your answer is what I needed

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.