Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Jan 2009
    Posts
    91
    Thanks
    17
    Thanked 1 Time in 1 Post

    Insert an associative array into another array

    I have been slamming my head against a wall.. how do I turn this:

    PHP Code:
    Array 
            ( 
                                  [
    note] =>  
                                  [
    careerID] => 20 
                                  
    [lastLogin] => 1231125665 
                                  
    [bearerStatus] => 
                                  
    [lastLoginNumberHour24] => 22 
                                  
    [dept] => 
                                  
    [lastLoginNumberDay] => 
                                  
    [statusNumber] => 
                                  
    [lastLoginNumberMonth] => 
                                  
    [lastLoginNumberYear] => 2009 
                                  
    [memberID] => 28178 

             
    )

    Array 
            ( 
                                  [
    note] =>  
                                  [
    careerID] => 20 
                                  
    [lastLogin] => 1224125665 
                                  
    [bearerStatus] => 
                                  
    [lastLoginNumberHour24] => 22 
                                  
    [dept] => 
                                  
    [lastLoginNumberDay] => 
                                  
    [statusNumber] => 
                                  
    [lastLoginNumberMonth] => 
                                  
    [lastLoginNumberYear] => 2009 
                                  
    [memberID] => 28178 

             

    into this:

    PHP Code:
    Array ( 
                [
    1] => Array 
            ( 
                                  [
    note] =>  
                                  [
    careerID] => 20 
                                  
    [lastLogin] => 1231125665 
                                  
    [bearerStatus] => 
                                  
    [lastLoginNumberHour24] => 22 
                                  
    [dept] => 
                                  
    [lastLoginNumberDay] => 
                                  
    [statusNumber] => 
                                  
    [lastLoginNumberMonth] => 
                                  
    [lastLoginNumberYear] => 2009 
                                  
    [memberID] => 28178 

             
    )
    Array ( 
                [
    2] => Array 
            ( 
                                  [
    note] =>  
                                  [
    careerID] => 20 
                                  
    [lastLogin] => 1224125665 
                                  
    [bearerStatus] => 
                                  
    [lastLoginNumberHour24] => 22 
                                  
    [dept] => 
                                  
    [lastLoginNumberDay] => 
                                  
    [statusNumber] => 
                                  
    [lastLoginNumberMonth] => 
                                  
    [lastLoginNumberYear] => 2009 
                                  
    [memberID] => 28178 

             

    Php manual does not seem t discuss this that I can find.

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    PHP Code:
    $a1 = array(          [note] =>  
                                  [
    careerID] => 20 
                                  
    [lastLogin] => 1231125665 
                                  
    [bearerStatus] => 
                                  
    [lastLoginNumberHour24] => 22 
                                  
    [dept] => 
                                  
    [lastLoginNumberDay] => 
                                  
    [statusNumber] => 
                                  
    [lastLoginNumberMonth] => 
                                  
    [lastLoginNumberYear] => 2009 
                                  
    [memberID] => 28178          );
    $a2 = array(           [note] =>  
                                  [
    careerID] => 20 
                                  
    [lastLogin] => 1224125665 
                                  
    [bearerStatus] => 
                                  
    [lastLoginNumberHour24] => 22 
                                  
    [dept] => 
                                  
    [lastLoginNumberDay] => 
                                  
    [statusNumber] => 
                                  
    [lastLoginNumberMonth] => 
                                  
    [lastLoginNumberYear] => 2009 
                                  
    [memberID] => 28178          );
    $aa1[1] = $a1;
    $aa2[2] = $a2
    best regards

  • #3
    New Coder
    Join Date
    Jan 2009
    Posts
    91
    Thanks
    17
    Thanked 1 Time in 1 Post
    I kind of understand what you are saying.. still cloudy though.

    I am currently messing around with:

    PHP Code:
    $query "SELECT * FROM current_roster"
         
    $result mysql_query($query) or die(mysql_error());
    while(
    $currentmysql_fetch_assoc($result)) {
    $roster=array($current);
    echo 
    "<pre>";
    print_r($roster);
    echo 
    "</pre>";

    The above works perfect except the key for the first array is always [0].

    PHP Code:
    Array
    (
        [
    0] => Array
            (
                                  [
    note] =>   
                                  [
    careerID] => 20  
                                  
    [lastLogin] => 1231125665  
                                  
    [bearerStatus] => 0  
                                  
    [lastLoginNumberHour24] => 22  
                                  
    [dept] => 0  
                                  
    [lastLoginNumberDay] => 4  
                                  
    [statusNumber] => 1  
                                  
    [lastLoginNumberMonth] => 1  
                                  
    [lastLoginNumberYear] => 2009  
                                  
    [memberID] => 28178        )

    )

    Array
    (
        [
    0] => Array
            (
                                  [
    note] =>   
                                  [
    careerID] => 20  
                                  
    [lastLogin] => 1224125665  
                                  
    [bearerStatus] => 0  
                                  
    [lastLoginNumberHour24] => 22  
                                  
    [dept] => 0  
                                  
    [lastLoginNumberDay] => 4  
                                  
    [statusNumber] => 1  
                                  
    [lastLoginNumberMonth] => 1  
                                  
    [lastLoginNumberYear] => 2009  
                                  
    [memberID] => 28178  
            
    )



  • #4
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    you define a new $roster each time and because array index start from 0, will be 0
    PHP Code:
    $query "SELECT * FROM current_roster"
         
    $result mysql_query($query) or die(mysql_error());
    while(
    $currentmysql_fetch_assoc($result)) {
    $roster=array($current);
    echo 
    "<pre>";
    print_r($roster);
    echo 
    "</pre>";

    this will build a array of arrays which start from index 0
    PHP Code:
    $query "SELECT * FROM current_roster"
         
    $result mysql_query($query) or die(mysql_error());
    $roster = array();
    while(
    $currentmysql_fetch_assoc($result)) {
    $roster[] =array($current);
    }
    echo 
    "<pre>";
    print_r($roster);
    echo 
    "</pre>"
    and this same thing starting from 1 or any other value you want
    PHP Code:
    $query "SELECT * FROM current_roster"
         
    $result mysql_query($query) or die(mysql_error());
    $roster = array();
    $pos 1;
    while(
    $currentmysql_fetch_assoc($result)) {
    $roster[$pos++] =array($current); // force key to be $pos and increment
    }
    echo 
    "<pre>";
    print_r($roster);
    echo 
    "</pre>"
    best regards

  • Users who have thanked oesxyl for this post:

    Hayyel (03-18-2009)

  • #5
    New Coder
    Join Date
    Jan 2009
    Posts
    91
    Thanks
    17
    Thanked 1 Time in 1 Post
    As always oesxyl thanks for your help.

    I ended up with this after trying your suggestion and seeing the result.

    PHP Code:
    $query "SELECT * FROM current_roster"
         
    $result mysql_query($query) or die(mysql_error());
    $roster = array();
    $pos 1;
    while(
    $currentmysql_fetch_assoc($result)) {
    $roster[$pos++] =$current// force key to be $pos and increment
    }
    echo 
    "<pre>";
    print_r($roster);
    echo 
    "</pre>"
    Is there anything wrong with this code?

    I am hoping that since it is in the same format as my other array I can run a array_diff_assoc .

  • #6
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by Hayyel View Post
    As always oesxyl thanks for your help.

    I ended up with this after trying your suggestion and seeing the result.

    PHP Code:
    $query "SELECT * FROM current_roster"
         
    $result mysql_query($query) or die(mysql_error());
    $roster = array();
    $pos 1;
    while(
    $currentmysql_fetch_assoc($result)) {
    $roster[$pos++] =$current// force key to be $pos and increment
    }
    echo 
    "<pre>";
    print_r($roster);
    echo 
    "</pre>"
    Is there anything wrong with this code?
    no, that if I really understand what you want.

    I am hoping that since it is in the same format as my other array I can run a array_diff_assoc .
    I don't understand what you mean, can you give some examples or samples and details?

    best regards

  • #7
    New Coder
    Join Date
    Jan 2009
    Posts
    91
    Thanks
    17
    Thanked 1 Time in 1 Post
    What I am ultimately trying to do is compare $roster, the array made from the query, and $finalarr, the array comprising of the new data being inserted into the table $roster came from. I need to pull out of $roster all of the members that are no longer members.

    array_dif does not do multidimensional arrays... so I am looking for a way to walk the arrays if you will. I have tried various methods but none seem to work correctly.

    Currently I am looking at:

    PHP Code:
    //Compare Arrays
    function my_serialize(&$arr,$pos){ 
      
    $arr serialize($arr); 


    function 
    my_unserialize(&$arr,$pos){ 
      
    $arr unserialize($arr); 


     
    //make a copy 
    $first_array_s $roster
    $second_array_s $finalarr

    // serialize all sub-arrays 
    array_walk($first_array_s,'my_serialize'); 
    array_walk($second_array_s,'my_serialize'); 

    // array_diff the serialized versions 
    $diff array_diff($first_array_s,$second_array_s); 

    // unserialize the result 
    array_walk($diff,'my_unserialize'); 

        echo 
    "<pre>";
        
    print_r($diff);
        echo 
    "</pre>"
    With this $diff == $finalarr which is not correct.

  • #8
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    in mysql, that table, do you have a field which is uniq, a primary key? in this case you can use it to build a array with it's value as elements and then use array_filter, array_diff or other things to do what you want.

    best regards


  •  

    Posting Permissions

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