1

I have this variable $office which will output when print_r:

{
  "total": 3,
  "results": [
    {
      "id": "4",
      "blog_id": "11",
      "office_name": "Japan",
      "office_status": "inactive"
    },
    {
      "id": "5",
      "blog_id": "11",
      "office_name": "USA",
      "office_status": "active"
    },
    {
      "id": "6",
      "blog_id": "11",
      "office_name": "London",
      "office_status": "inactive"
    },
    }

}

Now I want to create a new variable where the one with office_status of active will only show up, my desired output is:

{
      "total": 1,
      "results": [
        {
          "id": "5",
          "blog_id": "11",
          "office_name": "",
          "office_status": "active"
        },
        }

    }

This is what I tried so far but it still returns all of the array:

$v= "active";
$k = "office_status";
foreach($offices as $k => $v) {
    $offices_active[$k] = array_filter($v);
}
print_r($offices_active);

Please help. Thank you

2 Answers 2

1

Actually it can be done in many different ways, One way to do it this way.

<?php
$object = '{"total":3,"results":[{"id":"4","blog_id":"11","office_name":"Japan","office_status":"inactive"},{"id":"5","blog_id":"11","office_name":"USA","office_status":"active"},{"id":"6","blog_id":"11","office_name":"London","office_status":"inactive"}]}';
$offices = json_decode($object,true);
$new = array_filter($offices['results'], function ($var) {
    return ($var['office_status'] == 'active');
});

$expected['total'] = count($new);
$expected['results'] = $new;
echo json_encode($expected);
?>

WORKING DEMO: https://3v4l.org/NXZvQ

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

2 Comments

Your code is working on your link, but its not working on my side, I think it is because of your $object variable is different on mine, heres my code prntscr.com/rs1v39. As you can see in the image, I made it as $object = $offices; because $offices is the one holding the arrays, its a wordpress api. My error is json_decode() expects parameter 1 and invalid supply of for each Can you please check why? Thank you
@ICGDEVS did you check my lastest updated code and working demo link? if not please have a look again.
0

This code is help to get data by the status value

<?php

$src_ar = '{"total":3,"results":[{"id":"4","blog_id":"11","office_name":"Japan","office_status":"inactive"},{"id":"5","blog_id":"11","office_name":"USA","office_status":"active"},{"id":"6","blog_id":"11","office_name":"London","office_status":"inactive"}]}';
$office_info = json_decode($src_ar,true);

$inactive_array = sort_by_office_status('inactive', $office_info['results']);

print_r($inactive_array);

$active_array = sort_by_office_status('active', $office_info['results']);

print_r($active_array);


function sort_by_office_status( $office_status, $result_array ) {
    $return_ar =array();

    if( !empty( $result_array ) ) {
        foreach( $result_array as $res ) {
            if( $res['office_status'] == $office_status ) {
                $return_ar['results'][] = $res;
            }
        }
    } else {
        $return_ar['results'] = array();
    }

    $result_count = count( $return_ar['results'] );

    $return_ar['total'] = $result_count;

    return $return_ar;
}
?>

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.