Php从db查询数组

时间:2016-07-06 23:07:51

标签: php arrays

我知道这是一个愚蠢的问题,但我一直在打我的脑袋。我感谢所有的帮助。

我有一个mysql查询结果。我循环遍历结果并创建一个包含一些值的新数组。但是当我这样做时,它会将所有值加两次。我确定我做的事情很愚蠢。只是想不出来。

这是我的代码:

 $cook = getVolListCharRegion('V', 'Cook', $hmRegion);
 foreach($cook as $hm){
    $newCook[] = array("ID" => $hm['__id'], 
                      "name_first" => $hm['name_first'], 
                      "name_last" => $hm['name_last'],
                      "region" => $hm['demo_region_c'],
                     );
 }
 var_dump($newCook);

orig查询共返回29条记录。当我转储$ newCook数组时,有57?

由于安全性,我清理了这个数组。这是$ cook查询的结果

 Array
(
    [0] => Array
        (
            [recid] => 95874
            [name_middle] => Arnold
            [name_last] => Buck
            [name_suffix] => 
            [name_first] => Joe
            [demo_ethnicity] => Caucasian
            [demo_gender] => Male
            [_account_name] => jabuck
            [demo_region_c] => 4
            [note_acct] => 
            [type] => V
            [__id] => 7248
            [acct_SYS::preferred_DB] => 25
            [zzzCreateDate] => 07/13/2015 11:50:55
            [zzzCreateBy] => webuser
            [_isActive] => 1
        )

[1] => Array
    (
        [recid] => 95447
        [name_middle] => R.
        [name_last] => Campbell
        [name_suffix] => 
        [name_first] => David 
        [demo_ethnicity] => Caucasian
        [demo_gender] => Male
        [_account_name] => davec122
        [demo_region_c] => 4
        [type] => V
        [__id] => 6821
        [acct_SYS::preferred_DB] => 25
        [zzzCreateDate] => 12/20/2014 10:01:38
        [zzzCreateBy] => webuser
        [_isActive] => 1
    )

[2] => Array
    (
        [recid] => 93749
        [name_middle] => 
        [name_last] => Carman
        [name_suffix] => 
        [name_first] => Lisa
        [demo_ethnicity] => 
        [demo_gender] => Female
        [_account_name] => clclac@aol.com
        [demo_region_c] => 4
        [type] => V
        [__id] => 5333
        [acct_SYS::preferred_DB] => 25
        [zzzCreateDate] => 09/11/2012 18:20:51
        [zzzCreateBy] => php
        [_isActive] => 1
    )

[3] => Array
    (
        [recid] => 94384
        [name_middle] => 
        [name_last] => Coffer
        [name_suffix] => 
        [name_first] => Koy
        [demo_ethnicity] => Caucasion
        [demo_gender] => Female
        [_account_name] => Koy Coffer
        [demo_region_c] => 4
        [note_acct] => 
        [type] => V
        [__id] => 5758
        [acct_SYS::preferred_DB] => 25
        [zzzCreateDate] => 08/18/2014 12:23:17
        [zzzCreateBy] => webuser
        [_isActive] => 1
    )

[4] => Array
    (
        [recid] => 93469
        [name_middle] => 
        [name_last] => Corona
        [name_suffix] => 
        [name_first] => David
        [demo_ethnicity] => Hispanic
        [demo_gender] => Male
        [_account_name] => dscorona@aol.com
        [demo_region_c] => 4
        [type] => V
        [__id] => 5053
        [acct_SYS::preferred_DB] => 25
        [zzzCreateDate] => 09/22/2010 20:38:24
        [zzzCreateBy] => php
        [_isActive] => 1
    )

[5] => Array
    (
        [recid] => 95866
        [name_middle] => 
        [name_last] => Griffin
        [name_suffix] => 
        [name_first] => Dan
        [demo_ethnicity] => 
        [demo_gender] => Male
        [_account_name] => WNShunting
        [demo_region_c] => 4
        [type] => V
        [__id] => 7240
        [acct_SYS::preferred_DB] => 25
        [zzzCreateDate] => 07/07/2015 07:17:45
        [zzzCreateBy] => webuser
        [_isActive] => 1
    )

[6] => Array
    (
        [recid] => 95315
        [name_middle] => 
        [name_last] => Guzman
        [name_suffix] => 
        [name_first] => Mario
        [demo_ethnicity] => Hispanic
        [demo_gender] => Male
        [_account_name] => mguzman77
        [demo_region_c] => 4
        [type] => V
        [__id] => 6689
        [acct_SYS::preferred_DB] => 25
        [zzzCreateDate] => 11/30/2014 17:54:06
        [zzzCreateBy] => webuser
        [_isActive] => 1
    )

[7] => Array
    (
        [recid] => 94402
        [name_middle] => 
        [name_last] => Jones
        [name_suffix] => 
        [name_first] => Bryan
        [demo_ethnicity] => 
        [demo_gender] => Male
        [_account_name] => bjones
        [demo_region_c] => 4
        [note_acct] => 
        [type] => V
        [__id] => 5776
        [acct_SYS::preferred_DB] => 25
        [zzzCreateDate] => 08/20/2014 10:55:32
        [zzzCreateBy] => webuser
        [_isActive] => 1
    )

[8] => Array
    (
        [recid] => 95781
        [name_middle] => Duane
        [name_last] => Joslin
        [name_suffix] => 
        [name_first] => Ronald
        [demo_ethnicity] => Caucasian
        [demo_gender] => Male
        [_account_name] => duane joslin
        [demo_region_c] => 4 
        [type] => V
        [__id] => 7155
        [acct_SYS::preferred_DB] => 25
        [zzzCreateDate] => 05/04/2015 10:43:43
        [zzzCreateBy] => webuser
        [_isActive] => 1
    )

[9] => Array
    (
        [recid] => 93459
        [name_middle] => 
        [name_last] => Klecka
        [name_suffix] => 
        [name_first] => Linda
        [demo_ethnicity] => 
        [demo_gender] => Female
        [_account_name] => Circlemfarm270@gmail.com
        [demo_region_c] => 4
        [note_acct] => 
        [type] => V
        [__id] => 5043
        [acct_SYS::preferred_DB] => 25
        [zzzCreateDate] => 09/11/2010 09:06:20
        [zzzCreateBy] => php
        [_isActive] => 1
    )

[10] => Array
    (
        [recid] => 95655
        [name_middle] => Lee
        [name_last] => Lassere
        [name_suffix] => 
        [name_first] => Jason
        [demo_ethnicity] => Caucasian
        [demo_gender] => Male
        [_account_name] => jlclem
        [demo_region_c] => 4
        [type] => V
        [__id] => 7029
        [acct_SYS::preferred_DB] => 25
        [zzzCreateDate] => 02/06/2015 16:47:28
        [zzzCreateBy] => webuser
        [_isActive] => 1
    )

[11] => Array
    (
        [recid] => 94737
        [name_middle] => R
        [name_last] => Lewis
        [name_suffix] => 
        [name_first] => David
        [demo_ethnicity] => Caucasian
        [demo_gender] => Male
        [_account_name] => dr david
        [demo_region_c] => 4
        [note_acct] => 
        [type] => V
        [__id] => 6111
        [acct_SYS::preferred_DB] => 25
        [zzzCreateDate] => 09/29/2014 11:10:37
        [zzzCreateBy] => webuser
        [_isActive] => 1
    )

[12] => Array
    (
        [recid] => 95869
        [name_middle] => 
        [name_last] => Lindahl
        [name_suffix] => 
        [name_first] => Dan
        [demo_ethnicity] => Caucasian
        [demo_gender] => Male
        [_account_name] => dandaman1515
        [demo_region_c] => 4
        [type] => V
        [__id] => 7243
        [acct_SYS::preferred_DB] => 25
        [zzzCreateDate] => 07/08/2015 12:55:14
        [zzzCreateBy] => webuser
        [_isActive] => 1
    )

[13] => Array
    (
        [recid] => 93462
        [name_middle] => T
        [name_last] => Mancinas
        [name_suffix] => 
        [name_first] => Arthur
        [demo_ethnicity] => Hispanic
        [demo_gender] => Male
        [_account_name] => 
        [demo_region_c] => 4
        [type] => V
        [__id] => 5046
        [acct_SYS::preferred_DB] => 25
        [zzzCreateDate] => 09/14/2010 01:10:12
        [zzzCreateBy] => php
        [_isActive] => 1
    )

[14] => Array
    (
        [recid] => 96064
        [name_middle] => Jean
        [name_last] => Mann
        [name_suffix] => 
        [name_first] => Betty
        [demo_ethnicity] => Caucasian
        [demo_gender] => Female
        [_account_name] => Betty Mann
        [demo_region_c] => 4
        [type] => V
        [__id] => 7438
        [acct_SYS::preferred_DB] => 25
        [zzzCreateDate] => 08/27/2015 14:40:16
        [zzzCreateBy] => webuser
        [_isActive] => 1
    )

[15] => Array
    (
        [recid] => 93649
        [name_middle] => R
        [name_last] => Mitchell
        [name_suffix] => 
        [name_first] => Christopher
        [demo_ethnicity] => 
        [demo_gender] => Male
        [_account_name] => 
        [demo_region_c] => 4
        [type] => V
        [__id] => 5233
        [acct_SYS::preferred_DB] => 25
        [zzzCreateDate] => 01/02/2012 12:29:08
        [zzzCreateBy] => php
        [_isActive] => 1
    )

[16] => Array
    (
        [recid] => 95562
        [name_middle] => 
        [name_last] => Olson
        [name_suffix] => 
        [name_first] => Dustin
        [demo_ethnicity] => 
        [demo_gender] => Male
        [_account_name] => Dmolson
        [demo_region_c] => 4
        [type] => V
        [__id] => 6936
        [acct_SYS::preferred_DB] => 25
        [zzzCreateDate] => 01/09/2015 09:05:09
        [zzzCreateBy] => webuser
        [_isActive] => 1
    )

[17] => Array
    (
        [recid] => 96320
        [name_middle] => Elena
        [name_last] => Ortiz
        [name_suffix] => 
        [name_first] => Rosa
        [demo_ethnicity] => Hispanic
        [demo_gender] => Female
        [_account_name] => reo1986
        [demo_region_c] => 4
        [type] => V
        [__id] => 7694
        [acct_SYS::preferred_DB] => 25
        [zzzCreateDate] => 09/22/2015 10:18:31
        [zzzCreateBy] => webuser
        [_isActive] => 1
    )

[18] => Array
    (
        [recid] => 97179
        [name_middle] => David
        [name_last] => Pearson
        [name_suffix] => 
        [name_first] => Josh
        [demo_ethnicity] => Caucasian
        [demo_gender] => Male
        [_account_name] => joshpearson88
        [demo_region_c] => 4
        [type] => V
        [__id] => 8553
        [acct_SYS::preferred_DB] => 25
        [zzzCreateDate] => 05/27/2016 10:10:38
        [zzzCreateBy] => webuser
        [_isActive] => 1
    )

[19] => Array
    (
        [recid] => 95429
        [name_middle] => Jacob
        [name_last] => Reed
        [name_suffix] => 
        [name_first] => Bernard
        [demo_ethnicity] => Caucasian
        [demo_gender] => Male
        [_account_name] => Reed11
        [demo_region_c] => 4
        [type] => V
        [__id] => 6803
        [acct_SYS::preferred_DB] => 25
        [zzzCreateDate] => 12/17/2014 15:22:19
        [zzzCreateBy] => webuser
        [_isActive] => 1
    )

[20] => Array
    (
        [recid] => 96983
        [name_middle] => Roberto
        [name_last] => Reyna
        [name_suffix] => 
        [name_first] => Ricardo
        [demo_ethnicity] => Hispanic
        [demo_gender] => Male
        [_account_name] => RicardoRReyna
        [demo_region_c] => 4
        [type] => V
        [__id] => 8357
        [acct_SYS::preferred_DB] => 25
        [zzzCreateDate] => 01/13/2016 09:12:07
        [zzzCreateBy] => webuser
        [_isActive] => 1
    )

[21] => Array
    (
        [recid] => 94710
        [name_middle] => 
        [name_last] => Soefje
        [name_suffix] => 
        [name_first] => Aaron
        [demo_ethnicity] => Caucasian
        [demo_gender] => Male
        [_account_name] => sefgoodman
        [demo_region_c] => 4
        [type] => V
        [__id] => 6084
        [acct_SYS::preferred_DB] => 25
        [zzzCreateDate] => 09/26/2014 18:38:55
        [zzzCreateBy] => webuser
        [_isActive] => 1
    )

[22] => Array
    (
        [recid] => 94712
        [name_middle] => 
        [name_last] => Soefje
        [name_suffix] => 
        [name_first] => Ron 
        [demo_ethnicity] => Caucasian
        [demo_gender] => Male
        [_account_name] => Ron Soefje
        [demo_region_c] => 4
        [type] => V
        [__id] => 6086
        [acct_SYS::preferred_DB] => 25
        [zzzCreateDate] => 09/26/2014 18:50:02
        [zzzCreateBy] => webuser
        [_isActive] => 1
    )

[23] => Array
    (
        [recid] => 93533
        [name_middle] => 
        [name_last] => Starr
        [name_suffix] => 
        [name_first] => Kara
        [demo_ethnicity] => 
        [demo_gender] => Female
        [demo_region_c] => 4 
        [type] => V
        [__id] => 5117
        [acct_SYS::preferred_DB] => 25
        [zzzCreateDate] => 02/14/2011 15:30:51
        [zzzCreateBy] => php
        [_isActive] => 1
    )

[24] => Array
    (
        [recid] => 94279
        [name_middle] => 
        [name_last] => Starr
        [name_suffix] => 
        [name_first] => John
        [demo_ethnicity] => Caucasion
        [demo_gender] => Male
        [demo_region_c] => 4
        [type] => V
        [__id] => 5653
        [acct_SYS::preferred_DB] => 25
        [zzzCreateDate] => 07/28/2014 10:28:02
        [zzzCreateBy] => webuser
        [_isActive] => 1
    )

[25] => Array
    (
        [recid] => 93892
        [name_middle] => K
        [name_last] => Steubing
        [name_suffix] => 
        [name_first] => William
        [demo_ethnicity] => 
        [demo_gender] => Male
        [demo_region_c] => 4
        [type] => V
        [__id] => 5476
        [acct_SYS::preferred_DB] => 25
        [zzzCreateDate] => 02/24/2014 16:08:41
        [zzzCreateBy] => php
        [_isActive] => 1
    )

[26] => Array
    (
        [recid] => 93510
        [name_middle] => 
        [name_last] => Swann
        [name_suffix] => 
        [name_first] => Brad
        [demo_ethnicity] => Caucasian
        [demo_gender] => Male
        [demo_region_c] => 4
        [type] => V
        [__id] => 5094
        [acct_SYS::preferred_DB] => 25
        [zzzCreateDate] => 11/30/2010 11:57:03
        [zzzCreateBy] => php
        [_isActive] => 1
    )

[27] => Array
    (
        [recid] => 94151
        [name_middle] => 
        [name_last] => Test
        [name_suffix] => Jr.
        [name_first] => BScheib
        [demo_ethnicity] => Caucasian
        [demo_gender] => Female
        [demo_region_c] => 4
        [__id] => 5525
        [acct_SYS::preferred_DB] => 25
        [zzzCreateDate] => 06/23/2014 12:56:55
        [zzzCreateBy] => webuser
        [_isActive] => 1
    )

[28] => Array
    (
        [recid] => 93905
        [name_middle] => E
        [name_last] => Thompson
        [name_suffix] => 
        [name_first] => John
        [demo_ethnicity] => Caucasian
        [demo_gender] => Male
        [demo_region_c] => 4
        [__id] => 5489
        [acct_SYS::preferred_DB] => 25
        [zzzCreateDate] => 03/24/2014 11:39:31
        [zzzCreateBy] => Admin
        [_isActive] => 1
    )

这是$ newCook数组的转储:

Array
(
[0] => Array
    (
        [ID] => 7248
        [name_first] => Joe
        [name_last] => Buck
        [region] => 4
    )

[1] => Array
    (
        [ID] => 6821
        [name_first] => David 
        [name_last] => Campbell
        [region] => 4
    )

[2] => Array
    (
        [ID] => 5333
        [name_first] => Lisa
        [name_last] => Carman
        [region] => 4
    )

[3] => Array
    (
        [ID] => 5758
        [name_first] => Koy
        [name_last] => Coffer
        [region] => 4
    )

[4] => Array
    (
        [ID] => 5053
        [name_first] => David
        [name_last] => Corona
        [region] => 4
    )

[5] => Array
    (
        [ID] => 7240
        [name_first] => Dan
        [name_last] => Griffin
        [region] => 4
    )

[6] => Array
    (
        [ID] => 6689
        [name_first] => Mario
        [name_last] => Guzman
        [region] => 4
    )

[7] => Array
    (
        [ID] => 5776
        [name_first] => Bryan
        [name_last] => Jones
        [region] => 4
    )

[8] => Array
    (
        [ID] => 7155
        [name_first] => Ronald
        [name_last] => Joslin
        [region] => 4
    )

[9] => Array
    (
        [ID] => 5043
        [name_first] => Linda
        [name_last] => Klecka
        [region] => 4
    )

[10] => Array
    (
        [ID] => 7029
        [name_first] => Jason
        [name_last] => Lassere
        [region] => 4
    )

[11] => Array
    (
        [ID] => 6111
        [name_first] => David
        [name_last] => Lewis
        [region] => 4
    )

[12] => Array
    (
        [ID] => 7243
        [name_first] => Dan
        [name_last] => Lindahl
        [region] => 4
    )

[13] => Array
    (
        [ID] => 5046
        [name_first] => Arthur
        [name_last] => Mancinas
        [region] => 4
    )

[14] => Array
    (
        [ID] => 7438
        [name_first] => Betty
        [name_last] => Mann
        [region] => 4
    )

[15] => Array
    (
        [ID] => 5233
        [name_first] => Christopher
        [name_last] => Mitchell
        [region] => 4
    )

[16] => Array
    (
        [ID] => 6936
        [name_first] => Dustin
        [name_last] => Olson
        [region] => 4
    )

[17] => Array
    (
        [ID] => 7694
        [name_first] => Rosa
        [name_last] => Ortiz
        [region] => 4
    )

[18] => Array
    (
        [ID] => 8553
        [name_first] => Josh
        [name_last] => Pearson
        [region] => 4
    )

[19] => Array
    (
        [ID] => 6803
        [name_first] => Bernard
        [name_last] => Reed
        [region] => 4
    )

[20] => Array
    (
        [ID] => 8357
        [name_first] => Ricardo
        [name_last] => Reyna
        [region] => 4
    )

[21] => Array
    (
        [ID] => 6084
        [name_first] => Aaron
        [name_last] => Soefje
        [region] => 4
    )

[22] => Array
    (
        [ID] => 6086
        [name_first] => Ron 
        [name_last] => Soefje
        [region] => 4
    )

[23] => Array
    (
        [ID] => 5117
        [name_first] => Kara
        [name_last] => Starr
        [region] => 4
    )

[24] => Array
    (
        [ID] => 5653
        [name_first] => John
        [name_last] => Starr
        [region] => 4
    )

[25] => Array
    (
        [ID] => 5476
        [name_first] => William
        [name_last] => Steubing
        [region] => 4
    )

[26] => Array
    (
        [ID] => 5094
        [name_first] => Brad
        [name_last] => Swann
        [region] => 4
    )

[27] => Array
    (
        [ID] => 5525
        [name_first] => BScheib
        [name_last] => Test
        [region] => 4
    )

[28] => Array
    (
        [ID] => 5489
        [name_first] => John
        [name_last] => Thompson
        [region] => 4
    )

[29] => Array
    (
        [ID] => 7248
        [name_first] => Joe
        [name_last] => Buck
        [region] => 4
    )

[30] => Array
    (
        [ID] => 6821
        [name_first] => David 
        [name_last] => Campbell
        [region] => 4
    )

[31] => Array
    (
        [ID] => 5333
        [name_first] => Lisa
        [name_last] => Carman
        [region] => 4
    )

[32] => Array
    (
        [ID] => 5758
        [name_first] => Koy
        [name_last] => Coffer
        [region] => 4
    )

[33] => Array
    (
        [ID] => 5053
        [name_first] => David
        [name_last] => Corona
        [region] => 4
    )

[34] => Array
    (
        [ID] => 7240
        [name_first] => Dan
        [name_last] => Griffin
        [region] => 4
    )

[35] => Array
    (
        [ID] => 6689
        [name_first] => Mario
        [name_last] => Guzman
        [region] => 4
    )

[36] => Array
    (
        [ID] => 5776
        [name_first] => Bryan
        [name_last] => Jones
        [region] => 4
    )

[37] => Array
    (
        [ID] => 7155
        [name_first] => Ronald
        [name_last] => Joslin
        [region] => 4
    )

[38] => Array
    (
        [ID] => 5043
        [name_first] => Linda
        [name_last] => Klecka
        [region] => 4
    )

[39] => Array
    (
        [ID] => 7029
        [name_first] => Jason
        [name_last] => Lassere
        [region] => 4
    )

[40] => Array
    (
        [ID] => 6111
        [name_first] => David
        [name_last] => Lewis
        [region] => 4
    )

[41] => Array
    (
        [ID] => 7243
        [name_first] => Dan
        [name_last] => Lindahl
        [region] => 4
    )

[42] => Array
    (
        [ID] => 5046
        [name_first] => Arthur
        [name_last] => Mancinas
        [region] => 4
    )

[43] => Array
    (
        [ID] => 7438
        [name_first] => Betty
        [name_last] => Mann
        [region] => 4
    )

[44] => Array
    (
        [ID] => 5233
        [name_first] => Christopher
        [name_last] => Mitchell
        [region] => 4
    )

[45] => Array
    (
        [ID] => 6936
        [name_first] => Dustin
        [name_last] => Olson
        [region] => 4
    )

[46] => Array
    (
        [ID] => 7694
        [name_first] => Rosa
        [name_last] => Ortiz
        [region] => 4
    )

[47] => Array
    (
        [ID] => 8553
        [name_first] => Josh
        [name_last] => Pearson
        [region] => 4
    )

[48] => Array
    (
        [ID] => 6803
        [name_first] => Bernard
        [name_last] => Reed
        [region] => 4
    )

[49] => Array
    (
        [ID] => 8357
        [name_first] => Ricardo
        [name_last] => Reyna
        [region] => 4
    )

[50] => Array
    (
        [ID] => 6084
        [name_first] => Aaron
        [name_last] => Soefje
        [region] => 4
    )

[51] => Array
    (
        [ID] => 6086
        [name_first] => Ron 
        [name_last] => Soefje
        [region] => 4
    )

[52] => Array
    (
        [ID] => 5117
        [name_first] => Kara
        [name_last] => Starr
        [region] => 4
    )

[53] => Array
    (
        [ID] => 5653
        [name_first] => John
        [name_last] => Starr
        [region] => 4
    )

[54] => Array
    (
        [ID] => 5476
        [name_first] => William
        [name_last] => Steubing
        [region] => 4
    )

[55] => Array
    (
        [ID] => 5094
        [name_first] => Brad
        [name_last] => Swann
        [region] => 4
    )

[56] => Array
    (
        [ID] => 5525
        [name_first] => BScheib
        [name_last] => Test
        [region] => 4
    )

[57] => Array
    (
        [ID] => 5489
        [name_first] => John
        [name_last] => Thompson
        [region] => 4
    )

1 个答案:

答案 0 :(得分:0)

谢谢大家。正确的答案是之前声明数组。谢谢imtheman!