Decode the array values - php

I want to decode array values for passing it into json. I want to pass the values in contentvalue baaed on content type into json. Now It shows null. I want to move array value as $zip_num=$content->zip; based on content type.
while ($ee = mysql_fetch_array($query)) {
$key_val = $ee['CONTENT_TYPE'];
$content = json_decode($ee['CONTENT_VALUE']);
if ($key_val == 'stat_sum') {
$stat = $content;
}
if ($key_val == 'zip_stats') {
$zip[] = $content;
$zip_num=$content->zip;
$zip_cou=$content->count;
}
if ($key_val == 'qual_stats') {
$qual[] = $content;
}
}
$new = array('ID'=>$id,'zip'=>$zip_num);
echo $json = json_encode($new);
}

Replace $key_val (that one which is in if statement) with $ee['CONTENT_TYPE']

Related

json array is repeating index

this is my code :
if ($API->connect("192.168.81.130", "admin", "")) {
$API->write('/ip/route/print', false);
$API->write('=.proplist=.id', false);
$API->write('=.proplist=dst-address', false);
$API->write('=.proplist=pref-src', false);
$API->write('=.proplist=gateway');
$result = $API->read();
$API->disconnect();
foreach ($result as $route){
$response['id'] = $route['.id'];
$response['dst-address'] = $route['dst-address'];
if (isset($route['pref-src'])) {
$response['pref-src'] = $route['pref-src'];
} else {
$response['pref-src'] = "";
}
$response['gateway'] = $route['gateway'];
$array[] = $response;
echo json_encode($array);
}
}
and the output is :
[{"id":"*2","dst-address":"0.0.0.0\/0","pref-src":"","gateway":"192.168.1.1"}][{"id":"*2","dst-address":"0.0.0.0\/0","pref-src":"","gateway":"192.168.1.1"},{"id":"*420639B3","dst-address":"192.168.81.0\/24","pref-src":"192.168.81.130","gateway":"ether1"}]
result for "[{"id":"*2","dst-address":"0.0.0.0/0","pref-src":"","gateway":"192.168.1.1"}]" is show twice.
i want the out put like this :
> [{"id":"*2","dst-address":"0.0.0.0\/0","pref-src":"","gateway":"192.168.1.1"},{"id":"*420639B3","dst-address":"192.168.81.0\/24","pref-src":"192.168.81.130","gateway":"ether1"}].
can anyone help me?.
You need to initialize your array each time round the loop otherwise you are just adding to it each time, hence the duplication
You also need to move the echo of the json string to outside the loop
foreach ($result as $route){
$response = array();
$response['id'] = $route['.id'];
$response['dst-address'] = $route['dst-address'];
if (isset($route['pref-src'])) {
$response['pref-src'] = $route['pref-src'];
} else {
$response['pref-src'] = "";
}
$response['gateway'] = $route['gateway'];
$array[] = $response;
}
echo json_encode($array);

PHP String in Array returns only first charachter

