用键添加数组中的项目?

I have some PHP code and I was tried many times but no luck. this is my case i have one array that declare in difference place and want to merge in looping

$data[] = ['sales' => 'mark'];

for($i=0;$i<3;$i++){
 data[$i]="some value".$i;
}

the result

Array ( [0] => Array ( [sales] => mark ) [0] => some value0 [1] => some value1 [2] => some value2 )

my expected

Array ( [0] => Array ( [sales] => mark [0] => some value0 [1] => some value1 [2] => some value2 ) )

I think you are looking for:

$data['sales'] = 'mark';

for($i=0;$i<3;$i++){
 $data[$i]="some value".$i;
}
$x[0] = $data;
print_r($x);

print_r($data);

That outputs this:

Array
(
    [0] => Array
        (
            [sales] => mark
            [0] => some value0
            [1] => some value1
            [2] => some value2
        )
)

A mistake was made in the code at the data array in function should have been preceded by a $, like $data[$i] : try: 'mark'];

for($i=0;$i<5;$i++) {
    $data[$i]="some value".$i;
}
var_dump($data);

If I understand your question correctly, you're just missing some brackets:

$data[] = ['sales' => 'mark'];

for($i=0;$i<5;$i++){
    $data[$i][] = "some value".$i;
}

But maybe you should try to explain what it is you're trying to achieve. Know the $data is run through 5 time, but you only have one thing in it. Maybe you should use a foreach, like this:

$datas[] = ['sales' => 'mark']; $i = 0;
foreach($datas as $data){
    $data[] = 'some value'.$i;
    $i++;
}

Or if you have to have 5 iterations:

for($i=0; $i<5; $i++){
    foreach($datas as $data){
        $data[] = 'some value'.$i;
    }
}

The above will have 5 iterations on every $datas, if you only want it on the first one, you could hardcode it:

$data[] = ['sales' => 'mark'];

for($i=0;$i<5;$i++){
    $data[0][] = "some value".$i;
}

Method 1

$data[] = ['sales' => 'mark'];

for($i=0;$i<3;$i++){
  $data[0][$i]="some value".$i;
}

print_r( $data);

Output

Array
(
    [0] => Array
        (
            [sales] => mark
            [0] => some value0
            [1] => some value1
            [2] => some value2
        )

)

Method 2

$data1[] = ['sales' => 'mark'];

for($i=0;$i<3;$i++){
  $data2[$i]="some value".$i;
}

$data = array_merge($data1[0],$data2);
print_r($data);

Or

$data = ['sales' => 'mark'];

for($i=0;$i<3;$i++){
  $data[$i]="some value".$i;
}

print_r( $data);

Output

Array
(
    [sales] => mark
    [0] => some value0
    [1] => some value1
    [2] => some value2
)

Refer : https://eval.in/587485