Insert data from a json string

0

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?

    
asked by user59375 12.09.2017 в 16:39
source

4 answers

0

Once coded with json_encode , change your foreach to add with index

foreach ($contResult as $value) {
   $valuesAux = []
   $valuesAux['CategoryName'] = $value->CategoryName;
   $valuesAux['CategoryId'] = $value->CategoryId;
   $values[] = $valuesAux;
}
return json_encode($values);
    
answered by 12.09.2017 / 17:16
source
0

This would not be the best way to do it but it is one of the ways that can be done.

<?php 
$json = '[
{
"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
}
]';


 $json = json_decode($json,true);


unset($json[0]['ParentCategoryId']);
unset($json[0]['CategoryGroupId']);
unset($json[0]['SearchKeys']);
unset($json[0]['DisplayOrder']);
unset($json[0]['IsRootCategory']);

unset($json[1]['ParentCategoryId']);
unset($json[1]['CategoryGroupId']);
unset($json[1]['SearchKeys']);
unset($json[1]['DisplayOrder']);
unset($json[1]['IsRootCategory']);

print_r($json);
 ?>
    
answered by 12.09.2017 в 17:13
0

Use this:

$values = array();
foreach ($contResult as $valor) {
    $reg;
    foreach($valor as $k=>$v){
        if($k=='CategoryName' || $k=='CategoryId'){$reg[$k]=$v;}
    }
    array_push($values,$reg);
}
return json_encode($values);

================================================================================================= br> version explanation : at the request of our partner @ lois6b

$values = array();
//recorres tus datos, considerando solo el "valor" (no el indice o clave) y que tienes 2 grupos de datos
foreach ($contResult as $valor) {
    //variable array, que almacena datos temporalmente
    $reg;
    //recorres los datos internos (categoryname, categoryid, etc), esta vez considerando clave y valor
    foreach($valor as $k=>$v){
        //si es clave deseada se asigna a array temporal
        if($k=='CategoryName' || $k=='CategoryId'){$reg[$k]=$v;}
    }
    //asignando el array temporal al array principal
    array_push($values,$reg);
}
return json_encode($values);
    
answered by 12.09.2017 в 17:29
0

You could use the function created by Jack P. , called array_remove_keys .

You pass in parameter two arrays, the array with all the data, and another array with the keys you want to remove.

If your array is something like this:

$arrOriginal=
        array(
                array(
                        "CategoryId"=> 41,
                        "ParentCategoryId"=> 0,
                        "CategoryGroupId"=> 4,
                        "CategoryName"=> "Almacenamiento interno",
                        "SearchKeys"=> null,
                        "DisplayOrder"=> 1,
                        "IsRootCategory"=> false),

                array(
                        "CategoryId"=> 42,
                        "ParentCategoryId"=> 0,
                        "CategoryGroupId"=> 4,
                        "CategoryName"=> "Almacenamiento interno2",
                        "SearchKeys"=> null,
                        "DisplayOrder"=> 1,
                        "IsRootCategory"=> false)
             );

You create an array with the keys you want to remove:

$arrRemover=array(
                   "ParentCategoryId",
                   "CategoryGroupId",
                   "SearchKeys", 
                   "DisplayOrder",
                   "IsRootCategory"
                 );

And you pass those two variables to the function:

$arrNuevo = array_remove_keys($arrOriginal, $arrRemover);
var_dump($arrNuevo);

The output ( var_dump ) in this case will be something like this:

array(2) {
  [0]=>
  array(2) {
    ["CategoryId"]=>
    int(41)
    ["CategoryName"]=>
    string(22) "Almacenamiento interno"
  }
  [1]=>
  array(2) {
    ["CategoryId"]=>
    int(42)
    ["CategoryName"]=>
    string(23) "Almacenamiento interno2"
  }
}

You can see a full demo here: SEE DEMO IN REXTESTER

    
answered by 12.09.2017 в 19:14