1

In my PHP file, I'm receiving a total of 4 variables $data, $date, $shift and $val1. $data is an array and the other 3 are date and 2 strings obtained through AJAX with no problem. What I'm trying to do is to insert these 3 values inside my $data variable.

I tried using array merge, and a For each loop with multiple instances but no luck so far.

I obtained my variables like this:

if (isset($_POST['date'])){
    $date = $_POST['date'];
    $date = json_encode($date);
    $date = json_decode($date);
}

if (isset($_POST['shift'])){
    $shift = $_POST['shift'];
    $shift = json_encode($shift);
    $shift = json_decode($shift);
}

if (isset($_POST['val1'])){
    $val1 = $_POST['val1'];
    $val1 = json_encode($val1);
    $val1 = json_decode($val1);
}

if (isset($_POST['data'])){
    $dat = $_POST['data'];
    $data = json_decode($dat, true);
}

$values = array($date,$shift,$val1);

$r = (array_merge($data, $values)); 

My data array looks something like this:

Array (
    [0] => Array ( 
        [data] => Array ( 
            [0] => Array (
                [0] => 1
                [1] => 2
                [2] => 3 
                [3] => 0 
                [4] => Mat1 
                [5] => Box1 
                [6] => 100 
                [7] => 100 
                [8] => Piece1
                [9] => Loc1 
                [10] => Mach1 
                [11] => 1000
                [12] => Accepted 
                )  
            ) 
        ) 
    [1] => 2019-04-09 
    [2] => First
    [3] =>  Value1
)

But what I want to achieve is this:

Array (
    [0] => Array ( 
        [data] => Array ( 
            [0] => Array (
                [0] => 1
                [1] => 2
                [2] => 3 
                [3] => 0 
                [4] => Mat1 
                [5] => Box1 
                [6] => 100 
                [7] => 100 
                [8] => Piece 1
                [9] => Suc1 
                [10] => Mach1 
                [11] => 1000
                [12] => Accepted
                [13] => 2019-04-09 
                [14] => First
                [15] => Value1
                )  
            ) 
        ) 
    )

What am I doing wrong? Or How can I achieve what I'm trying to do?

Edit: Since I can get more than one array at my array, something like this

Array (
    [0] => Array ( 
        [data] => Array ( 
            [0] => Array (...)
            [1] => Array (...)
            [2] => Array (...)
            [3] => Array (...)  
            ) 
        ) 
    )

I just added this code to @HelgeB answer, I'm leaving it here in case someone might need it in the future.

$count = count($data[0]['data']);

for ($i=0; $i < $count ; $i++) { 
  $data[0]['data'][$i][] = $date;
  $data[0]['data'][$i][] = $shift;
  $data[0]['data'][$i][] = $val1;
} 
2
  • 1
    Would something like $_POST['data'][0][] = $_POST['date'] before assigning $r do what you're looking for? Commented Apr 17, 2019 at 21:23
  • I've tried that and it just gives me an offset warning or it just adds another parentheses to the result Commented Apr 18, 2019 at 2:31

1 Answer 1

2

As far as I can see from your merged output, your $data array structure is $data[0]['data'][0] = [1,2,3,...,'Accepted']. So in my opinion you need to insert the values exactly on the level $data[0]['data'][0] to obtain your result.

The simplest way to achieve this would be:

$data[0]['data'][0][] = $date;
$data[0]['data'][0][] = $shift;
$data[0]['data'][0][] = $val1;

If you want to use your merge approach you need to merge on the correct level like this:

$r = [0 => ['data' => [0 => (array_merge($data[0]['data'][0], $values))]]]; 
Sign up to request clarification or add additional context in comments.

1 Comment

Well this worked like a charm! Thanks! As a side note, I can also get more than 1 variable at $data[0]['data'][0], so I just added a count and a for loop to add it to every other array. I will edit my question to add that in case someone needs it in the future.

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.