Venkat Venkat - 3 months ago 6
JSON Question

best way of combining two differently formated arrays to create a new array

Hi I am trying to combine these two differently formatted arrays in a best way to create a new array but i am overwritten the values always with the last set.

Array 1: It's a dynamic array but format will be same

Array
(
[0] => stdClass Object
(
[label] => Name
[fieldtype] => text
[required] => 1
[description] => Sku Name
[sku_id] => skucid1
)

[1] => stdClass Object
(
[label] => Id
[fieldtype] => text
[required] =>
[description] => Sku Id
[sku_id] => skucid2
)

[2] => stdClass Object
(
[label] => Unit Price
[fieldtype] => decimal
[required] =>
[description] => Sku Unit Price
[sku_id] => skucid3
)

[3] => stdClass Object
(
[label] => Tax
[fieldtype] => decimal
[required] =>
[description] => Sku Tax
[sku_id] => skucid4
)

[4] => stdClass Object
(
[label] => Quantity
[fieldtype] => number
[required] => 1
[description] => Sku Quantity
[sku_id] => skucid5
)

[5] => stdClass Object
(
[label] => Total
[fieldtype] => decimal
[required] =>
[description] => Sku Total
[sku_id] => skucid6
)

)


Array 2: It's dynamic array but format will be same

Array
(
[0] => Array
(
[0] => J1
[1] => SKUJ1
[2] => 1000
[3] => 100
[4] => 10
[5] => 11000
)

[1] => Array
(
[0] => J2
[1] => SKUJ2
[2] => 2000
[3] => 100
[4] => 10
[5] => 21000
)

)


The out put array which i am trying was creating the same number array of objects of second array with extra key in each object with name "value" and value for that key is corresponding key value pair in the second array.

Example JSON Output of above two arrays:

[
[
{
"label": "Name",
"fieldtype": "text",
"required": true,
"description": "Sku Name",
"sku_id": "skucid1",
"value": "J1"
},
{
"label": "Id",
"fieldtype": "text",
"required": false,
"description": "Sku Id",
"sku_id": "skucid2",
"value": "SKUJ1"
},
{
"label": "Unit Price",
"fieldtype": "decimal",
"required": false,
"description": "Sku Unit Price",
"sku_id": "skucid3",
"value": "1000"
},
{
"label": "Tax",
"fieldtype": "decimal",
"required": false,
"description": "Sku Tax",
"sku_id": "skucid4",
"value": "100"
},
{
"label": "Quantity",
"fieldtype": "number",
"required": true,
"description": "Sku Quantity",
"sku_id": "skucid5",
"value": "10"
},
{
"label": "Total",
"fieldtype": "decimal",
"required": false,
"description": "Sku Total",
"sku_id": "skucid6",
"value": "11000"
}
],
[
{
"label": "Name",
"fieldtype": "text",
"required": true,
"description": "Sku Name",
"sku_id": "skucid1",
"value": "J2"
},
{
"label": "Id",
"fieldtype": "text",
"required": false,
"description": "Sku Id",
"sku_id": "skucid2",
"value": "SKUJ2"
},
{
"label": "Unit Price",
"fieldtype": "decimal",
"required": false,
"description": "Sku Unit Price",
"sku_id": "skucid3",
"value": "2000"
},
{
"label": "Tax",
"fieldtype": "decimal",
"required": false,
"description": "Sku Tax",
"sku_id": "skucid4",
"value": "100"
},
{
"label": "Quantity",
"fieldtype": "number",
"required": true,
"description": "Sku Quantity",
"sku_id": "skucid5",
"value": "10"
},
{
"label": "Total",
"fieldtype": "decimal",
"required": false,
"description": "Sku Total",
"sku_id": "skucid6",
"value": "21000"
}
]
]


Output i am getting at present:

[
[
{
"label": "Name",
"fieldtype": "text",
"required": true,
"description": "Sku Name",
"sku_id": "skucid1",
"value": "J2"
},
{
"label": "Id",
"fieldtype": "text",
"required": false,
"description": "Sku Id",
"sku_id": "skucid2",
"value": "SKUJ2"
},
{
"label": "Unit Price",
"fieldtype": "decimal",
"required": false,
"description": "Sku Unit Price",
"sku_id": "skucid3",
"value": "2000"
},
{
"label": "Tax",
"fieldtype": "decimal",
"required": false,
"description": "Sku Tax",
"sku_id": "skucid4",
"value": "100"
},
{
"label": "Quantity",
"fieldtype": "number",
"required": true,
"description": "Sku Quantity",
"sku_id": "skucid5",
"value": "10"
},
{
"label": "Total",
"fieldtype": "decimal",
"required": false,
"description": "Sku Total",
"sku_id": "skucid6",
"value": "21000"
}
],
[
{
"label": "Name",
"fieldtype": "text",
"required": true,
"description": "Sku Name",
"sku_id": "skucid1",
"value": "J2"
},
{
"label": "Id",
"fieldtype": "text",
"required": false,
"description": "Sku Id",
"sku_id": "skucid2",
"value": "SKUJ2"
},
{
"label": "Unit Price",
"fieldtype": "decimal",
"required": false,
"description": "Sku Unit Price",
"sku_id": "skucid3",
"value": "2000"
},
{
"label": "Tax",
"fieldtype": "decimal",
"required": false,
"description": "Sku Tax",
"sku_id": "skucid4",
"value": "100"
},
{
"label": "Quantity",
"fieldtype": "number",
"required": true,
"description": "Sku Quantity",
"sku_id": "skucid5",
"value": "10"
},
{
"label": "Total",
"fieldtype": "decimal",
"required": false,
"description": "Sku Total",
"sku_id": "skucid6",
"value": "21000"
}
]
]

Answer

Take a 3rd variable to hold o/p. Loop over second array and iterate each 2nd dimension items. Trace key/index of second level value (0,1,2) in first array and assign to new array. For this, I suggest you below code:-

$array1 = "Your Input Array";
$array2 = "Your Input Array";
$array3 = array()

foreach($array2 as $arr2Key => $arr2Value):
  $array3[$arr2Key] =  array();

  foreach($arr2Value as $arr2Innerkey => $arr2InnerValue):

    if (array_key_exists($arr2Innerkey, $array1)):
      $array1Obj = array();
      $array1Obj = (array) $array1[$arr2Innerkey];
      $array1Obj['value'] = $arr2InnerValue;
      $array3[$arr2Key][] = $arr2InnerValue; 
    endif;

  endforeach;

endforeach;


echo json_encode($array3);
Comments