0

I have string like this:

 $filters= {"DestinationZone":"","date":"2019-12-13","PolicyName":"","CloseReason":"","ApplicationName":"","level":"","Host":""}

I want to go through this string and divide it to key and value
I tried a lot but I don't know why it does not want to work. this is the code which I tried

   $filters=str_replace('{','', $filters);
  $filters=str_replace('}','', $filters);
 $filters=explode(',',$filters);    
  for($i=0;$i<count($filters);$i++){
$xx=explode(':',$filters[$i]);
var_dump($xx);
foreach ($xx as $xx) 
{  $value=trim($xx,'"');
var_dump($value);
 // $key=trim($key,'"');

if (!empty($value)) {

 //$query->where("$key", '=', "$value");
}
 }

  }

I think this code is very mess , I am using laravel

Abolfazl Mohajeri
  • 1,734
  • 2
  • 14
  • 26
stack over
  • 89
  • 1
  • 7

1 Answers1

1

This is not about Laravel frameworks, but just basic PHP.


Variable

You have an error with the $filters variable.

$filters= {"DestinationZone":"","date":"2019-12-13","PolicyName":"","CloseReason":"","ApplicationName":"","level":"","Host":""}

The JSON you have must be a string.

$filters = '{"DestinationZone":"","date":"2019-12-13","PolicyName":"","CloseReason":"","ApplicationName":"","level":"","Host":""}';

Key - Value

If you want to convert $filters to an array, you can just use :

$filters = json_decode($filters, true);

Result

array:7 [
  "DestinationZone" => ""
  "date" => "2019-12-13"
  "PolicyName" => ""
  "CloseReason" => ""
  "ApplicationName" => ""
  "level" => ""
  "Host" => ""
]
Wahyu Kristianto
  • 8,719
  • 6
  • 43
  • 68