Bonjour,
J'ai une variable en json :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
$json = '[
	{
	"Quantity": 0,
	"ID": "014",
	"Color": "red"
	},
	{
	"Quantity": 10,
	"ProductID": "015",
	"Color": "black"
	},
	{
	"Quantity": 25,
	"ProductID": "018",
	"Color": "green"
	},
	{
	"Quantity": 0,
	"ProductID": "045",
	"Color": "yellow"
    }
]';
Je vais supprimer tous les articles qui ont la quantité "zéro" ["Quantity": 0] pour obtenir cela :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
$json = '[
	{
	"Quantity": 10,
	"ProductID": "015",
	"Color": "black"
	},
	{
	"Quantity": 25,
	"ProductID": "018",
	"Color": "green"
	}
]';
Et je vais garder les autres. Comment puis-je faire ?
Je suis un peu bloqué dans mes raisonnements. Voici ce que je fais :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
 
$json = '[
	{
	"Quantity": 0,
	"ID": "014",
	"Color": "red"
	},
	{
	"Quantity": 10,
	"ProductID": "015",
	"Color": "black"
	},
	{
	"Quantity": 25,
	"ProductID": "018",
	"Color": "green"
	},
	{
	"Quantity": 0,
	"ProductID": "045",
	"Color": "yellow"
    }
]';
 
 
 
$objArray = json_decode($json, true);
//die(var_dump($obj));
 
foreach ($objArray as  $objArrayValue) {
 
    foreach ($objArrayValue as $key => $value) {
        // $newArray []=[
        //     $key => [$value]
 
        // ];
 
        $newArray =[
            $key => [$value]
 
        ];
        print_r  ($newArray);
    }
    //print_r ($newArray);
}
////var_dump ($newArray);
En fait, je n'arrive même pas bien ordonner mon array...

Est-ce qu'il y a une fonction pour supprimer certains blocs du json sans transformer en tableau (array) ?
Sinon comment je peux ordonner ma nouvelle tableau et ensuite comment je peux les supprimer ?

Merci