Reputation: 2649
I’m trying to query some data from a JSON file with PHP, i want to get objects based on string
and number
.
Here’s my JSON:
[
{
"id": "5V28tqJ1",
"string": "john doe",
"number": 1,
"aArray": [
"cc",
"kk",
"lo",
"mm"
],
"bool": false,
"createdAt": "2020-01-02T15:16:11",
"updatedAt": "2020-01-05T19:37:02"
},
{
"id": "PMuKM818",
"string": "sarah doe",
"number": 2,
"aArray": [
"bb",
"cc"
],
"bool": true,
"createdAt": "2020-01-02T16:16:23",
"updatedAt": "2020-01-05T19:37:14"
},
{
"id": "m8HSbEQe",
"string": "bob smith",
"number": 3,
"aArray": [
"dd",
"ee",
"ff"
],
"bool": false,
"createdAt": "2020-01-02T17:16:36",
"updatedAt": "2020-01-05T19:37:32"
}
]
Here’s my query-user.php
script:
<?php include 'header.php';
$string = $_GET['string'];
$aArray = $_GET['aArray'];
$number = $_GET['number'];
$bool = $_GET['bool'];
$data = file_get_contents($className. '.json');
$data = json_decode($data);
$results = array();
for ($i=0; $i<count($data); $i++) {
// check value in string
if(isset($string)){
if (strpos($data[$i]->string, $string) !== false) {
array_push($results, $data[$i]);
}
}
// check element in array
if(isset($aArray)){
if (in_array($aArray, $data[$i]->aArray)) {
array_push($results, $data[$i]);
}
}
// check value in number
if(isset($number)){
if (strpos($data[$i]->number, $number) !== false) {
array_push($results, $data[$i]);
}
}
// check value in bool
if(isset($bool)){
if ($data[$i]->bool == $bool) {
array_push($results, $data[$i]);
}
}
}// ./ for
echo json_encode($results, JSON_PRETTY_PRINT);
?>
On my browser, I call the following url:
http://example.com/users/query-user.php?string=doe&number=2
Here’s what I get as result:
[
{ "id": "5V28tqJ1", "string": "john doe", "number": 1, "aArray": [ "cc", "kk", "lo", "mm" ], "bool": false, "createdAt": "2020-01-02T15:16:11", "updatedAt": "2020-01-05T19:37:02" },
{ "id": "PMuKM818", "string": "sarah doe", "number": 2, "aArray": [ "bb", "cc" ], "bool": true, "createdAt": "2020-01-02T16:16:23", "updatedAt": "2020-01-05T19:37:14" },
{ "id": "PMuKM818", "string": "sarah doe", "number": 2, "aArray": [ "bb", "cc" ], "bool": true, "createdAt": "2020-01-02T16:16:23", "updatedAt": "2020-01-05T19:37:14" }
]
As you can see, I get the object with ID PMuKM818
twice, while I should get it once, together with the first object.
What am I doing wrong in my php code?
Upvotes: 0
Views: 168
Reputation: 138
For quick answer you just need to use: https://www.php.net/manual/en/function.array-column
What is fastest way to process json objects.
UPDATE:
for full your code correct please use: https://www.php.net/manual/en/function.array-search
combinate with array_column, what way you will search more faster then one by one.
Upvotes: 1
Reputation: 918
The object with the ID PMuKM818
corresponds to both of your query parameters: it contains Doe and also has the number 2.
If you want to prevent that, either set an order of precedence for your filters using if else
statements instead of simple if
, or add a check for existing ids in the resulting array before adding the element.
ADDITIONAL EDIT
Not the most elegant solution but it does what you need:
foreach ($data as $details) {
// check value in string
if(isset($string)){
if (strpos($details->string, $string) !== false) {
$results[$details->id] = $details;
}
}
// check element in array
if(isset($aArray)){
if (in_array($aArray, $details->aArray)) {
$results[$details->id] = $details;
}
}
// check value in number
if(isset($number)){
if (strpos($details->number, $number) !== false) {
$results[$details->id] = $details;
}
}
// check value in bool
if(isset($bool)){
if ($details->bool == $bool) {
$results[$details->id] = $details;
}
}
}// ./ for
echo json_encode(array_values($results), JSON_PRETTY_PRINT);
I've also replaced the for
loop with a foreach
, but that should not impact on the result
Upvotes: 1