0

I have 30 JSON objects inside an array. I would like to loop through each object and create a new Array of only 3 objects containing data of 10 objects each.

My issue here is I am not sure how to create new objects while in a foreach loop

Example:

keys on the left and value on the right

1 1
2 2
3 3
. .
. .
. .
1 11
1 12
2 13
. .
. .
. .
1 21
2 22
3 23
. .
. .

Here, sid tells what all objects needs to be grouped into a single object, as per this example group 10 objects into 1.

so currently I have :

[
{
   "key1" : "value1",
   "key2" : "value2",
   "sid"  : "1"

},
{
   "key1" : "value3",
   "key2" : "value4",
   "sid"  : "1"
},
.
. // Total 10 objects with sid = 1
.
,
{
   "key1" : "value5",
   "key2" : "value6",
   "sid"  : "2"
},
{
   "key1" : "value7",
   "key2" : "value8",
   "sid"  : "2"
},
.
. // Total 10 objects with sid = 2
.
,
{
   "key1" : "value9",
   "key2" : "value10",
   "sid"  : "3"
},
{
   "key1" : "value11",
   "key2" : "value12",
   "sid"  : "3"
},
.
. // Total 10 objects with sid = 3
]

I would like to replace it as:

[
{
  "value2" : "value1",
  "value4" : "value3",    //all data in this object belongs to sid = 1
  "valueN" : "valueM"     //Other respective 8 objects
},
{
  "value6" : "value5",
  "value8" : "value7",    //all data in this object belongs to sid = 2
  "valueN" : "valueM"     //Other respective 8 objects
},
{
  "value10" : "value9",
  "value12" : "value11",  //all data in this object belongs to sid = 3
  "valueN" : "valueM"     //Other respective 8 objects
}
]

code

I am directly loop through each and every object inside an array and adding them into new array called finalResult by setting key as value of the key2 and value as key1 value

for ($i = 0; $i < $count; $i++)
{

  $finalResult[$arrObj[$i]['key2']] = $arrObj[$i]['key1'];
}

This only creates a single object with all data in it.

I would like to do some check if sid == 11,12,13,... and so on then create new object and start adding all the data inside newly created object until next sid is encountered.

Update

This is what I got:

{
    "1": {
        "value2" : "value1",
        "value4" : "value3",    
        "valueN" : "valueM"     //Other respective 8 objects
    },
    "2": {
        "value6" : "value5",
        "value8" : "value7",    
        "valueN" : "valueM"     //Other respective 8 objects
    },
    "3": {
        "value10" : "value9",
        "value12" : "value11",    
        "valueN" : "valueM"     //Other respective 8 objects
    }
}

I was expecting :

[
        {
            "value2" : "value1",
            "value4" : "value3",    
            "valueN" : "valueM"     //Other respective 8 objects
        },
        {
            "value6" : "value5",
            "value8" : "value7",    
            "valueN" : "valueM"     //Other respective 8 objects
        },
        {
            "value10" : "value9",
            "value12" : "value11",    
            "valueN" : "valueM"     //Other respective 8 objects
        }
]

2 Answers 2

1

If I am understanding you correctly, you need to do something like this:

for ($i = 0; $i < $count; $i++)
{
    $sid = $arrObj[$i]['sid'];
    if(!isset($finalResult[$sid])) $finalResult[$sid] = array();
    $finalResult[$sid][$arrObj[$i]['key2']] = $arrObj[$i]['key1'];
}

var_dump(json_encode(array_values($finalResult)));

Edit: Full example.

$json = '[
{
   "key1" : "value1",
   "key2" : "value2",
   "sid"  : "1"

},
{
   "key1" : "value3",
   "key2" : "value4",
   "sid"  : "1"
},
{
   "key1" : "value5",
   "key2" : "value6",
   "sid"  : "2"
},
{
   "key1" : "value7",
   "key2" : "value8",
   "sid"  : "2"
}]';

$arrObj = json_decode($json, true);

$count = count($arrObj);

$finalResult = [];

for ($i = 0; $i < $count; $i++)
{
    $sid = $arrObj[$i]['sid'];
    if(!isset($finalResult[$sid])) $finalResult[$sid] = array();
    $finalResult[$sid][$arrObj[$i]['key2']] = $arrObj[$i]['key1'];
}

var_dump(json_encode(array_values($finalResult)));

Output:

[{"value2":"value1","value4":"value3"},{"value6":"value5","value8":"value7"}]

Sign up to request clarification or add additional context in comments.

3 Comments

please check update section in my question. after doing what you suggested, that's what I got. It's weird even after defining $finalResult = []; I get {}. and also I didnt wanted to sid to be before the object
that does not work. for some reason this [] does not change {} to [] and also sid is infront of the object
Did you use $finalArray not $finalResult?
0

You can change to the structure you want by using sid and key2 as two levels of keys in your result array:

foreach ($data as $item) {
    $result[$item->sid][$item->key2] = $item->key1;
}
$result = array_values($result);

Things to note:

  1. If there is more than one of the same key2 value for the same sid, the corresponding key1 value will be overwritten in $result by the subsequent value.

  2. array_values($result) is necessary to encode your results as an array of objects when you json_encode it. Without that, it will be an object with a property for each sid.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.