Reputation: 13434
I have a simple query that looks for the same id inside the $_SESSION['cart_items']
keys. This is the output:
and the code:
$statement = $conn->query("SELECT * FROM product WHERE id IN (".implode(',',array_keys($_SESSION['cart_items'])).")");
$data = array();
while($row = $statement->fetch()) {
$data[] = $row;
}
print_r($data);
This works fine but I want to add a 5th element inside the array. The value will be coming from the value of the associative array $_SESSION['cart_items'][$row['id']]
inside the while loop. So far what I did:
while($row = $statement->fetch()) {
$data[] = $row;
if(array_key_exists($row['id'], $_SESSION['cart_items']))
{
$another = $_SESSION['cart_items'][$row['id']];
array_push($data, $another);
}
}
print_r($data);
But I get this output:
As you can see, there is an additional [1]=>23
and [3]=>47
but that's not what I want to happen. What I want to happen is something like this:
I want it to be a part of the array inside an array. Or more like the 5th element. Can I do something like this?
Upvotes: 1
Views: 4109
Reputation: 47864
If the goal is to relate and map data between your session array and your database table, then I don't see any reason to avoid SQL for this task.
This dynamic SQL will serve your WHERE
requirements and allow your table schema to change over time without causing key collisions in the PHP code. Furthermore, because there is no PHP processing, you can enjoy a fetch_all()
instead of using a loop to transfer the result set data to a 2d array.
Code: (PHPize Demo)
$_SESSION = [
'cart_items' => [
35 => 47,
1 => 23,
]
];
$derivedSubqueries = [];
foreach ($_SESSION['cart_items'] as $k => $v) {
$derivedSubqueries[] = sprintf(
"SELECT %d prod_id, %d another",
$k,
$v
);
}
$sql = 'SELECT product.*,
derived.another
FROM product
JOIN (
' . implode("\n\t UNION\n\t ", $derivedSubqueries) . '
) derived ON product.id = derived.prod_id';
echo "\t$sql\n\n";
foreach ($mysqli->query($sql) as $row) {
echo json_encode($row) . "\n";
}
Result:
SELECT product.*,
derived.another
FROM product
JOIN (
SELECT 35 prod_id, 47 another
UNION
SELECT 1 prod_id, 23 another
) derived ON product.id = derived.prod_id
{"id":"1","name":"ballpen","price":"15","quantity":"50","another":"23"}
{"id":"35","name":"qwe","price":"2","quantity":"19","another":"47"}
Upvotes: 0
Reputation: 153
try this and tell me if it working for you !
while($row = $statement->fetch()) {
if(array_key_exists($row['id'], $_SESSION['cart_items']))
{
$another = $_SESSION['cart_items'][$row['id']];
array_push($row, $another);
}
$data[] = $row;
}
print_r($data);
For your second question try this and let me know
while($row = $statement->fetch()) {
if(array_key_exists($row['id'], $_SESSION['cart_items']))
{
$another = $_SESSION['cart_items'][$row['id']];
$new_row = $row+array("YOUR_TEXT" => $another);
}
$data[] = $new_row;
}
print_r($data);
Upvotes: 1
Reputation: 4153
the reason it does not inserted to your sub array
because you push it into your main array which is
the $data
SOLUTION:
before pushing it
create a new variable
$newRow = $row;
then:
push your 5th element
$another = $_SESSION['cart_items'][$row['id']];
array_push($newRow, $another);
then lastly you push your new row which contains your 5th element to $data
$data[] = $newRow;
so your new php code would be like this
<?php
while($row = $statement->fetch()) {
$newRow = $row;
if(array_key_exists($row['id'], $_SESSION['cart_items']))
{
$another = $_SESSION['cart_items'][$row['id']];
array_push($newRow, $another);
}
$data[] = $newRow;
}
print_r($data);
Upvotes: 1