2

I have a json data. Now I want to reform it. In my json data there is a property like person_on_zone I want to make a property named person_info and store them which person under this area.

Here is my data any code. Thanks in advance

My json data

$val = [
    {
     'city':'xx',
     'zone':'yy',
     'person_on_zone':'p1'
    },
    {
     'city':'xx',
     'zone':'yy',
     'person_on_zone':'p2'
    },
    {
     'city':'xx',
     'zone':'yy',
     'person_on_zone':'p3'
    },
    {
     'city':'xx',
     'zone':'ww',
     'person_on_zone':'p1'
    },
  ]

My expectation is

[
 {
  'city':'xx',
  'zone':'yy',
  'person_info':{
                  'person_on_zone':'p1',
                  'person_on_zone':'p2',
                  'person_on_zone':'p3',
                }
 },
 {
  'city':'xx',
  'zone':'ww',
  'person_info':{
                  'person_on_zone':'p1'
                }
 },
] 

Here I tried

foreach ($val as $v) {
    $new_array['city'] = $v['city'];
    $new_array['zone'] = $v['zone'];
    foreach ($val as $v2) {
        $new_array['person_info'] = $v['person_on_zone'];
    }
}
json_encode($new_array);
4
  • I think you have to change in inner loop like this foreach ($val as $v2){ $new_array['person_info'] = $v2['person_on_zone']; } Commented May 29, 2019 at 4:09
  • Thanks for your feedback, can you please correct this code ? Commented May 29, 2019 at 4:12
  • is $val json string Commented May 29, 2019 at 4:15
  • You cannot have one key multiple times. Commented May 29, 2019 at 6:25

5 Answers 5

1

Try this, use $map to store key city-zone, then transform to array to get the result

<?php

$val = [
    [
        'city' => 'xx',
        'zone' => 'yy',
        'person_on_zone' => 'p1'
    ],
    [
        'city' => 'xx',
        'zone' => 'yy',
        'person_on_zone' => 'p2'
    ],
    [
        'city' => 'xx',
        'zone' => 'yy',
        'person_on_zone' => 'p3'
    ],
    [
        'city' => 'xx',
        'zone' => 'ww',
        'person_on_zone' => 'p1'
    ]
];

$map = [];
foreach ($val as $v) {
    $key = $v['city'] . $v['zone'];
    if (!isset($map[$key])) {
        $map[$key] = [
            'city' => $v['city'],
            'zone' => $v['zone'],
            'person_info' => []
        ];
    }

    $map[$key]['person_info'][] = $v['person_on_zone'];
}

print_r(array_values($map));
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot. Its working but it shows '0':'p1' . I need 'person_on_zone':'p1'. Please
can not use duplicate key person_on_zone in person_info @mrmithun
0

Correct Code

  foreach ($val as $v){
                    $new_array['city'] = $v['city'];
                    $new_array['zone'] = $v['zone'];
                    foreach ($val as $v2){
                        $new_array['person_info'] = $v2['person_on_zone'];
                    }
                }
                json_encode($new_array);

Comments

0

try this:

first decode json array then use foreach loop with key

$val = json_decode($val);
foreach ($val as $v){
         $new_array['city'] = $v->city;
         $new_array['zone'] = $v->zone;
         foreach ($val as $key=>$v2){
         $new_array[$key]['person_info'] = $v->person_on_zone;
                    }
                }
 print_r($new_array);

Comments

0

I think you make a mistake around composing the json. I tried with your code and i found following is the correct way to do.. Remember single quote(') is not valid in json string that is being used to define key value pair in php

// I think your code has json string and i convert it into array of objects(stdClass) 
// and if your code has array then keep you code intact but need to change the notation of 
// objects to associative array.

// first thing first decode json string
$values =  json_decode('[
{ "city":"xx",
  "zone":"yy",
  "person_on_zone":"p1"
},
{
 "city":"xx",
 "zone":"yy",
 "person_on_zone":"p2"
},
{
 "city":"xx",
 "zone":"yy",
 "person_on_zone":"p3"
},
{
 "city":"xx",
 "zone":"ww",
 "person_on_zone":"p1"
}]');

// Now declare new values as array
$new_values = [];
// walk through each object
foreach ($values as $v){
    // $v becomes an object of stdClass here
    // $data is a temporary array
    $data['city'] = $v->city;
    $data['zone'] = $v->zone;
    $data['person_info'] = [];
    foreach ($values as $v2){
        if(!in_array($data['person_info'],['person_on_zone' => $v2->person_on_zone]))
        {
            // compare if zones are same or not
            if($v->zone == $v2->zone)
            {
                // push to the array instead of assiging
                array_push($data['person_info'], ['person_on_zone' => $v2->person_on_zone]);
            }
        }

    }
    // make array unique 
    $data['person_info'] = array_map("unserialize", array_unique(array_map("serialize", $data['person_info'])));
    array_push($new_values, $data);
}
// make array unique in mutidimensional array
$new_values = array_map("unserialize", array_unique(array_map("serialize", $new_values)));
echo '<pre>';
print_r($new_values);
echo '</pre>';

Comments

0

One simple foreach would do,

$result = [];
foreach ($arr as $key => $value) {
    //created unique combination of city and zone as key
    $result[$value['city'] . $value['zone']]['city']          = $value['city'];
    $result[$value['city'] . $value['zone']]['zone']          = $value['zone'];
    $result[$value['city'] . $value['zone']]['person_info'][] = ['person_on_zone' => $value['person_on_zone']];
}
echo json_encode(array_values($result));die;

array_values — Return all the values of an array

Working demo.

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.