I have an associative array as follows:
$ a =
[
"2023-05-18" => 10.0
"2023-07-14" => 2.0
"2023-11-01" => 16.0
"2023-11-11" => 2.0
"2023-12-25" => 8.0
"2024-01-01" => 2.0
"2024-04-01" => 22.0
]
And in a SQL query i want to do something like:
function ($a) {
$sql = " ... some code before ...
IF (table.date IN ($a), value of date (ex 10.0), 1)
"
;
}
Not only with if with case when, or whatever the idea is to get the value in associative array according to the date (if it exist in the array);
I think the better way to do that is to concatenate the logic with your query and use a case when to handle that. something like
$s= "*\nCASE\n";
foreach ($yourArray as $dateKey => $dateValue) {
$s .= "WHEN DATE_FORMAT(table.date, '%Y-%m-%d') = '" . $dateKey . "' THEN
" . $dateValue . " \n"
;
}
$s .= "END\n";
And in you query you can directly do something like
"query do something
". $s ."
do something again "
I hope this would help
Your description of what you want is not very clear, but from the conversation and a bit of assumption I think it probably boils down to:
"list all the values from the PHP array where the date key matches at least one row from the SQL table"
If so, then here's one way you could do it:
Now here's some sample code. There may be a more efficient way to do it, but this is my initial thought:
$a =
[
"2023-05-18" => 10.0,
"2023-07-14" => 2.0,
"2023-11-01" => 16.0,
"2023-11-11" => 2.0,
"2023-12-25" => 8.0,
"2024-01-01" => 2.0,
"2024-04-01" => 22.0
];
$dates = array_keys($a);
$params = array_fill(0, count($dates), "?");
//sql to get all rows from database which match dates in the array
$sql = "SELECT `date` from `table` WHERE `date` IN (".implode(",", $params).")";
echo $sql; //just for debugging
//let's assume you're using PDO and have a connection object named $pdo, for the sake of argument
$stmt = $pdo->prepare($sql);
$result = $pdo->execute($dates);
$data = $result->fetchAll(PDO::FETCH_ASSOC);
//loop the returned set of matching dates
foreach ($data as $row)
{
//output values from the original array which match the dates returned from the database
echo $a[$row["date"]].PHP_EOL;
}
Related documentation:
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
DBMS is a database management system. It is designed to change, search, add and delete information in the database. There are many DBMSs designed for similar purposes with different features. One of the most popular is MySQL.
It is a software tool designed to work with relational SQL databases. It is easy to learn even for site owners who are not professional programmers or administrators. MySQL DBMS also allows you to export and import data, which is convenient when moving large amounts of information.
https://www.mysql.com/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.