dcsimg
www.webdeveloper.com
Results 1 to 14 of 14

Thread: Insert Multidimensional array into database

  1. #1
    Join Date
    Jan 2017
    Posts
    7

    Post Insert Multidimensional array into database

    I want to insert this array into mysql table having fields user,product,remarks,color,size,count. how can i give the forloop condition and insert to database

    Array
    (
    [0] => Array
    (
    [user] => 10
    [product] => 58
    [remarks] => ok
    [details] => Array
    (
    [0] => Array
    (
    [color] => Red
    [data] => Array
    (
    [0] => Array
    (
    [size] => 12
    [count] => 1
    )

    [1] => Array
    (
    [size] => 14
    [count] => 2
    )

    )

    )

    )

    )

    [1] => Array
    (
    [user] => 10
    [product] => 60
    [remarks] => okk
    [details] => Array
    (
    [0] => Array
    (
    [color] => Black
    [data] => Array
    (
    [0] => Array
    (
    [size] => 10
    [count] => 5
    )

    [1] => Array
    (
    [size] => 12
    [count] => 2
    )

    )

    )

    [1] => Array
    (
    [color] => Red
    [data] => Array
    (
    [0] => Array
    (
    [size] => 10
    [count] => 3
    )

    [1] => Array
    (
    [size] => 12
    [count] => 7
    )

    )

    )

    )

    )

    )

  2. #2
    Join Date
    Jan 2017
    Posts
    7
    This is how the array looks..for better understanding
    Code:
    Array(
        [0] => Array (
                [user] => 10
                [product] => 58
                [remarks] => ok
                [details] => Array(
                        [0] => Array(
                                [color] => Red
                                [data] => Array(
                                        [0] => Array(
                                                [size] => 12
                                                [count] => 1
                                            )
    
                                        [1] => Array(
                                                [size] => 14
                                                [count] => 2
                                            )
                                    )
                            )
                    )
            )
        [1] => Array(
                [user] => 10
                [product] => 60
                [remarks] => okk
                [details] => Array(
                        [0] => Array(
                                [color] => Black
                                [data] => Array(
                                        [0] => Array(
                                                [size] => 10
                                                [count] => 5
                                            )
                                        [1] => Array(
                                                [size] => 12
                                                [count] => 2
                                            )
                                    )
                            )
                        [1] => Array(
                                [color] => Red
                                [data] => Array(
                                        [0] => Array(
                                                [size] => 10
                                                [count] => 3
                                            )
                                        [1] => Array(
                                                [size] => 12
                                                [count] => 7
                                            )
                                    )
                            )
                    )
            )
    )
    Last edited by NogDog; 01-09-2017 at 08:47 AM. Reason: Added [code]...[/code] tags

  3. #3
    Join Date
    Jan 2017
    Posts
    7
    The formatting is being removed while posting

  4. #4
    Join Date
    Jan 2017
    Posts
    7
    Pls help me.its urgent

  5. #5
    Join Date
    Jan 2017
    Location
    West Midlands, UK
    Posts
    39
    Hi,

    Look at the PHP command serialize and unserialize - reference Serialize on php.net

    Use serialise on the array to store it to the database and unserialize to retrieve it.

    Please note that your database field will have to be of the correct size - the standard varchar(45) will only allow serialized arrays of up to 45 characters, which typically will not be enough for multi-dimensional arrays.

    I hope that his helps.

    Shaun.

  6. #6
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    21,917
    Quote Originally Posted by affaz2 View Post
    The formatting is being removed while posting
    Using this forum's applicable BBCode tags will help. I edited your reply using the [code]...[/code] tags. We also have [php]...[/php] and [html]...[/html] tags.
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  7. #7
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    21,917
    I might try something like this (untested, no error-trapping):
    PHP Code:
    $sql = <<<EOD
    INSERT INTO table_name (
      user,
      product,
      remarks,
      color,
      size,
      count
    ) VALUES (
      :user,
      :product,
      :remarks,
      :color,
      :size,
      :count
    )
    EOD;
    $stmt $pdo->prepare($sql);
    foreach(
    $myArray as $item)
    {
        foreach(
    $item as $details) {
            foreach(
    $details as $data) {
                
    $stmt->execute(array(
                    
    ':user'    => $item['user'],
                    
    ':product' => $item['product'],
                    
    ':remarks' => $item['remarks'],
                    
    ':color'   => $details['color'],
                    
    ':size'    => $data['size'],
                    
    ':count'   => $data['count']
                ));
            }
        }

    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  8. #8
    Join Date
    Jan 2017
    Posts
    7
    It shows errors
    Invalid argument supplied for foreach()

  9. #9
    Join Date
    Jan 2017
    Posts
    7
    thank you

  10. #10
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    21,917
    Quote Originally Posted by affaz2 View Post
    It shows errors
    Invalid argument supplied for foreach()
    Did you replace $myArray with the name of the actual array variable?
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  11. #11
    Join Date
    Jan 2017
    Posts
    7
    Quote Originally Posted by NogDog View Post
    Did you replace $myArray with the name of the actual array variable?
    yes

  12. #12
    Join Date
    Nov 2016
    Posts
    12
    NogDog is doing this the best way, IMO. However, parsing your cluttered multidimensional arrays is your work, nobody wants to do it for you.

    I'd look at cleaning up the array to make it easier. :-)

  13. #13
    Join Date
    Jan 2017
    Location
    West Midlands, UK
    Posts
    39
    It seems that I misunderstood the question :-|

  14. #14
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    1,748
    Such a complex array begs for a db structure, not the house of cards that an array can be. SHOULD you at some point absolutely need an array a simple query and a pre-written function can build one for you. In the meantime - having a db structure to rely on to produce answers in any form that is needed becomes much more feasible.

    How about adding a new attribute? Are you going to manually edit that array to do that? Consider a simple restructure of your table to add a column, a simple query to insert the set of data into that column and a quick fix to that pre-written function to add that new attribute. Voila!
    JG
    PS - If you're posting here you should be using:

    error_reporting(E_ALL);
    ini_set('display_errors', '1');


    at the top of ALL php code while you develop it!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles