get variable from json - php

i have this:
{"sliders":{"c1":{"content":[{"title":1,"content_type":"image_content"}]}}}
i can get the title using the code below:
$decoded = json_decode($list[$i]['info'],true);
$json = $decoded['sliders']['c1']['content'][0]);
$x = $json['title'];
echo $x;
when i am trying to get the content_type...
$y = $json['content_type'];
echo $y;
...then it shows me undefined index error..why this happens?

First of all: The statement…
$json = $decoded['sliders']['c1']['content'][0]);
…should have given a syntax error for the excessive right bracket )
A var_dump of $decoded shows this:
object(stdClass)#1 (1) {
["sliders"]=>
object(stdClass)#2 (1) {
["c1"]=>
object(stdClass)#3 (1) {
["content"]=>
array(1) {
[0]=>
object(stdClass)#4 (2) {
["title"]=>
int(1)
["content_type"]=>
string(13) "image_content"
}
}
}
}
}
So, the entire variable $decoded is an object of objects.The object content is an array of one (in this case) object: content[0].
Therefore you would access the two items like this - using the object and array notation where appropriate:
echo $decoded->sliders->c1->content[0]->title;
echo $decoded->sliders->c1->content[0]->content_type;
OR
$json = $decoded->sliders->c1->content[0];
echo $json->title;
echo $json->content_type;
…which both would give:
1
image_content

Related

How to add data to OBJECT from Wordpress get_results in PHP

Seems really easy, but I can't seem to figure it out...
I have a simple line that gets mysql results through wordpress like this:
$sql_results = $wpdb->get_results($sql_phrase);
Then I parse it as JSON and echo it: json_encode($sql_results);
However, I want to add other data before I parse it as JSON. But I'm not sure how.
$sql_results basically gets me a list of post ID's, title and category.
It looks like this in var_dump (this is just the first row):
array(1)
{
[0]=> object(stdClass)#2737 (7)
{
["ID"]=> string(4) "2700"
["post_title"]=> string(18) "The compact helmet"
["category"]=> string(5) "Other"
}
}
Now to start with something easy, I'd like all associative arrays inside the object to have the extra key-value. I tried the following but got an error:
500 Internal error.
foreach($sql_search as $key => $value)
{
$value['pic_img'] = "test";
$sql_search[$key]=$value;
}
$result=$sql_search;
$sql_results = array(1)
{
[0]=> object(stdClass)#2737 (7)
{
["ID"]=> string(4) "2700"
["post_title"]=> string(18) "The compact helmet"
["category"]=> string(5) "Other"
}
}
foreach($sql_results as $key=>$value)
{
$value->solution = 'good';
$sql_results[$key]=$value;
}
$result=$sql_results;
var_dump($result);
$test = array ( array("ID"=>"35", "name"=>"Peter", "age"=>"43"),
array("ID"=>"34", "name"=>"James", "age"=>"19"), array("ID"=>"31", "name"=>"Joe", "age"=>"40") );
foreach($test as $key=>$value)
{
$value['solution'] = 'good';
$test[$key]=$value;
}
$result=$test;
var_dump($result);

Retrieving data from array in php

I am trying to retrieve data from this array in php.
array(2) {
["getWysiwyg"]=>
string(37) "[{"basicsDescription":"<p><br></p>"}]"
["getGoal"]=>
string(27) "[{"iconURL":"","title":""}]"
}
I tried Input::get('getWysiwyg') it returns [{"basicsDescription":"<p><br></p>"}]
Now how could i get the value i.e <p><br></p>
As I see your array items are json encoded ..
Try to decode them as this:
foreach($array as $key=>$value){
$decodedValue = json_decode($value, true);
print_r($decodedValue);
}
You have to use json_decode(), because the string [{"basicsDescription":"<p><br></p>"}]represents an array with an object in JSON.
$string = '[{"basicsDescription":"<p><br></p>"}]';
$objectArray = json_decode( $string );
$objectArray now looks like:
array(1) {
[0]=>
object(stdClass)#1 (1) {
["basicsDescription"]=>
string(11) "<p><br></p>"
}
}
To get the value of basicsDescription you need to access the array in this case with the index 0, then you have the object:
$object = $objectArray[0];
Once you've got the object you can access it's attributes with the object operator ->:
$object->basicsDescription;// content: <p><br></p>
Short form of this:
$string = '[{"basicsDescription":"<p><br></p>"}]';// in your case Input::get('getWysiwyg')
$objectArray = json_decode( $string );
$objectArray[0]->basicsDescription;
If it's possible, that there are more than one item in it, you should go for foreach
If all items of your array representing JSON strings you can use array_map():
$array = array(
"getWysiwyg" => '[{"basicsDescription":"<p><br></p>"}]',
"getGoal" => '[{"iconURL":"","title":""}]'
);
$array = array_map( 'json_decode' , $array );
echo "<pre>";
var_dump( $array );
This will output:
array(2) {
["getWysiwyg"]=>
array(1) {
[0]=>
object(stdClass)#1 (1) {
["basicsDescription"]=>
string(11) "<p><br></p>"
}
}
["getGoal"]=>
array(1) {
[0]=>
object(stdClass)#2 (2) {
["iconURL"]=>
string(0) ""
["title"]=>
string(0) ""
}
}
}
Decode and print as follows
$object = json_decode(Input::get('getWysiwyg'));
print $object[0]->basicsDescription;
or directly with the help of array dereferencing
print json_decode(Input::get('getWysiwyg'))[0]->basicsDescription;
will output
<p><br></p>

