Home > Net >  How to compare each first index of multidimensional array and to push into another array if they are
How to compare each first index of multidimensional array and to push into another array if they are

Time:12-22

I'm going to go into more detail.

Say I have an array like this:

JSON version of the array:

[
    {
        "SKU": "COL-10-49000 ",
        "SHOP": "GWUK ",
        "FLDDEF": "Body_HTML ",
        "PARB": " ",
        "STAT1": "U ",
        "FREIA1": " ",
        "FREIA2": " "
    },
    {
        "SKU": "COL-10-49000 ",
        "SHOP": "GWUK ",
        "FLDDEF": "Metafield_3 ",
        "PARB": "icon_image_2 ",
        "STAT1": "U ",
        "FREIA1": "19000726159421 ",
        "FREIA2": " "
    },
    {
        "SKU": "COL-10-49310 ",
        "SHOP": "GWIN ",
        "FLDDEF": "SEO_1 ",
        "PARB": "title_tag ",
        "STAT1": "U ",
        "FREIA1": "19034707656748 ",
        "FREIA2": " "
    },
    {
        "SKU": "COL-10-49000 ",
        "SHOP": "GWIN ",
        "FLDDEF": "Metafield_14 ",
        "PARB": "subtitle ",
        "STAT1": " ",
        "FREIA1": "19472915890220 ",
        "FREIA2": " "
    },
    {
        "SKU": "COL-10-49000 ",
        "SHOP": "GWEUEN ",
        "FLDDEF": "Metafield_9 ",
        "PARB": "icon_label_1 ",
        "STAT1": " ",
        "FREIA1": "20117934276662 ",
        "FREIA2": " "
    },
    {
        "SKU": "COL-10-49000 ",
        "SHOP": "GWEUDE ",
        "FLDDEF": "Metafield_14 ",
        "PARB": "icon_label_3 ",
        "STAT1": "U ",
        "FREIA1": "13660255911996 ",
        "FREIA2": " "
    },
    {
        "SKU": "COL-10-49000 ",
        "SHOP": "GWIN ",
        "FLDDEF": "Body_HTML ",
        "PARB": " ",
        "STAT1": " ",
        "FREIA1": " ",
        "FREIA2": " "
    },
    {
        "SKU": "COL-10-49310 ",
        "SHOP": "GWEUDE ",
        "FLDDEF": "Metafield_3 ",
        "PARB": "image ",
        "STAT1": "U ",
        "FREIA1": "13675072946236 ",
        "FREIA2": " "
    },
    {
        "SKU": "COL-10-49309 ",
        "SHOP": "GWUK ",
        "FLDDEF": "Collection_Title ",
        "PARB": " ",
        "STAT1": "U ",
        "FREIA1": " ",
        "FREIA2": " "
    },
    {
        "SKU": "COL-10-49309 ",
        "SHOP": "GWEUFR ",
        "FLDDEF": "Metafield_2 ",
        "PARB": "icon ",
        "STAT1": "U ",
        "FREIA1": "18980910989369 ",
        "FREIA2": " "
    },
    {
        "SKU": "COL-10-49310 ",
        "SHOP": "GWEUDE ",
        "FLDDEF": "Collection_Title ",
        "PARB": " ",
        "STAT1": "U ",
        "FREIA1": " ",
        "FREIA2": " "
    },
    {
        "SKU": "COL-10-49000 ",
        "SHOP": "GWEUEN ",
        "FLDDEF": "Metafield_7 ",
        "PARB": "subtitle ",
        "STAT1": " ",
        "FREIA1": "20117934211126 ",
        "FREIA2": " "
    },
    {
        "SKU": "COL-10-49310 ",
        "SHOP": "GWEUDE ",
        "FLDDEF": "Handle ",
        "PARB": " ",
        "STAT1": "U ",
        "FREIA1": "166926024764 ",
        "FREIA2": " "
    },
    {
        "SKU": "COL-10-49000 ",
        "SHOP": "GWEUEN ",
        "FLDDEF": "Body_HTML ",
        "PARB": " ",
        "STAT1": " ",
        "FREIA1": " ",
        "FREIA2": " "
    },
    {
        "SKU": "COL-10-49310 ",
        "SHOP": "GWEUFR ",
        "FLDDEF": "Body_HTML ",
        "PARB": " ",
        "STAT1": "U ",
        "FREIA1": " ",
        "FREIA2": " "
    },
    {
        "SKU": "COL-10-49309 ",
        "SHOP": "GWIN ",
        "FLDDEF": "Metafield_3 ",
        "PARB": "video ",
        "STAT1": "U ",
        "FREIA1": "19038265573420 ",
        "FREIA2": " "
    },
    {
        "SKU": "COL-10-49000 ",
        "SHOP": "GWEUEN ",
        "FLDDEF": "Metafield_5 ",
        "PARB": "icon_image_1 ",
        "STAT1": " ",
        "FREIA1": "20117934145590 ",
        "FREIA2": " "
    },
    {
        "SKU": "COL-10-49000 ",
        "SHOP": "GWEUDE ",
        "FLDDEF": "Metafield_12 ",
        "PARB": "icon_image_3 ",
        "STAT1": "U ",
        "FREIA1": "13660255846460 ",
        "FREIA2": " "
    },
    {
        "SKU": "COL-10-49000 ",
        "SHOP": "GWEUFR ",
        "FLDDEF": "Metafield_5 ",
        "PARB": "heading ",
        "STAT1": "U ",
        "FREIA1": "18980495228985 ",
        "FREIA2": " "
    },
    {
        "SKU": "COL-10-49309 ",
        "SHOP": "GWEUDE ",
        "FLDDEF": "Metafield_1 ",
        "PARB": "subtitle ",
        "STAT1": "U ",
        "FREIA1": "13672656044092 ",
        "FREIA2": " "
    },
    {
        "SKU": "COL-10-49000 ",
        "SHOP": "GWEUEN ",
        "FLDDEF": "Handle ",
        "PARB": " ",
        "STAT1": " ",
        "FREIA1": "57456984123 ",
        "FREIA2": " "
    },
    {
        "SKU": "COL-10-49309 ",
        "SHOP": "GWIN ",
        "FLDDEF": "Metafield_2 ",
        "PARB": "icon ",
        "STAT1": "U ",
        "FREIA1": "19038265540652 ",
        "FREIA2": " "
    },
    {
        "SKU": "COL-10-49309 ",
        "SHOP": "GWUK ",
        "FLDDEF": "Image_URL ",
        "PARB": " ",
        "STAT1": "U ",
        "FREIA1": " ",
        "FREIA2": " "
    },
    {
        "SKU": "COL-10-49000 ",
        "SHOP": "GWEUDE ",
        "FLDDEF": "Metafield_18 ",
        "PARB": "icon ",
        "STAT1": "U ",
        "FREIA1": "19587379658812 ",
        "FREIA2": " "
    },
    {
        "SKU": "COL-10-49000 ",
        "SHOP": "GWEUEN ",
        "FLDDEF": "Metafield_4 ",
        "PARB": "video ",
        "STAT1": " ",
        "FREIA1": "20117934112822 ",
        "FREIA2": " "
    },
    {
        "SKU": "COL-10-49309 ",
        "SHOP": "GWEUDE ",
        "FLDDEF": "Body_HTML ",
        "PARB": " ",
        "STAT1": "U ",
        "FREIA1": " ",
        "FREIA2": " "
    },
    {
        "SKU": "COL-10-49310 ",
        "SHOP": "GWUK ",
        "FLDDEF": "Handle ",
        "PARB": " ",
        "STAT1": "U ",
        "FREIA1": "261318574141 ",
        "FREIA2": " "
    }
]

