0

Hello I have the following scenario:

The following array is to be changed to a two-dimensional array.

Array
(
    [0] => Array
        (
            [0] => Name1
            [1] => Name2
            [2] => Name3
            [3] => Name4
            [4] => Name5
            [5] => Name6
            [6] => Name7
            [7] => Name8
        )

    [1] => Array
        (
            [0] => Company1
            [1] => Company2
            [2] => Company3
            [3] => Company4
            [4] => Company5
            [5] => Company6
            [6] => Company7
            [7] => Company8
        )

    [2] => Array
        (
            [0] => Street1
            [1] => Street2
            [2] => Street3
            [3] => Street4
            [4] => Street5
            [5] => Street6
            [6] => Street7
            [7] => Street8
        )

    [3] => Array
        (
            [0] => Date1
            [1] => Date2
            [2] => Date3
            [3] => Date4
            [4] => Date5
            [5] => Date6
            [6] => Date7
            [7] => Date8
        )

    [4] => Array
        (
            [0] => Date_2_1
            [1] => Date_2_2
            [2] => Date_2_3
            [3] => Date_2_4
            [4] => Date_2_5
            [5] => Date_2_6
            [6] => Date_2_7
            [7] => Date_2_8
        )

    [5] => Array
        (
            [0] => place1
            [1] => place2
            [2] => place3
            [3] => place4
            [4] => place5
            [5] => place6
            [6] => place7
            [7] => place8
        )

    [6] => Array
        (
            [0] => break1
            [1] => break2
            [2] => break3
            [3] => break4
            [4] => break5
            [5] => break6
            [6] => break7
            [7] => break8
        )

    [7] => Array
        (
            [0] => postcode1
            [1] => postcode2
            [2] => postcode3
            [3] => postcode4
            [4] => postcode5
            [5] => postcode6
            [6] => postcode7
            [7] => postcode8
        )

)

How the final array should look like

Array
(
    [0] => Array
        (
            [0] => Name1
            [1] => Company1
            [2] => Street1
            [3] => Date1
            [4] => Date_2_1
            [5] => place1
            [6] => break1
            [7] => postcode1
        )

    [1] => Array
        (
            [0] => Name2
            [1] => Company2
            [2] => Street2
            [3] => Date2
            [4] => Date_2_2
            [5] => place2
            [6] => break2
            [7] => postcode2
        )

    [2] => Array
        (
            [0] => Name3
            [1] => Company3
            [2] => Street3
            [3] => Date3
            [4] => Date_2_3
            [5] => place3
            [6] => break3
            [7] => postcode3
        )

    [3] => Array
        (
            [0] => Name4
            [1] => Company4
            [2] => Street4
            [3] => Date4
            [4] => Date_2_4
            [5] => place4
            [6] => break4
            [7] => postcode4
        )

    [4] => Array
        (
            [0] => Name5
            [1] => Company5
            [2] => Street5
            [3] => Date5
            [4] => Date_2_5
            [5] => place5
            [6] => break5
            [7] => postcode5
        )

    [5] => Array
        (
            [0] => Name6
            [1] => Company6
            [2] => Street6
            [3] => Date6
            [4] => Date_2_6
            [5] => place6
            [6] => break6
            [7] => postcode6
        )

    [6] => Array
        (
            [0] => Name7
            [1] => Company7
            [2] => Street7
            [3] => Date7
            [4] => Date_2_7
            [5] => place7
            [6] => break7
            [7] => postcode7
        )

    [7] => Array
        (
            [0] => Name8
            [1] => Company8
            [2] => Street8
            [3] => Date8
            [4] => Date_2_8
            [5] => place8
            [6] => break8
            [7] => postcode8
        )

)

function test($post_employee_nr){
require_once $_SERVER['DOCUMENT_ROOT'].'/module/dienstplan/_config.php';
$employee_query =  $dbh->query("SELECT FT.*,M.*,O.*,E.*,K.* FROM
            finish_time FT
        LEFT JOIN
            m_schicht M ON FT.m_schichtid = M.ID
        LEFT JOIN
            objekte O ON O.ID = M.objid
        LEFT JOIN
            mitarbeiter E ON E.ID = M.mitarbeiterid
        LEFT JOIN
            kunde K ON K.ID = M.kdid where FT.mitarbeiterid=$post_employee_nr")->fetchall();

    foreach ($employee_query as $row) {   
        $employee_ID[] = $row['FT.ID'];
        $customer[] = $row['kundenname'];
        $street[] = $row['straße'];
        $postcode[] = $row['plz'];
        $place[] = $row['ort'];
        $begin[] = $row['b_time'];
        $end[] = $row['e_time'];
        $break[] = $row['pause'];
        

   $output = array($employee_ID, $customer,$street,$postcode,$place,$begin,$end,$break);  
    } 

$html = $output;

  
    $response = $html;
echo json_encode($response);
}

I hope I could make it obvious enough

EDIT

This is my solution:

     $result = array();
foreach($employee_query as $employee_query) {


$result[] = array(
    $days[date('l', strtotime($employee_query['b_time']))],
    date("d.m.Y", strtotime($employee_query['b_time'])),        
    $employee_query['kundenname'],
    $employee_query['strasse'],
    $employee_query['plz'].' '.$employee_query['ort'],
    date("H:i", strtotime($employee_query['b_time'])),
    date("H:i", strtotime($employee_query['e_time'])),
    '<i class="fas fa-plus-circle" style="color:green;"></i>'
    );
}
echo json_encode($result);
     exit();
}
3
  • 1
    And what is your question? What have you tried so far? Can you provide your code? What are you having trouble with? Commented Oct 5, 2020 at 13:35
  • 1
    Why the value are changing ? Commented Oct 5, 2020 at 13:38
  • 1
    The array you showed us had three elements on the lowest level each, but in the code you appear to be stuffing eight values into the arrays. Please show example data that actually matches the code, resp. vice versa. Commented Oct 5, 2020 at 13:55

1 Answer 1

1

If you want to change it to a 2 layer array just create a variable that holds the first element of the 3 layer array:

somthing like:

var array2D = array3D[0];

Also this bit of code seems like its not necesarry

$html = array($output); 
$response = $html;
echo json_encode($response);

unless you need the array to be 3 layers when encoding it to Json. Otherwise just change it to:

echo json_encode($output);

Hopefully I understood your question and was able to help a little.

Edit

The way your foreach loop is currently running you're only creating a new array for each element then adding the same elements to their respective array and finally storing every array inside a new one (1 array with ALL id's, and one with ALL companynames etc...)

to fix it is very simple.

inside your foreach loop the $row variable looks like this:

$row => [idvalue, companyvalue, streetvalue etc....]

it's already an array containing the current $row's data, now all you need to do is directly add it to your $output array.

You're new foreach loop should look something like this:

foreach($employee_query as $row) {
    $output[] = $row; // when using [] after a variable you add to that array
}
echo json_encode($output);

if you don't want to use all the data that your query collected you can specify which attributes you want to use like so:

$output[] = array($row['FT.ID'], $row['kundenname'], $row['straße'], etc...);
Sign up to request clarification or add additional context in comments.

2 Comments

Hello, first of all thanks for the answer, I also discovered the error. I would love to mix the arrays as stated above.
@Maximum Glad to hear it helped, I've updated my answer. If there's anything else you need just let me know.

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.