0

I currently have an array in this format:

[
    {
        "name": "Size",
        "values": [
            "Small"
        ]
     },
     {
        "name": "Colour",
        "values": [
            "Red"
        ]
     },
     {
        "name": "Size",
        "values": [
            "Large"
        ]
     },
     {
         "name": "Colour",
         "values": [
              "Green"
          ]
      } 
]

I wish to loop through the array and as a result have its structure looking like this:

[
    {
        "name": "Size",
        "values": [
            "Small", "Large"
         ]
    },
    {
         "name": "Colour",
         "values": [
             "Red", "Green"
          ] 
     }

I wish for it to merge the "values" if the "names" match.

3
  • do you wants to check the values of both arrays and if they match and then to b e matches Commented Jul 18, 2019 at 15:58
  • @NipunTharuksha - For example, if there are two objects with the same "name" the "values" should be merged and the name kept. Commented Jul 18, 2019 at 16:06
  • did you tried with foreach and if inside it Commented Jul 18, 2019 at 16:21

4 Answers 4

1

I've done some changes in your data to get the output and it might solve your problem.

<?php
$val = json_decode('[
{
    "name": "Size",
    "values": [
        "Small"
    ]
},
{
    "name": "Colour",
    "values": [
        "Red"
    ]
},
{
    "name": "Size",
    "values": [
        "Large"
    ]
},
{
    "name": "Colour",
    "values": [
        "Green"
    ]
}
]');
$output =[];
foreach($val as $v) {
    if(!isset($output[$v->name])) {
        $output[$v->name]["name"] = $v->name;
        $output[$v->name]["values"] = $v->values;
    } else {
        $output[$v->name]["values"] = array_merge($v->values,$output[$v->name]["values"]);
    }
}
$output = array_values($output);
print_r(json_encode($output));
?>
Sign up to request clarification or add additional context in comments.

Comments

0

Simply array_reduce() might be a solution for your purpose.

$result = array_values(array_reduce($data, function ($old, $new) {
    $old[$new->name] = $new;
    return $old;
}, []));

Demo

Comments

0

This my code, I first created this array in php and then used some foreach to solve the problem, this is the result

<?php
    //creating source vector
    $source=array();
    array_push($source,["name"=>"Size","values"=> ["Small"]]);
    array_push($source,["name"=>"Colour","values"=> ["Red"]]);
    array_push($source,["name"=>"Size","values"=> ["Large"]]);
    array_push($source,["name"=>"Colour","values"=> ["Green"]]);
    print_r($source);
    //initializind the result vector
    $result=array();
    array_push($result,["name"=>"Size", "values" => []]);
    array_push($result,["name"=>"Colour", "values" => []]);
    $i=0;

    //starting sorting
    foreach($source as $element){
        if($element['name']==="Size"){
            foreach($element["values"] as $subelement){
                array_push($result[0]["values"],$source[$i]["values"][0]);
            }
        }
        else{
            foreach($element["values"] as $subelement){
                array_push($result[1]["values"],$source[$i]["values"][0]);
            }
        }
        $i++;
    }


    //output
    print_r($source);
    print("<br><br>");
    print_r($result);
?>

And this the final output

    Array ( 
          [0] => Array ( 
                      [name] => Size 
                      [values] => Array ( 
                                [0] => Small
                                [1] => Large ) ) 
          [1] => Array ( 
                      [name] => Colour 
                      [values] => Array (
                                [0] => Red 
                                [1] => Green ) )  
)

Hope it helps!

Comments

0

Try this:

$newArray = [];
foreach($sourceArray as $element) {
    $newArray[$element->name]['name'] = $element->name;
    $newArray[$element->name]['values'][] = implode($element->values);
}
$newArray = array_values($newArray);

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.