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"
}] 
2
  • 1
    Are you sure this is PHP? Never saw {} for declaring "objects" Commented Aug 8, 2018 at 19:06
  • it is javascript Commented Aug 8, 2018 at 19:08

3 Answers 3

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;
    }
    ...
}
Sign up to request clarification or add additional context in comments.

Comments

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

3 Comments

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 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.
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.
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.

6 Comments

This does not answer the question.
How come? It will have two arrays with each object and their respective keys, no?
No. There is two items with Radiology. And you can be sure the Json will always look like that
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.
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
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.