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

Jovs
  • 852
  • 7
  • 23
  • 1
    It's a little confusing, can you add expected output and input. – user269867 Jul 02 '19 at 01:43
  • I already input a sample of array that I want to be an output. thanks – Jovs Jul 02 '19 at 02:02
  • You might try [`array_merge`](https://www.php.net/manual/en/function.array-merge.php). "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." – showdev Jul 02 '19 at 02:10
  • `array_merge` just put another array inside of array. my example is array[0] then it will create array[1] with the input of other `$array` – Jovs Jul 02 '19 at 02:14
  • 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](http://phpfiddle.org/lite/code/px7v-iybw). – showdev Jul 02 '19 at 02:22
  • Thank you. Does it possible to put the `$array` in the middle of it? – Jovs Jul 02 '19 at 02:25
  • 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](https://stackoverflow.com/a/9847709/924299). – showdev Jul 02 '19 at 02:29

0 Answers0