I want to assign one key and value pair to one index position

Viewed 10

I have created two arrays, one for ProductID and one for CostToClient, they get their data from the results of an SQL query. I want to combine the two arrays, with ProductID being the key and CostToClient being the value. I got that right but when I printed the data all the keys and values were under index 0.

I want to assign one key and value pair to one index position then move to the next index position and assign the next key and value pair so I can make use of the array indexes at a later stage in the code.

This is what I have so far:

<?php

include_once('connect.php');

$sqlPIDCTC = "SELECT ProductID, CostToClient FROM bmp_pharmacy_calender_product_specs";
$resultPIDCTC = $con->query($sqlPIDCTC);

$sqlQPID = "SELECT Quantity, ProductID FROM bmp_pharmacy_calender_orders";
$resultQPID = $con->query($sqlQPID);

$combined_PIDCTC = array();
$i = 0;
if (mysqli_num_rows($resultPIDCTC) >0){
    while($row = mysqli_fetch_assoc($resultPIDCTC)){
        $PID[] = $row['ProductID'];
        $CTC[] = $row['CostToClient'];
        $combined_PIDCTC[$i] = array_combine($PID[$i], $CTC[$i]);
        $i = +1;
    }
    return array($combined_PIDCTC);
}

print_r($PID);
print_r($CTC);
print_r($combined_PIDCTC);

$con->close();
?>

This is the error message I get:

PHP Fatal error:  Uncaught TypeError: array_combine(): Argument #1 ($keys) must be of type array, string given in C:\Users\blah blah blah\calculate.php:17
Stack trace:
#0 C:\Users\blah blah blah\calculate.php(17): array_combine('0001', '997.73')
#1 {main}
  thrown in C:\Users\blah blah blah\calculate.php on line 17

I want to mention I am new to PHP so I'm learning as I go, any assistance would be appreciated.

Edit:

I don't understand how the 'CostToClient' and 'ProductID' reference in

$PID[] = $row['ProductID'];
$CTC[] = $row['CostToClient'];

are strings as I have included those column names in the SQL query. Am I missing something obvious here ??

0 Answers
Related