I have a php function to display a list of revslider's sliders (wp plugin), the string returns only the first letter of the sliders' names
here is my code :
function jobboard_revslider(){
if (class_exists('RevSlider')) {
$theslider = new RevSlider();
$arrSliders = $theslider->getArrSliders();
$arrA = array();
$arrT = array();
foreach($arrSliders as $slider){
$arrA[] = $slider->getAlias();
$arrT[] = $slider->getTitle();
}
if($arrA && $arrT){
$result = array_combine($arrA, $arrT);
}
else
{
$result = false;
}
return $result;
}
}
I tried all I know and other answers around here but no hope.
I would really appreciate a push !
Thanks
Check sizeof ($ array)> 0 do this for both arrays. Also just try to echo what you are getting in getalias and gettitle methods before storing it in array.
function jobboard_revslider(){
if (class_exists('RevSlider')) {
$theslider = new RevSlider();
$arrSliders = $theslider->getArrSliders();
$arrA = array();
$arrT = array();
foreach($arrSliders as $slider){
$arrA[] = substr($slider->getAlias(), 1);
$arrT[] = substr($slider->getTitle(), 1);
}
if($arrA && $arrT){
$result = array_combine($arrA, $arrT);
}
else
{
$result = false;
}
return $result;
}

How to create an array and save them into database?

Based on the code below, how would I create an array and then save the arrays to the database?
<?php
$all_userid = '';
if (isset($_POST['userid'])) {
foreach($_POST['userid'] as $userid) {
// Add filtering here
$all_userid .= $userid;
}
}
echo $all_userid;
?>
Output from echo;
003210032100321
Use json_encode http://www.php.net/json_encode
$string = json_encode($array);
Then when retriving it from database, use json_decode http://php.net/json_decode
<?php
$all_statuses = '';
if (isset($_POST['status'])) {
foreach($_POST['status'] as $status) {
// Add filtering here
$all_statuses .= $status;
}
}
echo($all_statuses);
To Save in database serialize this array
serialize($all_statuses)
?>
you can implode them with an - or / or _ and save them.Like
$i = 0;
$cnt = count($_POST['status']);
foreach($_POST['status'] as $status) {
// Add filtering here
$all_statuses .= $status;
if($i++ < $cnt) {
$all_statuses .= '_';
}
}
echo $all_statuses;
Will gives you like
id1_id2_id3_id4....
And while retriving you simply explode them with the imploded string,you will get all the result in form of an array.

mysqli fetch array return null as result

have no idea my result return nothing
if ($stmt2->execute()) {
$photo_items = $stmt2->get_result();
while ($imgArray = $photo_items->fetch_array()) {
}
echo $imgArray[] = $imgArray;
}
and I also tried this
echo $imgArray[] = $imgArray['mycolname'];
Try
while ($img = $photo_items->fetch_array()) {
$imgArray[] = $img;
}
To view the array elements, try:
print_r($imgArray);

Encoding JSON with double quotes instead of single quotes

I have a json.php file which delivers results like this:
{ "markers": [ {'a1_id':"4213CK58", etc.
The problem is that the Google Maps API doesn't like single quotes and so I need my results like this:
{ "markers": [ {"a1_id":"4213CK58", etc.
Replacing ' by " in the code doesn't deliver...
<?php
// Iterate over the rows
$nextRow= $result->nextRow();
$r = 1;
$info = array();
while ( $nextRow ) {
$nextColumn = $result->nextColumn();
// Has this column been printed already
if ( $unique )
{
$d = $result->getDataForField($unique);
if ( array_key_exists($d, $already) )
{
$nextRow= $result->nextRow();
continue;
}
$already[$d] = true;
}
echo '{';
// Iterate over the columns in each row
while ( $nextColumn )
{
// Get the variable
$variable = $result->getOutputVariable();
$name = $variable->getName(true);
$data = $result->getDataForField();
if ( !isset($info[$name]) ) {
$info[$name]['translate'] = $variable->shouldTranslate();
$info[$name]['type'] = $variable->getDataType();
$info[$name]['linkable'] = $variable->isLinkable();
}
// Translate the data if requested
if ( $info[$name]['translate'] ) {
$data = LQMTemplate::_($data);
}
$data = $variable->format($data, false);
$type = $info[$name]['type'];
if ( ($type == 'bool') or ($type == 'boolean') )
{
$data = $data ? '1' : '0';
echo "'$name':$data";
} elseif ( $encode ) {
// Can we use json_encode ?
// str_replace because some versions of PHP have a bug that will over escape forward slashes
echo "'$name':".str_replace('\\/', '/', json_encode($data));
} else {
$data = LQMUtility::jsonEscape($data, '"');
echo "'$name':\"$data\"";
}
// Conditionally print the next column
$nextColumn = $result->nextColumn();
if ( $nextColumn ) echo ",\n ";
}
// Conditionally print the next column
$nextRow = $result->nextRow();
echo $nextRow ? "},\n" : "}\n";
$r++;
}
unset($result);
echo ']}';
}
}
Create an array containing the data you want to encode and then use PHP's builtin json_encode() function.

Categories