I have the following PHP code which searches the array by the key name and returns result equals to the search. Just for curiosity how can I do that, that no matter what I search for the result will be elements where the name key value equals "A"? Shortly how can I retrieve elements where name is "A" ?
I tried the following but does´t work:
$jSearchResult[] = $ajProducts[$i]->name = "A";
Please help me cause I just simply can´t figure out how to retrive elements of an array by key value, however I am sure it must be something very simple.
<?php
//DATA coming from the BROWSER
$sSearch = $_GET['search'];
//TURN it into UPPERCASE
strtoupper( $sSearch );
//GETTING from the TEXT FILE:
$sajProducts = file_get_contents( 'products.txt' );
$ajProducts = json_decode( $sajProducts );
$match_found = false;
//Collect all matching result in an array
$jSearchResult = array();
//LOOPING THROUGH THE ARRAY OF PRODUCTS
for ( $i=0; $i< count( $ajProducts ); $i++ ) {
if ( $sSearch == $ajProducts[$i]->name ) {
$jSearchResult[] = $ajProducts[$i]->name;
$match_found = true;
}
}
//if there is a match display the product
if ( $match_found ) {
echo json_encode ( $jSearchResult );
exit;
}
//if not display ALL products
else {
echo json_encode ( $ajProducts );
exit;
}
?>
$ajProducts looks like this:
[
{
"id": "59d278cae7017",
"name": "A",
"price": "1",
"quantity": 2,
"image": "img_webshop\/productimage-59d74304917c2.jpg"
},
{
"id": "59d27e20c8028",
"name": "A",
"price": "2",
"quantity": 1,
"image": "img_webshop\/productimage-59d743233c0cf.jpg"
},
{
"id": "59d6a7ae16d15",
"name": "A",
"price": "3",
"quantity": 2,
"image": "img_webshop\/productimage-59d743392fbb5.jpg"
},
{
"id": "59d6d6ee5f752",
"name": "A",
"price": "4",
"quantity": 1,
"image": "img_webshop\/productimage-59d74352d5b94.jpg"
},
{
"id": "59d743d207bd5",
"name": "B",
"price": "5",
"quantity": 1,
"image": "img_webshop\/productimage-59d743d1e6e64.jpg"
},
{
"id": "59d74451225ac",
"name": "B",
"price": "6",
"quantity": 0,
"image": "img_webshop\/productimage-59d7445120871.jpg"
},
{
"id": "59e0d992d1f3b",
"name": "C",
"price": "6",
"quantity": 2,
"image": "img_webshop\/productimage-59e725ac79583.jpg"
}
]
There is a php function that does just that: http://php.net/array_filter
$searchResult = array_filter($ajProducts, function ($product) {
return $product->name === 'A';
});
This will get you all objects in $ajProducts having name property set to 'A'.
To see if there're any results:
$matchFound = !empty($searchResult);
If you want to match elements where the name key value equals "A", then you can just make a check with value of name key is equal to "A" like -
foreach ($ajProducts as $ajProduct ) {
if ( "A" == $ajProduct->name ) {
$jSearchResult[] = $ajProduct->name;
$match_found = true;
}
}
Related
I tried to parse a JSON using PHP from url. I need a list of item that have no children - means children field should be empty array and parent_id shouldn't be 0 - means not be parents.
JSON:
{
"body": {
"data": [
{
"id": 1,
"name": "car",
"parent_id": "0",
"children": [
{
"id": 2,
"name": "bmw",
"parent_id": "1",
"children": [
{
"id": 4,
"name": "bmw i8",
"parent_id": "2",
"children": []
}
]
},
{
"id": 3,
"name": "mustang",
"parent_id": "1",
"children": []
}
]
},
{
"id": 5,
"name": "clothes",
"parent_id": "0",
"children": []
},
{
"id": 6,
"name": "mobile",
"parent_id": "0",
"children": [
{
"id": 7,
"name": "apple",
"parent_id": "6",
"children": [
{
"id": 9,
"name": "Iphone 12 pro",
"parent_id": "7",
"children": []
},
{
"id": 10,
"name": "Iphone 11",
"parent_id": "7",
"children": []
}
]
},
{
"id": 8,
"name": "Xiaomi",
"parent_id": "6",
"children": []
}
]
}
]
}
}
Output Expected:
[4,3,9,10,8]
This is my php code that i tried and doesn't work.
$CategoryUrl = file_get_contents(self::CATEGORY_URL,true);
$array = json_decode($CategoryUrl,true);
$list = array();
foreach( $array['body']['data'] as $value ){
if (($value['parent_id'] != 0) && empty($value['children'])) {
foreach( $value['children'] as $val ){
$list[] = $val;
}
}
}
print_r($list);
It is indeed a little tricky to keep track of parent ID values and children call using array_walk_recursive as it jumps directly to its children. However, this can be still accomplished with your own recursive version like below. Keep checking with parent_id and children count. If both constraints satisfy, add them to $result, else keep calling children recursively.
<?php
$str = '{"body":{"data":[{"id":1,"name":"car","parent_id":"0","children":[{"id":2,"name":"bmw","parent_id":"1","children":[{"id":4,"name":"bmw i8","parent_id":"2","children":[]}]},{"id":3,"name":"mustang","parent_id":"1","children":[]}]},{"id":5,"name":"clothes","parent_id":"0","children":[]},{"id":6,"name":"mobile","parent_id":"0","children":[{"id":7,"name":"apple","parent_id":"6","children":[{"id":9,"name":"Iphone 12 pro","parent_id":"7","children":[]},{"id":10,"name":"Iphone 11","parent_id":"7","children":[]}]},{"id":8,"name":"Xiaomi","parent_id":"6","children":[]}]}]}}';
$data = json_decode($str,true);
$result = [];
function walkRecursive($data,&$result){
foreach($data as $entry){
if($entry['parent_id'] != 0 && count($entry['children']) == 0){
$result[] = $entry['id'];
}else{
walkRecursive($entry['children'],$result);
}
}
}
walkRecursive($data['body']['data'],$result);
print_r($result);
We can solve this problem by function call recursive algorithm,
hope it clear for you
$CategoryUrl = file_get_contents(self::CATEGORY_URL,true);
$array = json_decode($CategoryUrl, true);
$items = $array['body']['data'];
$list = [];
findParentIds(items, $list);
// doSomething
print_r($list);
/**
* passe by ref (list)
* #param array $children
* #param $list
*/
function findParentIds(array $children, & $list)
{
foreach ($children as $child) {
// use case 1: has no children and parent_id is 0 , just continue
if (empty($child['children']) && $child['parent_id'] == "0") {
continue;
}
// use case 2: has no children and parent_id is 0 , it's parent item
if (empty($child['children']) && $child['parent_id'] != "0") {
array_push($list, $child['id']);
}
// has children and parent_id is not 0 , recall function to treat use case 1 and 2 ..
findParentIds($child['children'], $list);
}
}
I'm retrieving data from a database and pushing it to arrays then use json_encode() to output in json format. I have ended up having the data in this format.
[
{
"category_id": "1",
"category_name": "Construction Materials"
},
{
"items": [
{
"id": "1",
"item_name": "Wire Mesh",
"price": "459",
"image_url": null
},
{
"id": "2",
"item_name": "Cement",
"price": "700",
"image_url": null
},
{
"id": "3",
"item_name": "Barbed Wire",
"price": "3000",
"image_url": null
},
{
"id": "4",
"item_name": "Iron sheet",
"price": "200",
"image_url": null
}
]
},
{
"category_id": "2",
"category_name": "Plumbing"
},
Here is what I want to achieve:
[
{
"category_id": "1",
"category_name": "Construction Materials"
"items": [
{
"id": "1",
"item_name": "Wire Mesh",
"price": "459",
"image_url": null
},
{
"id": "2",
"item_name": "Cement",
"price": "40",
"image_url": null
},
{
"id": "3",
"item_name": "Barbed Wire",
"price": "3000",
"image_url": null
},
{
"id": "4",
"item_name": "Iron sheet",
"price": "200",
"image_url": null
}
]
},
{
"category_id": "2",
"category_name": "Plumbing"
},
How can I achive this in php. Is it possible to edit the contents of the main array? how can I add "items" after "category_name"
Regards..
$array = json_decode($yourJson);
$arrayLength = count($array);
$finalArray = [];
for ($i=0; $i < $arrayLength; $i+=2) {
$finalArray[] = $array[$i] + $array[$i + 1];
}
$backToJson = json_encode($finalArray);
Alternative to Mattijs's answer.
function get_first_property($object) {
$properties = array_keys(get_object_vars($object));
return reset($properties);
}
function object_merge($object1, $object2) {
return (object) array_merge((array) $object1, (array) $object2);
}
// loop through each of the array objects
$previous_first_property = null;
foreach ($array as $i => $object) {
$first_property = get_first_property($object);
// merge if the 1st property is "items", and the previous 1st was "category_id"
if ($first_property == "items" && $previous_first_property == "category_id") {
$array[$i - 1] = object_merge($array[$i - 1], $array[$i]);
unset($array[$i]);
}
$previous_first_property = $first_property;
}
I have an array, which i wrote like a table for more reability.
[{
"id": "1",
"title": "boune",
"value": "3"
}, {
"id": "2",
"title": "check",
"value": "4"
}, {
"id": "3",
"title": "boune",
"value": "5"
}]
As the result of what i want to see is this json, where 'value' of identical 'titles' united inside [] brackets. I am very newbie in php. I undestand that i need for loop to sort query result, but can`t heck...Cant even put my mind to it...
[{
"id":"1",
"title": "boune",
"value": [3, 5]
}]
<?php
$json = '[{"id":"1","title":"boune","value":"3"},{"id":"2","title":"check","value":"4"},{"id":"3","title":"boune","value":"5"}]';
$json_data = json_decode($json,true);
function accumulateValuesByTitle($json_data){
$hash = [];
foreach($json_data as $each_record){
if(isset($hash[$each_record['title']])){
$hash[$each_record['title']]['value'][] = $each_record['value'];
if($hash[$each_record['title']]['id'] > $each_record['id']){
$hash[$each_record['title']]['id'] = $each_record['id'];
}
}else{
$hash[$each_record['title']] = $each_record;
$hash[$each_record['title']]['value'] = [$hash[$each_record['title']]['value']];
}
}
return array_values($hash);
}
$modified_data = accumulateValuesByTitle($json_data);
echo json_encode($modified_data);
Well, I have a web project and I have to be saving things temporarily, I started work with a json file, so far I can add and update.
The json file looks like this:
[
{
"username": "Baldwin",
"products": [
{
"id": 0,
"amount": 10
},
{
"id": 1,
"amount": 9
},
{
"id": 2,
"amount": 9
}
]
},
{
"username": "Alice",
"products": [
{
"id": 0,
"amount": 11
},
{
"id": 1,
"amount": 13
},
{
"id": 2,
"amount": 6
}
]
},
{
"username": "Terry",
"products": [
{
"id": 0,
"amount": 12
},
{
"id": 1,
"amount": 14
},
{
"id": 2,
"amount": 5
}
]
}
]
The problem comes when I want to delete an specific array or when I want to delete it completely, I can do it and it works fine, but I have the doubt about why when I delete the object, other fields are add to the json file, like an id.
When i delete just one product inside of the "products" array something like this happen:
[
{
"username": "Baldwin",
"products": { "1": { "id": 1, "amount": 9 }, "2": { "id": 2, "amount": 9 } }
},
{
"username": "Alice",
"products": [
{ "id": 0, "amount": 11 },
{ "id": 1, "amount": 13 },
{ "id": 2, "amount": 6 }
]
},
{
"username": "Terry",
"products": [
{ "id": 0, "amount": 12 },
{ "id": 1, "amount": 14 },
{ "id": 2, "amount": 5 }
]
}
]
And when i delete a complete array from the json file, something like this happen:
{
"1": {
"username": "Alice",
"products": [
{ "id": 0, "amount": 11 },
{ "id": 1, "amount": 13 },
{ "id": 2, "amount": 6 }
]
},
"2": {
"username": "Terry",
"products": [
{ "id": 0, "amount": 12 },
{ "id": 1, "amount": 14 },
{ "id": 2, "amount": 5 }
]
}
}
My php file to delete:
<?php
// load file
$data = file_get_contents('results.json');
// decode json to associative array
$json_arr = json_decode($data, true);
$flag = false;
// We check if the user wants to delete all or just one product
if(isset($_POST["all"])):
$username = $_POST["username"];
foreach ($json_arr as $key => $value):
// find the username on the json file
if($value["username"] == $username):
unset($json_arr[$key]);
break;
endif;
endforeach;
elseif(isset($_POST["one"])):
$username = $_POST["username"];
$id = $_POST["id"];
foreach ($json_arr as $key => $value):
// find the username on the json file
if($value["username"] == $username):
// loop products of the current username
foreach ($json_arr[$key]["products"] as $k => $product):
// find the id of the product
if($json_arr[$key]["products"][$k]["id"] == (int)$id):
// delete the product
unset($json_arr[$key]["products"][$k]);
endif;
endforeach;
endif;
endforeach;
endif;
// encode json and save to file
file_put_contents('results.json', json_encode($json_arr));
// redirect to show.php
header("Location: show.php");
?>
I've been taking a look to questions like this one but i couldn't find something with php, i would like to know how to solve this or if this is normal.
What happens when you use unset($json_arr[0]) is that the first element is removed, but the keys are not updated. If you inspect the array after the removal, you'll find that your array has two elements, at $json_arr[1] and $json_arr[2].
When you then perform a json_encode($json_arr) on this, PHP's JSON decoder looks at the array and since arrays are supposed to begin at the 0th element but this array begins at 1, it decides that in order to preserve the keys, the array would have to be converted to an associative array - which transforms the integer array keys into string keys in JSON.
For a short and quick solution, you can try:
$json_arr = array_diff($json_arr, [$key]);
You could even use array_splice or array_values - see here for inspiration.
I am making a search based on name of the product. It does work actually and it does find the product according search, however the problem is when I have several products with the same name it only shows me the first one. How could it be optimized? I would appreciate your help.
<?php
//DATA coming from the browser
$sSearch = $_GET['search'];
//TURN it into UPPERCASE
strtoupper( $sSearch );
//GETTING FROM FILE:
$sajProducts = file_get_contents( 'products.txt' );
$ajProducts = json_decode( $sajProducts );
$matchfound = false;
//LOOPING THROUGH THE ARRAY OF PRODUCTS
for ( $i=0; $i< count( $ajProducts ); $i++ ) {
if ( $sSearch == $ajProducts[$i]->name ) {
$jSearchResult = $ajProducts[$i];
$matchfound = true;
break;
}
}
//if there is a match display the product
if ( $matchfound ) {
echo json_encode ( $jSearchResult );
exit;
}
//if not display ALL products
else {
echo json_encode( $ajProducts );
exit;
}
?>
And the JSON products.text file:
[
{
"id": "59d278cae7017",
"name": "A",
"price": "1",
"quantity": 3,
"image": "img_webshop\/productimage-59d74304917c2.jpg"
},
{
"id": "59d27e20c8028",
"name": "A",
"price": "2",
"quantity": 3,
"image": "img_webshop\/productimage-59d743233c0cf.jpg"
},
{
"id": "59d6a7ae16d15",
"name": "A",
"price": "3",
"quantity": 2,
"image": "img_webshop\/productimage-59d743392fbb5.jpg"
},
{
"id": "59d6d6ee5f752",
"name": "A",
"price": "4",
"quantity": 4,
"image": "img_webshop\/productimage-59d74352d5b94.jpg"
},
{
"id": "59d743d207bd5",
"name": "B",
"price": "5",
"quantity": 1,
"image": "img_webshop\/productimage-59d743d1e6e64.jpg"
},
{
"id": "59d74451225ac",
"name": "B",
"price": "6",
"quantity": 1,
"image": "img_webshop\/productimage-59d7445120871.jpg"
},
{
"id": "59e0d95cd4111",
"name": "B",
"price": "4",
"quantity": "5",
"image": "img_webshop\/productimage-59e0d95cd2c4b.jpg"
},
{
"id": "59e0d992d1f3b",
"name": "C",
"price": "6",
"quantity": "5",
"image": "img_webshop\/productimage-59e0d992d19be.jpg"
},
{
"id": "59e0d9c59fbf2",
"name": "D",
"price": "4",
"quantity": "5",
"image": "img_webshop\/productimage-59e0d9c59f1a5.jpg"
}
]
Collect all matching result in an array and then return it in JSON.
$jSearchResult = array();
for ( $i=0; $i< count( $ajProducts ); $i++ ) {
if ( $sSearch == $ajProducts[$i]->name ) {
$jSearchResult[] = $ajProducts[$i];
$matchfound = true;
//break;
}
}
So, here after foreach loop completes the iteration for all json values, we will have final array of all the matches in $jSearchResult
then return it:
if ( $matchfound ) {
echo json_encode ( $jSearchResult );
exit;
}
Use this code:
$matchfound = false;
$jSearchResult = array();
//LOOPING THROUGH THE ARRAY OF PRODUCTS
for ( $i=0; $i< count( $ajProducts ); $i++ ) {
if ( $sSearch == $ajProducts[$i]->name ) {
$jSearchResult[] = $ajProducts[$i];
$matchfound = true;
}
}
Instead of this:
$matchfound = false;
//LOOPING THROUGH THE ARRAY OF PRODUCTS
for ( $i=0; $i< count( $ajProducts ); $i++ ) {
if ( $sSearch == $ajProducts[$i]->name ) {
$jSearchResult = $ajProducts[$i];
$matchfound = true;
break;
}
}