My goal is to loop through the array and to see where SKU is equal to itself. The goal is to then push all arrays where the SKU is the same into a different multidimensional array.

So for example all the arrays where the SKU = COL-10-49000 should end up in 1 multidimensional array and all the arrays where the SKU = COL-10-49310 should end up in 1 multidimensional array.

This is what I currently have, but doesn't seem to be doing the trick

foreach($aArray as $a) {
  if($a[SKU] == $a[SKU]) {
        array_push($bArray, $a)
  }
}

I am hoping to then have an array which will look like this:

[0] => Array( 
  [0] => Array ([SKU] => COL-10-49310 [SHOP] => EU3 [FLDDEF] => Handle [PARB] => [STAT1] => U [FREIA1] => 166926024764 [FREIA2] =>) 
  [1] => Array ([SKU] => COL-10-49310 [SHOP] => EU3 [FLDDEF] => Collection_Title [PARB] => [STAT1] => U [FREIA1] => [FREIA2] =>) 
  [2] => Array ([SKU] => COL-10-49310 [SHOP] => INT1 [FLDDEF] => SEO_1 [PARB] => title_tag [STAT1] => U [FREIA1] => 19034707656748 [FREIA2] => )
) 
[1] => Array([0] => Array ([SKU] => COL-10-49309 [SHOP] => EU3 [FLDDEF] => Handle [PARB] => [STAT1] => U [FREIA1] => 166926024764 [FREIA2] =>) 
  [1] => Array ([SKU] => COL-10-49309 [SHOP] => EU3 [FLDDEF] => Collection_Title [PARB] => [STAT1] => U [FREIA1] => [FREIA2] =>) 
  [2] => Array ([SKU] => COL-10-49309 [SHOP] => INT1 [FLDDEF] => SEO_1 [PARB] => title_tag [STAT1] => U [FREIA1] => 19034707656748 [FREIA2] => ))

So each SKU, and all its coresponding arrays will be together in one multidimensional array, where I can then loop through it with a ($i = 0; $i < count($aArray); $i ){} style.

CodePudding user response:

It sounds like you want to split the data into separate arrays, with each array containing only the entries relating to a single SKU.

As you mentioned in the comments that the top-level array can be indexed associatively rather than numerically, this makes it quite trivial to allocate items to each sub-array by reading the SKU from the item.

e.g.

$output = array();

foreach ($input as $item)
{
    $sku = $item["SKU"];
    $output[$sku][] = $item;
}

Live demo: http://sandbox.onlinephpfunctions.com/code/770b4fcc096449ee6c0a9fd39174f5b6bcd4106a

CodePudding user response:

So according to your question I can provide you this solution:

  1. Decode json

    $data = json_decode(<your_json_string>)

  2. Then we can use array_reduce() function to get result array.

$result = array_reduce($data, function (array $accumulator, $item) {
    $accumulator[$item->SKU][] = $item;

    return $accumulator;
}, []);

So $result array will contain to dimensional array with each SKU value key

  • Related