Link to home
Start Free TrialLog in
Avatar of Eddie Shipman
Eddie ShipmanFlag for United States of America

asked on

Parsing array, only want 2 from each key.

I have the following array:
array(
    "536-01033L" => array (
                        0 => array ("id" => "BB8", "company" => "CS (BB8)", "part" => "CALIPER", "age" => "543", "cond" => "A", "stock" => "ZZ0759", "ic" => "536-01033L", "holl" => "536", "price" => "35.00", "qty" => "1", "location" => "1E44", "comments" => "LH,SE"),
                        1 => array ("id" => "BB8", "company" => "CS (BB8)", "part" => "CALIPER", "age" => "459", "cond" => "A", "stock" => "170032", "ic" => "536-01033L", "holl" => "536", "price" => "35.00", "qty" => "1", "location" => "1I29", "comments" => "LH,SXT,ABS"),
                        2 => array ("id" => "BB8", "company" => "CS (BB8)", "part" => "CALIPER", "age" => "431", "cond" => "B", "stock" => "170060", "ic" => "536-01033L", "holl" => "536", "price" => "35.00", "qty" => "1", "location" => "1I20", "comments" => "LH,ABS"),
                        3 => array ("id" => "BB8", "company" => "CS (BB8)", "part" => "CALIPER", "age" => "388", "cond" => "A", "stock" => "170101", "ic" => "536-01033L", "holl" => "536", "price" => "35.00", "qty" => "1", "location" => "1K08", "comments" => "LH"),
                        4 => array ("id" => "BB8", "company" => "CS (BB8)", "part" => "CALIPER", "age" => "372", "cond" => "A", "stock" => "170130", "ic" => "536-01033L", "holl" => "536", "price" => "35.00", "qty" => "1", "location" => "1N05", "comments" => "LH,ABS"),
                        5 => array ("id" => "BB8", "company" => "CS (BB8)", "part" => "CALIPER", "age" => "270", "cond" => "A", "stock" => "170189", "ic" => "536-01033L", "holl" => "536", "price" => "35.00", "qty" => "1", "location" => "1D26", "comments" => "LH"),
                        6 => array ("id" => "BB8", "company" => "CS (BB8)", "part" => "CALIPER", "age" => "210", "cond" => "A", "stock" => "170211", "ic" => "536-01033L", "holl" => "536", "price" => "35.00", "qty" => "1", "location" => "1T01", "comments" => "LH,SE,ABS"),
                        7 => array ("id" => "BB8", "company" => "CS (BB8)", "part" => "CALIPER", "age" => "63", "cond" => "A", "stock" => "180306", "ic" => "536-01033L", "holl" => "536", "price" => "35.00", "qty" => "1", "location" => "1B19", "comments" => "LH,REAR, L."),
                        8 => array ("id" => "BB8", "company" => "CS (BB8)", "part" => "CALIPER", "age" => "38", "cond" => "A", "stock" => "S06738", "ic" => "536-01033L", "holl" => "536", "price" => "35.00", "qty" => "1", "location" => "1C36", "comments" => "LH,ABS,REAR, L."),
                        9 => array ("id" => "KY1", "company" => "BAP (KY1)", "part" => "CALIPER", "age" => "35", "cond" => "A", "stock" => "F0050628", "ic" => "536-01033L", "holl" => "536", "price" => "10.00", "qty" => "1", "location" => "TRU044", "comments" => ""),
                        10 => array ("id" => "KY1", "company" => "BAP (KY1)", "part" => "CALIPER", "age" => "31", "cond" => "A", "stock" => "F0050688", "ic" => "536-01033L", "holl" => "536", "price" => "10.00", "qty" => "1", "location" => "TRU057", "comments" => ""),
                        11 => array ("id" => "KY1", "company" => "BAP (KY1)", "part" => "CALIPER", "age" => "11", "cond" => "A", "stock" => "F0051105", "ic" => "536-01033L", "holl" => "536", "price" => "10.00", "qty" => "1", "location" => "TRU044", "comments" => ""),
                        12 => array ("id" => "KY2", "company" => "BGAP (KY2)", "part" => "CALIPER", "age" => "667", "cond" => "A", "stock" => "B6H112", "ic" => "536-01033L", "holl" => "536", "price" => "35.00", "qty" => "1", "location" => "Y02I05", "comments" => ""),
                        13 => array ("id" => "KY2", "company" => "BGAP (KY2)", "part" => "CALIPER", "age" => "576", "cond" => "A", "stock" => "B6K193", "ic" => "536-01033L", "holl" => "536", "price" => "35.00", "qty" => "1", "location" => "Y02L06", "comments" => ""),
                        14 => array ("id" => "KY2", "company" => "BGAP (KY2)", "part" => "CALIPER", "age" => "528", "cond" => "A", "stock" => "B6L236", "ic" => "536-01033L", "holl" => "536", "price" => "35.00", "qty" => "1", "location" => "Y02P04", "comments" => ""),
                        15 => array ("id" => "KY2", "company" => "BGAP (KY2)", "part" => "CALIPER", "age" => "403", "cond" => "A", "stock" => "B7E079", "ic" => "536-01033L", "holl" => "536", "price" => "35.00", "qty" => "1", "location" => "Y02R12", "comments" => ""),
                        16 => array ("id" => "KY2", "company" => "BGAP (KY2)", "part" => "CALIPER", "age" => "340", "cond" => "A", "stock" => "B7E099", "ic" => "536-01033L", "holl" => "536", "price" => "35.00", "qty" => "1", "location" => "Y01T07", "comments" => ""),
                        17 => array ("id" => "KY2", "company" => "BGAP (KY2)", "part" => "CALIPER", "age" => "269", "cond" => "A", "stock" => "B7G138", "ic" => "536-01033L", "holl" => "536", "price" => "35.00", "qty" => "1", "location" => "Y02C01", "comments" => ""),
                        18 => array ("id" => "KY2", "company" => "BGAP (KY2)", "part" => "CALIPER", "age" => "239", "cond" => "A", "stock" => "B7J204", "ic" => "536-01033L", "holl" => "536", "price" => "35.00", "qty" => "1", "location" => "Y03D11", "comments" => ""),
                        19 => array ("id" => "KY2", "company" => "BGAP (KY2)", "part" => "CALIPER", "age" => "190", "cond" => "A", "stock" => "B7K232", "ic" => "536-01033L", "holl" => "536", "price" => "35.00", "qty" => "1", "location" => "MCICOR1", "comments" => ""),
                        20 => array ("id" => "MD6", "company" => "BWAP (MD6)", "part" => "CALIPER", "age" => "2", "cond" => "A", "stock" => "8A415", "ic" => "536-01033L", "holl" => "536", "price" => "12.00", "qty" => "1", "location" => "LOT-23", "comments" => ""),
                        21 => array ("id" => "M06", "company" => "MAP (M06)", "part" => "CALIPER", "age" => "162", "cond" => "A", "stock" => "1710131", "ic" => "536-01033L", "holl" => "536", "price" => "50.00", "qty" => "1", "location" => "KCA-10", "comments" => "128K"),
                        22 => array ("id" => "M06", "company" => "MAP (M06)", "part" => "CALIPER", "age" => "137", "cond" => "A", "stock" => "1800459", "ic" => "536-01033L", "holl" => "536", "price" => "50.00", "qty" => "1", "location" => "KCD-07", "comments" => "115K"),
                        23 => array ("id" => "M06", "company" => "MAP (M06)", "part" => "CALIPER", "age" => "46", "cond" => "A", "stock" => "1802696", "ic" => "536-01033L", "holl" => "536", "price" => "50.00", "qty" => "1", "location" => "KCE-14", "comments" => "144K"),
                        24 => array ("id" => "M06", "company" => "MAP (M06)", "part" => "CALIPER", "age" => "10", "cond" => "A", "stock" => "1803621", "ic" => "536-01033L", "holl" => "536", "price" => "50.00", "qty" => "1", "location" => "KCDM-A-09", "comments" => "130K"),
                        25 => array ("id" => "M06", "company" => "MAP (M06)", "part" => "CALIPER", "age" => "4", "cond" => "A", "stock" => "1803780", "ic" => "536-01033L", "holl" => "536", "price" => "50.00", "qty" => "1", "location" => "KCDM-A-20", "comments" => "136K"),
                        26 => array ("id" => "NC1", "company" => "APS (NC1)", "part" => "CALIPER", "age" => "135", "cond" => "A", "stock" => "DD1143", "ic" => "536-01033L", "holl" => "536", "price" => "35.00", "qty" => "1", "location" => "Y14B45", "comments" => "IN CAR 8EXC"),
                        27 => array ("id" => "TN9", "company" => "MSAP (TN9)", "part" => "CALIPER", "age" => "150", "cond" => "A", "stock" => "24092", "ic" => "536-01033L", "holl" => "536", "price" => "35.00", "qty" => "1", "location" => "13C33", "comments" => "ABS,LR"),
                        28 => array ("id" => "VA2", "company" => "CBAP (VA2)", "part" => "CALIPER", "age" => "371", "cond" => "A", "stock" => "CPC1187", "ic" => "536-01033L", "holl" => "536", "price" => "25.00", "qty" => "1", "location" => "BIN360A52", "comments" => ""),
                        29 => array ("id" => "VA2", "company" => "CBAP (VA2)", "part" => "CALIPER", "age" => "332", "cond" => "A", "stock" => "CPA1590", "ic" => "536-01033L", "holl" => "536", "price" => "25.00", "qty" => "1", "location" => "32.BJC.3", "comments" => ""),
                        30 => array ("id" => "VA2", "company" => "CBAP (VA2)", "part" => "CALIPER", "age" => "585", "cond" => "A", "stock" => "CNT2318", "ic" => "536-01033L", "holl" => "536", "price" => "15.00", "qty" => "1", "location" => "BIN360A48", "comments" => ""),
                        31 => array ("id" => "VA2", "company" => "CBAP (VA2)", "part" => "CALIPER", "age" => "452", "cond" => "A", "stock" => "CPC542", "ic" => "536-01033L", "holl" => "536", "price" => "12.00", "qty" => "1", "location" => "BIN360A34", "comments" => ""),
                        32 => array ("id" => "VA2", "company" => "CBAP (VA2)", "part" => "CALIPER", "age" => "444", "cond" => "A", "stock" => "CPA624", "ic" => "536-01033L", "holl" => "536", "price" => "12.00", "qty" => "1", "location" => "BIN360A32", "comments" => ""),
                        33 => array ("id" => "VA2", "company" => "CBAP (VA2)", "part" => "CALIPER", "age" => "541", "cond" => "A", "stock" => "CNC2667", "ic" => "536-01033L", "holl" => "536", "price" => "10.00", "qty" => "1", "location" => "BIN360A34", "comments" => ""),
                        34 => array ("id" => "VA2", "company" => "CBAP (VA2)", "part" => "CALIPER", "age" => "540", "cond" => "A", "stock" => "CNC2586", "ic" => "536-01033L", "holl" => "536", "price" => "10.00", "qty" => "1", "location" => "BIN360A17", "comments" => ""),
                        35 => array ("id" => "VA1", "company" => "AFUAP (VA1)", "part" => "CALIPER", "age" => "92", "cond" => "A", "stock" => "388078", "ic" => "536-01033L", "holl" => "536", "price" => "55.00", "qty" => "1", "location" => "W03D109", "comments" => ""),
                        36 => array ("id" => "VA1", "company" => "AFUAP (VA1)", "part" => "CALIPER", "age" => "91", "cond" => "A", "stock" => "388395", "ic" => "536-01033L", "holl" => "536", "price" => "55.00", "qty" => "1", "location" => "W03D98", "comments" => ""),
                        37 => array ("id" => "VA8", "company" => "GAS (VA8)", "part" => "CALIPER", "age" => "22", "cond" => "A", "stock" => "8E6669", "ic" => "536-01033L", "holl" => "536", "price" => "10.00", "qty" => "1", "location" => "TRUE CORE", "comments" => ""),
                        38 => array ("id" => "WV3", "company" => "APO (WV3)", "part" => "CALIPER", "age" => "325", "cond" => "A", "stock" => "G7G234", "ic" => "536-01033L", "holl" => "536", "price" => "35.00", "qty" => "1", "location" => "Z05B35", "comments" => ""),
        ),
    "536-01036" => array (
                        0 => array ("id" => "M21", "company" => "BWUPI (M21)", "part" => "CALIPER", "age" => "122", "cond" => "A", "stock" => "8A065", "ic" => "536-01036", "holl" => "536", "price" => "15.00", "qty" => "1", "location" => "36AU", "comments" => ""),
                        1 => array ("id" => "M22", "company" => "BWSS (M22)", "part" => "CALIPER", "age" => "122", "cond" => "A", "stock" => "8A065", "ic" => "536-01036", "holl" => "536", "price" => "15.00", "qty" => "1", "location" => "36AU", "comments" => ""),
                        2 => array ("id" => "M23", "company" => "BWA (M23)", "part" => "CALIPER", "age" => "122", "cond" => "A", "stock" => "8A065", "ic" => "536-01036", "holl" => "536", "price" => "15.00", "qty" => "1", "location" => "36AU", "comments" => ""),
                        3 => array ("id" => "VA2", "company" => "CBAP (VA2)", "part" => "CALIPER", "age" => "357", "cond" => "A", "stock" => "CPA1257", "ic" => "536-01036", "holl" => "536", "price" => "12.00", "qty" => "1", "location" => "BIN.360.A", "comments" => ""),
        ),
    "536-01037" => array (
                        0 => array ("id" => "KY1", "company" => "BAP (KY1)", "part" => "CALIPER", "age" => "16", "cond" => "A", "stock" => "F0050893", "ic" => "536-01037", "holl" => "536", "price" => "15.00", "qty" => "1", "location" => "HX0208", "comments" => ""),
                        1 => array ("id" => "VA2", "company" => "CBAP (VA2)", "part" => "CALIPER", "age" => "417", "cond" => "A", "stock" => "CPA779", "ic" => "536-01037", "holl" => "536", "price" => "12.00", "qty" => "1", "location" => "BIN360A42", "comments" => ""),
        ),
    )

