php


Solve New Line in JSON via PHP


I'm Parsing This json Array and I Want to Take type Object and Put That in New Column type2, and This is one Row of My json Rows,
I Get Invalid argument supplied for foreach() Because of New Line in json in Some Rows. How Can I Solve This?
This One is Not Okey
[{"id":"26","answer":[{"option":"4","text":"Hello
"}],"type":"3"}]
AndThis One is Okey
[{"id":"26","answer":[{"option":"4","text":"Hello"}],"type":"3"}]
And This is My Code:
<?php
$con=mysqli_connect("localhost","root","","array");
mysqli_set_charset($con,"utf8");
// Check connection
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="SELECT `survey_answers`,us_id FROM `user_survey_start`";
if ($result=mysqli_query($con,$sql)){
while ($row = mysqli_fetch_row($result)){
$json = $row[0];
if(!is_null($json)){
$jason_array = json_decode($json,true);
// type2
$type = array();
foreach ($jason_array as $data) {
if (array_key_exists('type', $data)) {
// Now we will only use it if it actually exists
$type[] = $data['type'];
}
}
// lets check first your $types variable has value or not?
if(!empty($type)) {
$types= implode(',',$type); /// implode yes if you got values
}
else {
$types = ''; //blank if not have any values
}
$sql2="update user_survey_start set type2='$types' where us_id=".$row[1];//run update sql
echo $sql2."<br>";
mysqli_query($con,$sql2);
}
}
}
mysqli_close($con);
?>
Replace your new line with \n before json decode:
$json = preg_replace('/\r|\n/','\n',trim($json));
$jason_array = json_decode($json,true);
The problem is invalid JSON format.
If your text content have multi lines, you should be use \n, not typing a enter.
[{"id":"26","answer":[{"option":"4","text":"Hello\n"}],"type":"3"}]
^^

Related Links

PHP curl_multi_exec output to array
Wordpress Define Variable Plugin
Getting Extra Data Based on Original SQL Result
Get the selected value of a <select> tag in php
PHP code with Jumi component in Joomla 3.3.1
including from document root in PHP
unable to load dashboard page after login successful in codeigniter
Why does ltrim remove one character too much?
Getting post values after submit
How to arrange a multidimensional associative array based on values in the same array in php
Only bulleted list shown when using 'simplexml_load_file()' in PHP to read RSS Feeds
Is it possible to update the order item quantity in Magento?
Entering Values To DB With PDO With Foreach Loop
Connection to mysql database: PDO Premature end of data
“Mage” file in magento root
Php button action event needed

Categories

HOME
elasticsearch
apache-nifi
yaml
yahoo-weather-api
yii
dafny
code-formatting
iverilog
informatica
web2py
mirc
dependencies
openflow
swift2
dji-sdk
linker
repair
ag-grid
sharepoint-online
eddystone
reportviewer
elastic-load-balancer
vimeo
bundler
sonicwall
web-deployment
user-defined-types
fosuserbundle
gmp
adminlte
cmake-gui
libgit2
yii2-basic-app
data-type-conversion
sipp
cell
software-packaging
mod-wsgi
web-config-transform
clipboard
region
source-insight
rust-cargo
nsmutablearray
listjs
image-optimization
custom-keyboard
creation
autoencoder
grails-2.5
netstat
sendinput
reverse-dns
realstudio
dredd
supertest
password-hash
renderman
clrs
rustdoc
bpms
portal
cefpython
xcode6.4
xmlbeans
fuzzer
zynq
tsqlt
system.net.mail
raw-sockets
between
nsdata
right-to-left
gnucash
wikimedia-commons
myfaces
enunciate
juniper-network-connect
extend
phpredis
gflags
r.java-file
jcheckbox
adwords-apiv201402
storing-data
lmax
accessory
git-reset
bittorrent-sync
tabletools
usb-flash-drive
jmenubar
coalesce
libxml-js
insertion
unicoins
notin
axacropdf
jbox2d
asp.net-mvc-3-areas
ckeditor.net
user-forums
zend-pdf
nsfont
navigationcontroller
startupscript
cewolf
upshot
photo-management
git-log
fuzzy-comparison
activex-exe
code-camp

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App