0

I have a problem where I want to put a random array with random count of how many it is inside an array. I want to do it like the following.

// Array don't have exact count of how many data is there
$array = array(1,2,3,4,.....)   

$data['data'][] = array(
   $id,
   $date_file,
   $emp_id,
   $name,
   $from_to,
   $reason,
   $status,
);

I want to put the $array inside of $data. But it's not supposed to be a nested the array I want it to be on the same field as id, date_file, emp_id, etc.

The output I want is like this

    array:1 [▼
  0 => {#401 ▼
    +"id": 1
    +"date_file": "2019-07-03 00:00:00"
    +"emp_id": 1
    +"name": aysan
    +"from_to": "2019-07-03 00:00:00"
    +"1": 1 //where I want to put the other array start here
    +"2": 2 //The number of array is random or different from the others I want to put
    +"reason": none
    +"Status": "For Approval"
  }
]

The $array need to be foreach but I don't think I can do that inside of array

7
  • 1
    It's a little confusing, can you add expected output and input. Commented Jul 2, 2019 at 1:43
  • I already input a sample of array that I want to be an output. thanks Commented Jul 2, 2019 at 2:02
  • You might try array_merge. "Merges the elements of one or more arrays together... If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended." Commented Jul 2, 2019 at 2:10
  • 1
    It doesn't put an array inside an array (although that's exactly what the question title says). It merges the arrays. Here's an example. Commented Jul 2, 2019 at 2:22
  • 1
    No, its appended to the end. But you can probably merge one array into the middle of another array by also using array_slice, something like this. Commented Jul 2, 2019 at 2:29

0

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.