0

one array of object =

[{
    "id": "5b2a4b0287bc2c082ebc2897",
    "test_name": "BSF",
    "test_type": "Pathology",
    "cost": "100"
}, {
    "id": "5b2a4ac63db998081e7f6c97",
    "test_name": "Brain & PNS",
    "test_type": "Radiology",
    "cost": "1000"
}, {
    "id": "5b2a4ac63db998081e22336c97",
    "test_name": "BPNS",
    "test_type": "Radiology",
    "cost": "1000"
}] 

I want to split this array of object into two separate array of object based on key value pair - "test_type"

O/p 1 st array

[{
    "id": "5b2a4b0287bc2c082ebc2897",
    "test_name": "BSF",
    "test_type": "Pathology",
    "cost": "100"
}]

2 nd array

[ {
    "id": "5b2a4ac63db998081e7f6c97",
    "test_name": "Brain & PNS",
    "test_type": "Radiology",
    "cost": "1000"
},{
    "id": "5b2a4ac63db998081e22336c97",
    "test_name": "BPNS",
    "test_type": "Radiology",
    "cost": "1000"
}] 

3 Answers3

4

You can create temporary arrays:

$arr = json_decode($str, true);

foreach($arr as $a){
    if($a['test_type'] == 'Radiology'){
        $radiology_array[] = $a;
    }
    if($a['test_type'] == 'Pathology'){
        $pathology_array[] = $a;
    }
    ...
}
Kisaragi
  • 2,198
  • 3
  • 16
  • 28
0

I believe this is JSON format.

Just use the old json_decode($variable) and assign each key to another variable.

$result = json_decode($variable);

$type = [];
foreach ($result as $item) {
    $type[$item->test_type][] = $item;     
}

This way, each test_type will have it's own key. Which could be used as one array for each test_type.

Rafael
  • 1,495
  • 1
  • 14
  • 25
  • This does not answer the question. – Andreas Aug 08 '18 at 19:15
  • How come? It will have two arrays with each object and their respective keys, no? – Rafael Aug 08 '18 at 19:25
  • No. There is two items with Radiology. And you can be sure the Json will always look like that – Andreas Aug 08 '18 at 19:26
  • The question was specific about one array with two objects to be divided in two arrays. I created a specific answer for that. This answer the question that was created initially. – Rafael Aug 08 '18 at 19:27
  • No the question has three items and reading questions litteral is not a good idea. Code and especially json/arrays are not the same all the time. If that was the case we could all stick to html – Andreas Aug 08 '18 at 19:31
  • Ok, but that is another discussion entirely. The question was created in one way and I answer that. If that is a good or not a good idea is a different reasoning than "this does not answer the question". But I do see the problem now and working on the fix. – Rafael Aug 08 '18 at 19:33
0

You can use array_column to make a flat array of the type which you the use as the matching array.
When you loop the matching array ($type) you can use array_intersect to get the subarrays as you wanted.

I place them in an associative array that can be extracted to new variables.

$arr = json_decode($json,true);
$type = array_column($arr, "test_type");

Foreach(array_unique($type) as $t){
   $new[$t] = array_intersect_key($arr, array_intersect($type, [$t]));
}
extract($new);
var_dump($Radiology, $Pathology);

https://3v4l.org/nj936

This method will only loop the unique count of types and will work even if you add a new type to the list like: https://3v4l.org/Jf0SE

Andreas
  • 23,610
  • 6
  • 30
  • 62
  • 1
    This is a clever approach, but ruined by dumping arbitrary variables into the local scope using `extract()` at the end. Just use the array. – Sammitch Aug 08 '18 at 19:36
  • @Sammitch OP wants separate arrays, but I agree that it's better to use the $new array. There are other ways to "extract" the values but I uses the "quickest" method just to answer the question. – Andreas Aug 08 '18 at 19:45
  • I never use `extract()` in any real projects, but this answer is unjustly DV'ed. The use of `extract` is entirely appropriate for this case. – mickmackusa Dec 14 '20 at 23:02