Retrieving data from array in php - 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>

Related

PHP Change multidimensional array structure

Hello I've multidimensional array that looks like that:
array(13890) {
[0]=>
array(2) {
["Icd"]=>
array(2) {
["id"]=>
int(111)
["nazwa"]=>
string(6) "DŻUMA"
}
["ProjectIcd"]=>
array(0) {
}
}
[1]=>
array(2) {
["Icd"]=>
array(2) {
["id"]=>
int(566)
["nazwa"]=>
string(7) "ŚWINKA"
}
["ProjectIcd"]=>
array(0) {
}
}
An so on.
I want to change it so it looks something like that:
array(13890) {
[0]=> array(2) {
["id"]=>
int(111)
["text"]=>
string(6) "DŻUMA"
}
How is this possible to do?
I want to add, I want to convert the array to json and feed it to select2 js in ajax.
Will that be a problem or not?
Short solution using array_map function:
// $arr is your initial array
$new_arr = array_map(function($a){
return ['id' => $a['Icd']['id'], 'text' => $a['Icd']['nazwa']];
}, $arr);
So you can simple create a new array and add there the values, which you want based on the old array. Then you convert the array to a json string with the php function json_encode():
$array = array("text"=>$old_array[0]["Icd"]["nazwa"]);
echo json_encode($array);
I hope this is something that you want.
$res = [];
$i = 0;
foreach($array as $arr) {
//get keys
if (count($arr) > 0) {
$keys = array_keys($arr);
// loop through the keys and fetch data of those keys
// put in array
foreach($keys as $key) {
if ($arr[$key]) {
$res[$i]['id'] = $arr[$key]['id'];
$res[$i]['text'] = $arr[$key]['nazwa'];
}
$i++;
}
}
}
print_r($res);
// To change array to json
echo json_encode($res);

get variable from json

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

Print single field from JSON in PHP

I've been trying for hours now to get the title field from the json code. Below is my php code
$search = $_GET['search'];
$new = str_replace(' ', '+', $search);
$url = "http://api.themoviedb.org/3/search/movie?api_key=###&query=".$new;
$json = file_get_contents($url);
$json_data = json_decode($json, true);
$title = $json_data->title;
echo $title;
this is the var dump of the json
array(4) { ["page"]=> int(1) ["results"]=> array(1) { [0]=> array(10) { ["adult"]=> bool(false) ["backdrop_path"]=> string(32) "/4uJZvmEMX6Z6ag3bzym5exLY9wI.jpg" ["id"]=> int(65) ["original_title"]=> string(6) "8 Mile" ["release_date"]=> string(10) "2002-11-08" ["poster_path"]=> string(32) "/dXzTrKwpbLpCqn8O70FUUhNbYQT.jpg" ["popularity"]=> float(3.792332418578) ["title"]=> string(6) "8 Mile" ["vote_average"]=> float(6.2) ["vote_count"]=> int(185) } } ["total_pages"]=> int(1) ["total_results"]=> int(1) }
the error i keep getting is Notice: Trying to get property of non-object
any help would be greatly appreciated.
$json_data = json_decode($json, true);
will return an array not object
so you need to use as
$json_data["title"];
NOTE : Your json decoded array is nested so you may need to use as in your case.
$json_data["results"][0]["title"];
Or better loop through and get the desired data.
I just started using PHP and I THINK this might help you... but i'n not sure
foreach($json_data as $key => $value){
if($key == "results"){
$json_data2 = $value;
}
}
foreach($json_data2 as $key => $value){
if($key == "original_title"){
$title = $value;
}
}
echo $title;

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);

nested array with json

$url2 = "http://www.website.com/test.json";
$json2 = file_get_contents($url2);
$data2 = json_decode($json2);
foreach($data2 as $mydata2) {
$product_discount = $mydata2->applied_discounts;
var_dump($product_discount);
}
This is returning:
array(1) {
[0]=> object(stdClass)#2 (2) {
["id"]=> string(6) "coupon"
["amount"]=> float(9.99)
}
}
I want to return just the amount of "9.99"
I tried $product_discount[0]['amount'], but that doesn't seem to be right??
It's an object, so you need the following syntax:
$product_discount = $mydata2->applied_discounts[0]->amount;
However, if you want an array instead, you could set json_decode()'s second parameter as TRUE:
$data2 = json_decode($json2, TRUE);
You want to do this:
$product_discount = $mydata2->applied_discounts[0]->amount;
It's an object, not an array. Try...
$product_discount[0]->amount

Categories