Array push with multidimentional array

I have this multidimentional array:

array(1) {
  [0]=>
  array(1) {
    ["service"]=>
    string(3) "top"
  }
}

and I want to push to the inner array the string below

string(3) "443"

the string above I got it from an array using array_shift

$id = array_shift($_POST['updateIDs']);

so it can become like this:

array(1) {
  [0]=>
  array(1) {
    ["service"]=>
    string(3) "top"
    ['id']=>'443'
  }
}

I think it must be done with array_push and foreach…i tried but I could not.

array_push() does not create string indices …

$array[0]['id'] = 443;

ok…we must solve the first problem…adding 443 to the inner array…

See post #2.

unless there are constraints I don’t know about.

oh…you mean this is the solution:

$array[0]['id'] = 443;

Indeed.

1 Like

it does not work…may be I should some more retails:

the multi-dimensional array is inside this variable:

$output['form']

and ‘443’ is inside in this variable

$id

As such I tried this:

$output[0]['form']=$id;

The multi-dimensional array remained the same however…the string was not added to it.

It is my fault, I should have told you from the beginning

I tried also this:

$output['form']['id']=$id;

but it did not bring the result I wanted.

Surely it might be

$output['form'][0]['id'] = $id;

yes…that did it…thx.

Ι must add sth important…I made this question simple cause I thought it would suffice the solve the bigger problem…which is many inner arrays and many IDs to to attach respectively to each one of them.

The multi-dimensional array is held in this variable:

$output['form'])

and the IDs are held here:

$_POST['updateIDs']

I tried using a foreach loop within foreach loop and array_push but I could not do it.
Furthermore…I think that first I must attach the IDs in the inner arrays and then change the name of the corresponding key.

What do you think?

Depending on the array structure array_merge_recursive() is an option.

yes it could be an option…but the fact that the first array is 2-dimensional complicates things.
I have tried several things(loops etc) but I cannot make it work.
In many cases what happens is that the IDs of the second array attach themselves to the first inner array of the 2 dimensional array.

So code like this one(for example):

  for ($i = 0; $i < count($output['form'][0]); $i++) {
    
            $result =  array_merge_recursive($output['form'][$i], $_POST['updateIDs']);
       }

gives this result:

array(3) {
  ["service"]=>
  string(4) "topp"
  [0]=>
  string(3) "443"
  [1]=>
  string(3) "456"
}

while the 2 dimensional array is this:

array(2) {
  [0]=>
  array(1) {
    ["service"]=>
    string(3) "topp"
  }
  [1]=>
  array(1) {
    ["service"]=>
    string(3) "tok"
  }
}

array_merge_recursive() only makes sense when a) you don’t use loops and b) if you can find some kind of mapping between the two arrays. Since we only know the structure of the first array, we don’t have enough info to tell if array_merge_recursive() is a valid option to begin with.

Ok then,first array:

array(2) {
  [0]=>
  array(1) {
    ["service"]=>
    string(4) "topp"
  }
  [1]=>
  array(1) {
    ["service"]=>
    string(4) "tokk"
  }
}

second array:

array(2) {
  [0]=>
  string(3) "443"
  [1]=>
  string(3) "456"
}

In this case (if both arrays are of equal length) use

$result = array_map(null, $a, $b);

A completely different option is to employ the form data, since both arrays seem to come off a form.

<input type="checkbox" name="form[0][id]" value="443">

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.