array return wrong value laravel

I make an ajax request and the response its an array of images, the problem is that i echo the value of the array and its the one that i want, but when the foreach ends and i see what have the array every value its changed by the last item of the array
foreach ($x as $y) {
$auxImg->misc_id = $y->misc_id;
$auxImg->image = $y->image;
$aux[$i] = $auxImg;
echo $aux[$i]->image.' ';
//response of the array in the echo
/* 5/maqueta.png - 5/ponto.png - 5/ciades.jpg - 5/35235.jpg */
$i++;
}
echo var_dump($aux);
//response in the var_dump of the aux array
array(4) {
[0]=>
object(stdClass)#400 (2) {
["misc_id"]=>
int(9)
["image"]=>
string(11) "5/35235.jpg"
}
[1]=>
object(stdClass)#400 (2) {
["misc_id"]=>
int(9)
["image"]=>
string(11) "5/35235.jpg"
}
[2]=>
object(stdClass)#400 (2) {
["misc_id"]=>
int(9)
["image"]=>
string(11) "5/35235.jpg"
}
[3]=>
object(stdClass)#400 (2) {
["misc_id"]=>
int(9)
["image"]=>
string(11) "5/35235.jpg"
}
}
i really cant understand why this happend, that is the only time i use $aux var please help
The problem here is that $auxImg is all the time the same object, so in each step you modify this object and append it to array but because $auxImg is an object it's not copied.
You should add
$auxImg = new stdClass();
or
$auxImg = clone $auxImg;
(depending what code you use before loop)
after:
foreach ($x as $y) {
to get expected result.

Joomla 3.0 JFactory::getApplication() get always returns null

I'm trying the extract the array data from a nested array (shown is part of a var_dump):
["passcodes"]=> array(2) {
[0]=> array(1) {
[0]=> string(33) "pAWn78hI2Uw5FA9iSGVuAkvISM0LTWL9X"
}
[1]=> array(1) {
[0]=> string(33) "dfS7VHqEXmcSkBubESaA0mIt8rEy2fSWE"
}
}
With the following PHP:
<?php
$app = JFactory::getApplication();
$input = $app->input;
$codes= $input->get('passcodes',array(),'array');
echo "*********** ".$codes." **********";
print_r($codes);
var_dump($codes);
var_dump($input);
?>
^^ I can't seem how to figure out the declaration for $codes, it's always null and size 0
Instead of $input->get('passcodes',array(),'array') try:
$vars = 'passcodes'; // Associative array of keys and filter types to apply
$source = 'youArrayName'; // Array to retrieve data from
$codes = $input->getArray(array $vars, $source);

How to access JSON decoded array in PHP

I returned an array of JSON data type from javascript to PHP, I used json_decode($data, true) to convert it to an associative array, but when I try to use it using the associative index, I get the error "Undefined index" The returned data looks like this
array(14) { [0]=> array(4) { ["id"]=> string(3) "597" ["c_name"]=> string(4) "John" ["next_of_kin"]=> string(10) "5874594793" ["seat_no"]=> string(1) "4" }
[1]=> array(4) { ["id"]=> string(3) "599" ["c_name"]=> string(6) "George" ["next_of_kin"]=> string(7) "6544539" ["seat_no"]=> string(1) "2" }
[2]=> array(4) { ["id"]=> string(3) "601" ["c_name"]=> string(5) "Emeka" ["next_of_kin"]=> string(10) "5457394839" ["seat_no"]=> string(1) "9" }
[3]=> array(4) { ["id"]=> string(3) "603" ["c_name"]=> string(8) "Chijioke" ["next_of_kin"]=> string(9) "653487309" ["seat_no"]=> string(1) "1" }
Please, how do I access such array in PHP? Thanks for any suggestion.
As you're passing true as the second parameter to json_decode, in the above example you can retrieve data doing something similar to:
$myArray = json_decode($data, true);
echo $myArray[0]['id']; // Fetches the first ID
echo $myArray[0]['c_name']; // Fetches the first c_name
// ...
echo $myArray[2]['id']; // Fetches the third ID
// etc..
If you do NOT pass true as the second parameter to json_decode it would instead return it as an object:
echo $myArray[0]->id;
$data = json_decode($json, true);
echo $data[0]["c_name"]; // "John"
$data = json_decode($json);
echo $data[0]->c_name; // "John"
$data = json_decode(...);
$firstId = $data[0]["id"];
$secondSeatNo = $data[1]["seat_no"];
Just like this :)
As you're passing true as the second parameter to json_decode, in the above example you can retrieve data doing something similar to:
<?php
$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
var_dump(json_decode($json));
var_dump(json_decode($json, true));
?>
This may help you!
$latlng='{"lat":29.5345741,"lng":75.0342196}';
$latlng=json_decode($latlng,TRUE); // array
echo "Lat=".$latlng['lat'];
echo '<br/>';
echo "Lng=".$latlng['lng'];
echo '<br/>';
$latlng2='{"lat":29.5345741,"lng":75.0342196}';
$latlng2=json_decode($latlng2); // object
echo "Lat=".$latlng2->lat;
echo '<br/>';
echo "Lng=".$latlng2->lng;
echo '<br/>';
When you want to loop into a multiple dimensions array, you can use foreach like this:
foreach($data as $users){
foreach($users as $user){
echo $user['id'].' '.$user['c_name'].' '.$user['seat_no'].'<br/>';
}
}

Categories