0

I am trying to create a array from one entry inside another array. So the prosess is that i get an array from frontend that looks like this:

 [{
    "account_id": "123456789",
    "month_id": 201808,
    "month_budget": 11
 }, {
    "account_id": "111222",
    "month_id": 201809,
    "month_adops_forecast": 11
 }]

now in my backend i must retrive either "month_budget" or "month_adops_forecast", i have created an if statement in backend like this:

    if (isset($data['month_budget'])) {
        $metric = "month_budget =". $data['month_budget'];
    } else if (isset($data['month_adops_forecast'])){
        $metric = "month_adops_forecast =". $data['month_adops_forecast'];
    }else {
        return false;
    }

As you can see i create it as an string, but i would want this "metric" in an own array i create. I tried to retrive both key and value, but this only gave me the value because it is inside an if (isset($data['month_budget'])) { so by saying $data['month_budget'] i cannot retrive the key

What would be the best way to retrive this data?

Wanted result:
$data = same array as it is
$metric = month_budget or month_adops_forecast

$metric = array('month_adops_forecast' => 11);

OR

$metric = array('month_budget' => 11);
6
  • 2
    This looks like a json string. You need to decode this either into an array or an object, and then you can access the field you want. Thus, being able to derive anything from it. And another thing, month_budget key only exists in one of the two objects you provided. Creating an array from it would result in a single element. Commented Aug 22, 2018 at 14:17
  • it could be useful json_decode php.net/manual/en/function.json-decode.php Commented Aug 22, 2018 at 14:17
  • Show what you want the result to look like. Commented Aug 22, 2018 at 14:19
  • @PatrickQ, yes offcourse. i edited question with wanted result. Commented Aug 22, 2018 at 14:23
  • Using a foreach loop will give you the key. foreach($array as $key => $value){ echo $key}; Commented Aug 22, 2018 at 14:23

2 Answers 2

1

You can use this:

if (isset($data['month_budget'])) {
    $metric['month_budget'] = $data['month_budget'];
} else if (isset($data['month_adops_forecast'])){
    $metric['month_adops_forecast'] = $data['month_adops_forecast'];
} else {
    return false;
}

This $metric = "month_budget =". $data['month_budget'] notation cannot be used to create arrays, you have to either set the key using $array['key'] or $array = array('key' => 'value') among other ways, of course, being this ones around the most common.

I'm assuming you were trying the last one.

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

2 Comments

Thank you @Rafael, works perfect. I was thinking wrong way. Thank you!
No problem. I assumed you haven't decoded the json. Next time add this information to the question also, please. (It's quite common for people to not decode the json string and have problems because of that)
0

If I got your answer right, to get the result you expect you can use this:

if (isset($data['month_budget'])) {
    $metric['month_budget'] = $data['month_budget']);
} else if (isset($data['month_adops_forecast'])){
    $metric['month_adops_forecast'] = $data['month_adops_forecast']);
}else {
    return false;
}

2 Comments

Identical post as the answer?
You are right! Sorry.. for some reason I read the answer wrong.

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.