SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Zealot
    Join Date
    Aug 2010
    Posts
    194
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    remove like items from associative array

    This is sample POST data I need to save in a database:

    PHP Code:
    Array
    (
        [
    suggested_quantity|sku1] => 
        [
    suggested_comment|sku1] => test
        
    [suggested_quantity|sku2] => 
        [
    suggested_comment|sku2] => test
     

    Instead of looping through all POST data that could be 100 or more, and doing 100 or more db updates.. is it possible to pull out all like data?

    For example, instead of doing 2 db updates for sku1, is there a way to pull out those name/value pairs to add to 1 update statment?

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Can you show the result of
    PHP Code:
    echo var_export($yourarray1); 
    instead of the var_dump() ?

  3. #3
    SitePoint Wizard wonshikee's Avatar
    Join Date
    Jan 2007
    Posts
    1,223
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    You'll want to parse it out and group them into a 2 dimensional array like this. This makes it easy to build an insert query.

    PHP Code:
    <?php
        
    foreach ($_POST as $k=>$v)
        {
            
    $parts explode('|'$k);
            
            if (
    $parts[1]) {
                
    $post[$parts[1]][sanitize($parts[0])] = sanitize($v);    
            }
            
        }
        echo 
    '<pre>';
        
    print_r($post); die;
    ?>

  4. #4
    SitePoint Zealot
    Join Date
    Aug 2010
    Posts
    194
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    thanks for your help.

    boss changed the specs so now it makes sense to just loop over the $_POST data. Your suggestions would have worked for me though

    What's the difference between var_dump() and var_export()? I printed them on screen and it appears to hold the same information.

  5. #5
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    var_export outputs a proper PHP array that we can copy/paste -- var_dump shows you what is in the array, but we have to type it out to replicate what you had.

    What you posted as var_dump did not make sense to me -- I wanted to see how it was written in PHP.

  6. #6
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    Off Topic:

    Quote Originally Posted by Cups View Post
    What you posted as var_dump ...
    You mean, print_r(), right?
    Salathe
    Software Developer and PHP Manual Author.

  7. #7
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Ah, was it so?

    Just as equally useless if you want anyone to take your array problem and fix it for you.

    edit:

    Actually I don't think I have used print_r since I was like, 7. var_dump and xDebug is a nice combo that shows you so much more data.


Bookmarks

Posting Permissions

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