Good, I'm trying to insert data from string json
, but I do not know how to show only the information I want.
I have the following:
[
{
"CategoryId": 41,
"ParentCategoryId": 0,
"CategoryGroupId": 4,
"CategoryName": "Almacenamiento interno",
"SearchKeys": null,
"DisplayOrder": 1,
"IsRootCategory": false
},
{
"CategoryId": 469,
"ParentCategoryId": 0,
"CategoryGroupId": 2,
"CategoryName": "Accesorios Portátil",
"SearchKeys": null,
"DisplayOrder": 1,
"IsRootCategory": false
}
]
And I want to pass it to this:
[
{
"CategoryId": 41,
"CategoryName": "Almacenamiento interno"
},
{
"CategoryId": 469,
"CategoryName": "Accesorios Portátil",
}
]
My main problem is that I do not know how to "remove" the information that I have left, or how to put the one I want in another string json (or array, I guess the database does not care much if I use a foreach
).
Here is an example of what I have tried:
'$contResult = json_decode($contentResult);
$values = array();
foreach ($contResult as $value) {
array_push($values, $value->CategoryName);
}
return $values;'
But it gives me an error ( Notice: Array to string conversion
) on line 4 here:
require 'api/classes/Autoloader.php';
$obj = new Request();
echo $obj->request();
I have managed to do what I tried (almost, at least). Now I am trying to put the keys together, this is what I have tried so far:
$contResult = json_decode($contentResult);
$values = array();
foreach ($contResult as $value) {
array_push($values, $value->CategoryName);
array_push($values, $value->CategoryId);
}
What gives me back the following ["Almacenamiento interno",41,"Accesorios Portátil",469]
, which would almost become what I intend to do. How can I separate them?