I have array like this
datasets:[{"label":"admin ",
"backgroundColor":"#FF6384",
"data":"[0,0,3,30,53,6,0,0,54,0,0,6]"},
{"label":"user1",
"backgroundColor":"#FF6384",
"data":"[0,20,0,3,0,0,5,20,30,0,5,0]"}],
I want to Remove double quotes from data array
And I want change to this
datasets:[{"label":"admin ",
"backgroundColor":"#FF6384",
"data":[0,0,3,30,53,6,0,0,54,0,0,6]},
{"label":"user1",
"backgroundColor":"#FF6384",
"data":[0,20,0,3,0,0,5,20,30,0,5,0]}],
Using a RegEx too, but only editing on the data keys:
// Assuming your data string as $json
$json = preg_replace('/"data":"(\[[0-9,]*\])"/', '"data":$1', $json);
// Output to verify
echo '<pre>' . print_r(json_decode($json, true), true) . '</pre>';
Not too much elegant but functionally
preg_replace("/\"(\[.*\])\"/", "$1", $Json);
$1 is string that contain [something].
Related
I have data set smiller to dictonary and I want to get key value pair for this data.
here is DATA SET:
$data = "{u'test_field2': u'NONE', u'test_field3': u'NONE', u'test_account': u'NONE', u'test_account_1': u'NONE'}"
I am doing json_decode($data, true); but have no luck with it
Sorry If I am unclear.f
BTW I am doing it in PHP
the result should be like this:
test_field2: NONE
test_field3: NONE
Since your data is invalid json because of that u in it here is a solution
json_decode(str_replace("'",'"',str_replace("u'","'",$data)), true);
Should do the trick
You've to try like this because on your existing code couple of issue exists.
Replace Unnecessary character u' with '
Replace single quotes(') on json string. For json double quotes(") is permitted.
So just search these two characters and replace with '' and " respectively.
<?php
$data = "{u'test_field2': u'NONE', u'test_field3': u'NONE', u'test_account': u'NONE', u'test_account_1': u'NONE'}";
$search = ["u'","'"];
$replace = ["'",'"'];
$without_u = str_replace($search,$replace,$data);
$array = json_decode($without_u, true);
print '<pre>';
print_r($array);
print '</pre>';
?>
DEMO: https://eval.in/1032316
I have this string saved in $metaV:
{"feedName":"Paypal Test","paypalEmail":"managercvtech#gmail.com","mode":"test","transactionType":"subscription","recurringAmount":"form_total","billingCycle_length":"1","billingCycle_unit":"day","recurringTimes":"0","recurringRetry":"0","trial_enabled":"1","trial_product":"73","trial_amount":"","trialPeriod_length":"1","trialPeriod_unit":"month","billingInformation_firstName":"4","billingInformation_lastName":"27","billingInformation_email":"5","billingInformation_address":"","billingInformation_address2":"","billingInformation_city":"","billingInformation_state":"","billingInformation_zip":"34","billingInformation_country":"","pageStyle":"","continueText":"","cancelUrl":"","disableShipping":"0","disableNote":"0","delayNotification":"0","selectedNotifications":"","feed_condition_conditional_logic":"0","feed_condition_conditional_logic_object":{"conditionalLogic":{"actionType":"show","logicType":"all","rules":[{"fieldId":"73","operator":"is","value":"1 month"}]}},"type":"subscription","recurring_amount_field":"form_total","update_user_action":"","delay_registration":"","update_site_action":""}
I want to replace this part of the string:
"trial_enabled":"0"
I tried to use str_replace() for this:
str_replace('\"trial_enabled\":\"1\"', '\"trial_enabled\":\"0\"',$metaV);
You could use json_decode and json_encode to transform your string into an array and the way back.
$data = json_decode($metaV, true);
$data['trial_enabled'] = "1";
$metaV = json_encode($data);
I have this Json Object Below, I want to extract this data and output it in PHP
{"seat_booked":"A5","0":"A5","1":"A3"}
then get them into this format
$seat_booked = "'A5', 'A5', 'A3'";
How can I do this?
I hope you are looking for this, its very simple example by using json_decode():
$string = '{"seat_booked":"A5","0":"A5","1":"A3"}';
$decoded = json_decode($string,true);
$resuiredString = '"'."'".implode("','", $decoded)."'".'"';
echo $resuiredString;
Result:
"'A5','A5','A3'"
Side Note:
I suggest you to learn about variable concatenation.
PHP Concatenation
Another solution:
$json = '{"seat_booked":"A5","0":"A5","1":"A3"}';
$decoded = array_map(
function($val) {
return "'". $val."'";
},
array_values(json_decode($json, true))
);
To get an object from a json in php you can use json_decode has explained here.
But you have another problem, your json is wrong!
If you want to represent a single dimensional array you should at least do this
["A5","A5","A3"]
Finally, using json_decode:
$obj = json_decode('["A5","A5","A3"]');
var_dump($obj);
Also, you could do something like:
{"0":"A5","1":"A5","2":"A3"}
$obj = json_decode('{"0":"A5","1":"A3", "2": "A5"}', true);
var_dump($obj);
Edit:
It's not very clear from your question if you are trying to get back an object from a json or if you just want to get a string from it.
If what you need is an string then you don't even need json, you could do this by string manipulation and/or using regex.
But just for completeness, if a quoted comma separated string is what you need you can do this:
$array = json_decode('["A5","A5","A3"]');
$str = implode("','",$array);
$str = "'" . $str . "'";
var_dump($str);
I have comma separated value pairs and I would like to convert it to associative array in php.
Example:
{
Age:30,
Weight:80,
Height:180
}
Converted to:
Echo $obj['Weight']; // 80
Does it make a difference that my values are not in inverted commas? I mean:
Weight:80
Vs
Weight:'80'
P.S. I've posted from a phone, so I don't have a lot of fancy markup available to make this question look more presentable.
http://php.net/manual/en/function.json-decode.php
It's an JSON object which you would like to convert to an array.
$string = '{ "Age":30, "Weight":80, "Height":180 }';
$array = json_decode($string, true);
echo $array['Age']; // returns 30
Provided that the given string is a valid JSON.
UPDATE
If that doesn't work because the string doesn't contain a valid JSON object (because I see the keys are missing double quotes), you could execute this regex function first:
$string = "{ Age:30, Weight:80, Height:180 }";
$json = preg_replace('/(?<!")(?<!\w)(\w+)(?!")(?!\w)/u', '"$1"', $string); // fix missing quotes
$obj = json_decode($json, true);
echo $obj['Age']; // returns 30
When using the regex above, make sure the string doesn't contain any quotes at all. So make sure that not some keys have quotes and some not. If so, first get rid of all quotes before executing the regex:
str_replace('"', "", $string);
str_replace("'", "", $string);
You can get all values in an array by using this basic example:
// your string
$string = "{
Age:30,
Weight:80,
Height:180
}";
// preg_match inside the {}
preg_match('/\K[^{]*(?=})/', $string, $matches);
$matchedResult = $matches[0];
$exploded = explode(",",$matchedResult); // explode with ,
$yourData = array();
foreach ($exploded as $value) {
$result = explode(':',$value); // explode with :
$yourData[$result[0]] = $result[1];
}
echo "<pre>";
print_r($yourData);
Result:
Array
(
[Age] => 30
[Weight] => 80
[Height] => 180
)
Explanation:
(?<=}) look behind asserts.
K[^{] matches the opening braces and K tells what was matched.
I am trying to use the implode function on array; and its working fine and i am returning result fine as it should suppose to do. I would now like to add extra quotation marks at the start and end of each item.
EG: I am currently getting this result in implode:
jan,feb,march,april,etc,etc
Instead I would like each item to be quoted:
"jan","feb","march","april","etc","etc"
Here is little code how i am using to implode something from my array
$selectedMonths = implode(",",array_column($selectedMonths,'id'));
As it is already string, I tried this below code also, but it was of no use. as it is already a string, but when imploding the commas are not added.
foreach($selectedMonths as $value){
array_replace($selectedMonths,array_map('strval', array_slice($value, 0)));
}
Initially I am getting $selectedMonths as a json array, for which i have used json_decode() in php and in that foreach I have tried after converting the json array to associated array. But now how to add extra "" around every comma separated value?
This should work for you:
<?php
$array = array("jan", "feb", "march", "april", "etc", "etc");
echo '"' . implode('","', $array) . '"';
?>
Output:
"jan","feb","march","april","etc","etc"
It sounds like what you want to do, is get a JSON string from your array? For that you can use json_encode:
$json = json_encode($array);
But to append and prepend a quote mark to every string in an array, you can use array_map:
$array = ["jan","feb","march","april","etc","etc"];
$mapped = array_map($array, function($value) {
return '"' . $value . '"';
});