0

I have an array:

[
{"sku":"5221","qty":1,"price":17.5,"desc":"5395 - Replenish Natural Hydrating Lotion 3.5oz"},
{"sku":"11004","qty":1,"price":30.95,"desc":"150 - Q-Plus 16oz"}}

Now I want to delete the first row by matching sku, something like if sku == 5221 than delete whole record row:

{"sku":"5221","qty":1,"price":17.5,"desc":"5395 - Replenish Natural Hydrating Lotion 3.5oz"}

How do I do that?

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
Karan Adhikari
  • 485
  • 1
  • 5
  • 16
  • 3
    By the time you're doing this, you're not dealing with JSON, just an array. JSON is a *textual notation* for data exchange. If you're dealing with JavaScript source code, and not dealing with a *string*, you're not dealing with JSON. – T.J. Crowder Apr 12 '16 at 12:40
  • convert JSON string into a PHP data type (array/object) using `json_decode()` Then process the PHP Array of objects just like any other – RiggsFolly Apr 12 '16 at 12:42
  • Generally, read the JSON into an array, then manipulate the array as needed (adding, removing, modifying rows/cells). Just write the array back to a json file when you are done manipulating it. – Rabbit Guy Apr 12 '16 at 12:47

3 Answers3

1

Simply convert the JSON STRING into a PHP data type using json_decode() In this case the data type will be an array of objects as json is wrapped in [] and objects are denoted by {}

Then process the resulting array looking for the Key you want to delete and UNSET that array occurance.

<?php
$json_string =
'[
{"sku":"5221","qty":1,"price":17.5,"desc":"5395 - Replenish Natural Hydrating Lotion 3.5oz"},
{"sku":"11004","qty":1,"price":30.95,"desc":"150 - Q-Plus 16oz"}
]';

$array = json_decode($json_string);

echo 'before'.PHP_EOL;
print_r($array);

foreach ($array as $key => &$obj) {
    if ( $obj->sku == 5221 ) {
        unset($array[$key]);
        break;
    }
}
echo 'After'.PHP_EOL;
print_r($array);
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
0

PHP has a handy array_filter method that can easily take care of this. Basically, what you're trying to do is say: if the current array does not match the one I want to remove, include it by returning TRUE:

$badSku = "5221";

$myNewArray = array_filter($myArray, function($currObject){
  return $currObject["sku"] != $badSku;
});

You could also do this manually with a traditional loop. Just make sure to start from the end to avoid pointer issues:

$badSku = "5221";

for($i = count($myArray); $i > 0; $i--) {
  if($myArray[$i]["sku"] == $badSku) {
    array_splice($myArray, $i, 1);

    // If you know there is only a single match, you can break here for optimal efficiency
    break;
  }
}
Matthew Herbst
  • 29,477
  • 23
  • 85
  • 128
0

use unset() function to delete the record from array...

<?php
$string = '[{"sku":"5221","qty":1,"price":17.5,"desc":"5395 - Replenish Natural Hydrating Lotion 3.5oz"},{"sku":"11004","qty":1,"price":30.95,"desc":"150 - Q-Plus 16oz"}]';
$array = json_decode($string, true);
foreach($array as $key => $val)
{
    if($val['sku']=='5221')
    {
        unset($array[$key]);
    }
}
echo "<pre>"; print_r($array);
echo json_encode($array);
?>

This will output :

Associative array look like this:

Array
(
    [1] => Array
        (
            [sku] => 11004
            [qty] => 1
            [price] => 30.95
            [desc] => 150 - Q-Plus 16oz
        )

)

JSON array will be like this and deleted record sku = 5221

{"1":{"sku":"11004","qty":1,"price":30.95,"desc":"150 - Q-Plus 16oz"}}
Manjeet Barnala
  • 2,975
  • 1
  • 10
  • 20