Open in new window

What I want to do is take the FIRST 2 entries from each id and put it into another array with same structure. If there is nly one entry, then it should be that one.

Resulting array would look like this:
array(
    "536-01033L" => array (
                        0 => array ("id" => "BB8", "company" => "CS (BB8)", "part" => "CALIPER", "age" => "543", "cond" => "A", "stock" => "ZZ0759", "ic" => "536-01033L", "holl" => "536", "price" => "35.00", "qty" => "1", "location" => "1E44", "comments" => "LH,SE"),
                        1 => array ("id" => "BB8", "company" => "CS (BB8)", "part" => "CALIPER", "age" => "459", "cond" => "A", "stock" => "170032", "ic" => "536-01033L", "holl" => "536", "price" => "35.00", "qty" => "1", "location" => "1I29", "comments" => "LH,SXT,ABS"),
                        2 => array ("id" => "KY1", "company" => "BAP (KY1)", "part" => "CALIPER", "age" => "35", "cond" => "A", "stock" => "F0050628", "ic" => "536-01033L", "holl" => "536", "price" => "10.00", "qty" => "1", "location" => "TRU044", "comments" => ""),
                        3 => array ("id" => "KY1", "company" => "BAP (KY1)", "part" => "CALIPER", "age" => "31", "cond" => "A", "stock" => "F0050688", "ic" => "536-01033L", "holl" => "536", "price" => "10.00", "qty" => "1", "location" => "TRU057", "comments" => ""),
                        4 => array ("id" => "KY2", "company" => "BGAP (KY2)", "part" => "CALIPER", "age" => "667", "cond" => "A", "stock" => "B6H112", "ic" => "536-01033L", "holl" => "536", "price" => "35.00", "qty" => "1", "location" => "Y02I05", "comments" => ""),
                        5 => array ("id" => "KY2", "company" => "BGAP (KY2)", "part" => "CALIPER", "age" => "576", "cond" => "A", "stock" => "B6K193", "ic" => "536-01033L", "holl" => "536", "price" => "35.00", "qty" => "1", "location" => "Y02L06", "comments" => ""),
                        6 => array ("id" => "MD6", "company" => "BWAP (MD6)", "part" => "CALIPER", "age" => "2", "cond" => "A", "stock" => "8A415", "ic" => "536-01033L", "holl" => "536", "price" => "12.00", "qty" => "1", "location" => "LOT-23", "comments" => ""),
                        7 => array ("id" => "M06", "company" => "MAP (M06)", "part" => "CALIPER", "age" => "162", "cond" => "A", "stock" => "1710131", "ic" => "536-01033L", "holl" => "536", "price" => "50.00", "qty" => "1", "location" => "KCA-10", "comments" => "128K"),
                        8 => array ("id" => "NC1", "company" => "APS (NC1)", "part" => "CALIPER", "age" => "135", "cond" => "A", "stock" => "DD1143", "ic" => "536-01033L", "holl" => "536", "price" => "35.00", "qty" => "1", "location" => "Y14B45", "comments" => "IN CAR 8EXC"),
                        9 => array ("id" => "TN9", "company" => "MSAP (TN9)", "part" => "CALIPER", "age" => "150", "cond" => "A", "stock" => "24092", "ic" => "536-01033L", "holl" => "536", "price" => "35.00", "qty" => "1", "location" => "13C33", "comments" => "ABS,LR"),
                        10 => array ("id" => "VA2", "company" => "CBAP (VA2)", "part" => "CALIPER", "age" => "371", "cond" => "A", "stock" => "CPC1187", "ic" => "536-01033L", "holl" => "536", "price" => "25.00", "qty" => "1", "location" => "BIN360A52", "comments" => ""),
                        11 => array ("id" => "VA2", "company" => "CBAP (VA2)", "part" => "CALIPER", "age" => "332", "cond" => "A", "stock" => "CPA1590", "ic" => "536-01033L", "holl" => "536", "price" => "25.00", "qty" => "1", "location" => "32.BJC.3", "comments" => ""),
                        12 => array ("id" => "VA1", "company" => "AFUAP (VA1)", "part" => "CALIPER", "age" => "92", "cond" => "A", "stock" => "388078", "ic" => "536-01033L", "holl" => "536", "price" => "55.00", "qty" => "1", "location" => "W03D109", "comments" => ""),
                        13 => array ("id" => "VA1", "company" => "AFUAP (VA1)", "part" => "CALIPER", "age" => "91", "cond" => "A", "stock" => "388395", "ic" => "536-01033L", "holl" => "536", "price" => "55.00", "qty" => "1", "location" => "W03D98", "comments" => ""),
                        14 => array ("id" => "VA8", "company" => "GAS (VA8)", "part" => "CALIPER", "age" => "22", "cond" => "A", "stock" => "8E6669", "ic" => "536-01033L", "holl" => "536", "price" => "10.00", "qty" => "1", "location" => "TRUE CORE", "comments" => ""),
                        15 => array ("id" => "WV3", "company" => "APO (WV3)", "part" => "CALIPER", "age" => "325", "cond" => "A", "stock" => "G7G234", "ic" => "536-01033L", "holl" => "536", "price" => "35.00", "qty" => "1", "location" => "Z05B35", "comments" => ""),
        ),
    "536-01036" => array (
                        0 => array ("id" => "M21", "company" => "BWUPI (M21)", "part" => "CALIPER", "age" => "122", "cond" => "A", "stock" => "8A065", "ic" => "536-01036", "holl" => "536", "price" => "15.00", "qty" => "1", "location" => "36AU", "comments" => ""),
                        1 => array ("id" => "M22", "company" => "BWSS (M22)", "part" => "CALIPER", "age" => "122", "cond" => "A", "stock" => "8A065", "ic" => "536-01036", "holl" => "536", "price" => "15.00", "qty" => "1", "location" => "36AU", "comments" => ""),
                        2 => array ("id" => "M23", "company" => "BWA (M23)", "part" => "CALIPER", "age" => "122", "cond" => "A", "stock" => "8A065", "ic" => "536-01036", "holl" => "536", "price" => "15.00", "qty" => "1", "location" => "36AU", "comments" => ""),
                        3 => array ("id" => "VA2", "company" => "CBAP (VA2)", "part" => "CALIPER", "age" => "357", "cond" => "A", "stock" => "CPA1257", "ic" => "536-01036", "holl" => "536", "price" => "12.00", "qty" => "1", "location" => "BIN.360.A", "comments" => ""),
        ),
    "536-01037" => array (
                        0 => array ("id" => "KY1", "company" => "BAP (KY1)", "part" => "CALIPER", "age" => "16", "cond" => "A", "stock" => "F0050893", "ic" => "536-01037", "holl" => "536", "price" => "15.00", "qty" => "1", "location" => "HX0208", "comments" => ""),
                        1 => array ("id" => "VA2", "company" => "CBAP (VA2)", "part" => "CALIPER", "age" => "417", "cond" => "A", "stock" => "CPA779", "ic" => "536-01037", "holl" => "536", "price" => "12.00", "qty" => "1", "location" => "BIN360A42", "comments" => ""),
        ),
    )

