madprabh's avatar

Adding key/value pair to every array item

Hey Folks,

So I have this data

[
  {
    "user_framework_step_id": 1,
    "user_framework_id": 1,
    "help_text": "Describe the event or situation that is taking place when they run into the problem."
  },
  {
    "user_framework_step_id": 2,
    "user_framework_id": 1,
    "help_text": "What problem or challenge do they run into?"
  },
  {
    "user_framework_step_id": 3,
    "user_framework_id": 1,
    "help_text": "What pain do they experience due to the problem?"
  },
  {
    "user_framework_step_id": 4,
    "user_framework_id": 1,
    "help_text": "Why does the problem happen in the first place?"
  },
  {
    "user_framework_step_id": 5,
    "user_framework_id": 1,
    "help_text": "What future state are they trying to achieve?"
  }
]

I want to add

user_id:1,
problem_id:2,

To every array item above to get something like below

[
  {
    "user_framework_step_id": 1,
    "user_framework_id": 1,
    "help_text": "Describe the event or situation that is taking place when they run into the problem.",
    "user_id":1,
    "problem_id":2
  },
  {
    "user_framework_step_id": 2,
    "user_framework_id": 1,
    "help_text": "What problem or challenge do they run into?",
	"user_id":1,
    "problem_id":2
  },
  {
    "user_framework_step_id": 3,
    "user_framework_id": 1,
    "help_text": "What pain do they experience due to the problem?",
	"user_id":1,
    "problem_id":2
  },
  {
    "user_framework_step_id": 4,
    "user_framework_id": 1,
    "help_text": "Why does the problem happen in the first place?",
	"user_id":1,
    "problem_id":2
  },
  {
    "user_framework_step_id": 5,
    "user_framework_id": 1,
    "help_text": "What future state are they trying to achieve?",
	"user_id":1,
    "problem_id":2
  }
]

Can someone let me know how I can achieve this?

0 likes
4 replies
MichalOravec's avatar
Level 75
$result = collect($data)->map(function ($item) {
    $item->user_id = 1;

    $item->problem_id = 2;

    return $item;
});

if you already have a collection then

$result = $data->map(function ($item) {
    $item->user_id = 1;

    $item->problem_id = 2;

    return $item;
});
tykus's avatar

What are the items in the array, PHP objects, or is it raw JSON?

jlrdw's avatar

Also if whole array applies to

user_id:1,
problem_id:2,

You could make it like a parent child relation:

user_id:1,
problem_id:2,

=============================================

[
  {
    "user_framework_step_id": 1,
    "user_framework_id": 1,
    "help_text": "Describe the event or situation that is taking place when they run into the problem."
  },
  {
    "user_framework_step_id": 2,
    "user_framework_id": 1,
    "help_text": "What problem or challenge do they run into?"
  },
  {
    "user_framework_step_id": 3,
    "user_framework_id": 1,
    "help_text": "What pain do they experience due to the problem?"
  },
  {
    "user_framework_step_id": 4,
    "user_framework_id": 1,
    "help_text": "Why does the problem happen in the first place?"
  },
  {
    "user_framework_step_id": 5,
    "user_framework_id": 1,
    "help_text": "What future state are they trying to achieve?"
  }
]

Rather that repeat it in every sub array, just a thought:

Please or to participate in this conversation.