Open in new window

Avatar of Swatantra Bhargava
Swatantra Bhargava
Flag of India image

You need to run a loop of the outer loop and use an array_slice function as below.

echo "<pre>";
print_r(array_slice($arr["536-01033L"],0,2));
Avatar of Eddie Shipman

ASKER

@Swatantra Almost there. It only pulls the first 2 from each first level array, please look at expected results above.
array(
    "536-01033L" => array (
                        0 => array ("id" => "BB8", "company" => "CS (BB8)", "part" => "CALIPER", "age" => "543", "cond" => "A", "stock" => "ZZ0759", "ic" => "536-01033L", "holl" => "536", "price" => "35.00", "qty" => "1", "location" => "1E44", "comments" => "LH,SE"),
                        1 => array ("id" => "BB8", "company" => "CS (BB8)", "part" => "CALIPER", "age" => "459", "cond" => "A", "stock" => "170032", "ic" => "536-01033L", "holl" => "536", "price" => "35.00", "qty" => "1", "location" => "1I29", "comments" => "LH,SXT,ABS"),
        ),
    "536-01036" => array (
                        0 => array ("id" => "M21", "company" => "BWUPI (M21)", "part" => "CALIPER", "age" => "122", "cond" => "A", "stock" => "8A065", "ic" => "536-01036", "holl" => "536", "price" => "15.00", "qty" => "1", "location" => "36AU", "comments" => ""),
                        1 => array ("id" => "M22", "company" => "BWSS (M22)", "part" => "CALIPER", "age" => "122", "cond" => "A", "stock" => "8A065", "ic" => "536-01036", "holl" => "536", "price" => "15.00", "qty" => "1", "location" => "36AU", "comments" => ""),
        ),
    "536-01037" => array (
                        0 => array ("id" => "KY1", "company" => "BAP (KY1)", "part" => "CALIPER", "age" => "16", "cond" => "A", "stock" => "F0050893", "ic" => "536-01037", "holl" => "536", "price" => "15.00", "qty" => "1", "location" => "HX0208", "comments" => ""),
                        1 => array ("id" => "VA2", "company" => "CBAP (VA2)", "part" => "CALIPER", "age" => "417", "cond" => "A", "stock" => "CPA779", "ic" => "536-01037", "holl" => "536", "price" => "12.00", "qty" => "1", "location" => "BIN360A42", "comments" => ""),
        ),
    )

Open in new window

Check the length of outer loop if that is > 5 pass 2 in slice function otherwise length.
What? This has to be dynamic, the array may have more or less items in each subarray
Yes... That is why we are calculating the length of the array every time.
Still don't know what you are saying, The expected result above is much bigger than 5 elements. Because you have to check for the id value and the count, array_XXXX functions will not work, here.. You have to use loops.
This code is getting close. I just can only get the first 2 of the subarray, not the first 2 of each id:

$new_arr = array();
foreach($x as $k=>$v) {
    $id = "";
    $cnt = 1;
    $new_arr[$k] = array();
    foreach($v as $ic) {
        if($id !== $ic["id"] and $cnt < 3) {
            if(is_array($ic)) {
                array_push($new_arr[$k], $ic);
            }
            $id = $ic["id"];
            $cnt++;
        }
    };
};
print_r($new_arr);

Open in new window

SOLUTION
Avatar of Swatantra Bhargava
Swatantra Bhargava
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Julian, that did it...