why json alert data returing Undefined - php

after the alert(JSON.stringify(ort_list));
I'm getting output like this.
[{"status":"success","result":{"BUNDESLAND_NAME":"Rheinland-Pfalz","KREIS_TYP":"Landkreis","GEMEINDE_NAME":"Aach","GEMEINDE_LAT":"4978960","GEMEINDE_LON":"659052","ID_0":"2","ORT_NAME":"Aach","ORT_LAT":"4978972","ORT_LON":"659055","PLZ":"54298"}}]
now I want to get the alert value of BUNDESLAND_NAME how could I get that.

Use this:
var data = [{"status":"success","result":{"BUNDESLAND_NAME":"Rheinland-Pfalz","KREIS_TYP":"Landkreis","GEMEINDE_NAME":"Aach","GEMEINDE_LAT":"4978960","GEMEINDE_LON":"659052","ID_0":"2","ORT_NAME":"Aach","ORT_LAT":"4978972","ORT_LON":"659055","PLZ":"54298"}}];
alert(data[0].result.BUNDESLAND_NAME);
or directly, based on your code
alert(ort_list[0].result.BUNDESLAND_NAME);
assume
ort_list = [{"status":"success","result":{"BUNDESLAND_NAME":"Rheinland-Pfalz","KREIS_TYP":"Landkreis","GEMEINDE_NAME":"Aach","GEMEINDE_LAT":"4978960","GEMEINDE_LON":"659052","ID_0":"2","ORT_NAME":"Aach","ORT_LAT":"4978972","ORT_LON":"659055","PLZ":"54298"}}];

<?php
$data='[{"status":"success","result":{"BUNDESLAND_NAME":"Rheinland-Pfalz","KREIS_TYP":"Landkreis","GEMEINDE_NAME":"Aach","GEMEINDE_LAT":"4978960","GEMEINDE_LON":"659052","ID_0":"2","ORT_NAME":"Aach","ORT_LAT":"4978972","ORT_LON":"659055","PLZ":"54298"}}]';
$newData=json_decode($data,true);
echo '<pre>';
print_r($newData);
echo '</br>';
echo 'the field you are looking for is : '.$newData[0]['result']['BUNDESLAND_NAME'];
And the output is :
Array
(
[0] => Array
(
[status] => success
[result] => Array
(
[BUNDESLAND_NAME] => Rheinland-Pfalz
[KREIS_TYP] => Landkreis
[GEMEINDE_NAME] => Aach
[GEMEINDE_LAT] => 4978960
[GEMEINDE_LON] => 659052
[ID_0] => 2
[ORT_NAME] => Aach
[ORT_LAT] => 4978972
[ORT_LON] => 659055
[PLZ] => 54298
)
)
)
the field you are looking for is : Rheinland-Pfalz
So here i provide you how your array looks like and how i access the specific element you wanted. I hope it's clear.

Related

How do i get separate field in array

I want to get separate field in array, I have array and some fields but I need single fields how can get below I given my code and out put of data(print_r) please how can I do this one, I'm new in php.
I want only for 'code' field only how can I get...
print_r() I'm getting this output:
Array ( [0] => Array ( [id] => 1 [code] => kg1 [name] => Kindergarden [status] => 1 [craeteddate] => 2022-02-03 17:33:05 )
My php code:
$data['classes'] = $this->enquiryform_model->get_class();
$class_code = $data['classes']['code'];
you have two arrays. 1 inside another. So to get the code you need to do this.
<?php
$data['classes'] = array( 0 => array( 'id' => 1, 'code' => 'kg1', 'name' => 'Kindergarden', 'status' => 1, 'craeteddate' => '2022-02-03 17:33:05' ));
$class_code = $data['classes'][0]['code'];
echo $class_code;
echo "\n";
$class_code = $data['classes'][0];
echo $class_code['code'];
?>
Here is the working example

Get form serialize value from the array list

I have an array like this:
Array
(
[action_name] => edit
[formData] => color=red&size=full&symmetry=square&symmetry=circle&symmetry=oval
)
Here, form data is coming using the serialize method of JS and so it is displayed like above. I want to get each data from the formData key. How can I get that?
I tried:
$_POST['formData']['color']
But that is not working. I think the method to fetch this shall be different. How can I do that?
You can use parse_​str to "parse string as if it were the query string passed via a URL and sets variables in the current scope (or in the array if result is provided)."
<?php
$_POST = [
'action_name' => 'edit',
'formData' => 'color=red&size=full&symmetry=square',
];
parse_str($_POST['formData'], $parsed);
print_r($parsed);
will output
Array
(
[color] => red
[size] => full
[symmetry] => square
)
Edit:
Having multiple values for symmetry, your query should look like:
<?php
$_POST = [
'action_name' => 'edit',
'formData' => 'color=red&size=full&symmetry[]=square&symmetry[]=circle&symmetry[]=oval',
];
parse_str($_POST['formData'], $parsed);
print_r($parsed);
This would output:
Array
(
[color] => red
[size] => full
[symmetry] => Array
(
[0] => square
[1] => circle
[2] => oval
)
)

Print a specific value from array in ci

I stored some data to an array using this code ($rval['arr']):
$id = $this->input->get("id");
$rval['arr'] = $this->General_model->details($id);
When I print my result by using print_r($rval['arr']); it shows like this:
Array ( [0] => Array ( [id] => 54 [name] => Rajib [address] => DumDum [mobile] => 9865321245 [doj] => 21-2-2010 [fare] => 1245 [img_name] => Penguins.jpg ) )
Now I want to store the address value to a variable like add. How can I do this?
Try this....
$address=$rval['arr'][0]['address'];
demo
You can also do it in a foreach() loop:
foreach($rval['arr'] as $array){
echo $array['name'];
echo $array['address'];
echo $array['mobile'];
}

Echo a specific value on a very nested array

I have a function that generate an array (I did not made that function, comes from an API) and then saves it to a variable($customerList). I need to display just one value of an entire nested array; this array contains all this code (shown with print_r($customerList);) :
Array
(
[0] => Customer Object
(
[status:protected] =>
[creation_date:protected] => 2016-01-14T12:07:07-06:00
[balance:protected] =>
[clabe:protected] => 321654qweasd
[derivedResources:protected] => Array
(
[cards] => 123123123123
//a lot of code
How can I display just the value on "[cards]" using echo?
I have tried
//the for loop {
echo $customerList['0']['derivedResources:protected']['cards'];
And
//the for loop {
echo $customerList['0']['derivedResources']['cards'];
But it just display nothing. I haven't seen an array like this, so maybe is just a newbie mistake.
I think that "protected" thing has something to do with; but if I can display it, I can read/access it, right?
If any of you need the entire array (it's very long) I can put it here.
Thanks.
I haven't seen an array like this
It is an array of objects. Learn something about OOP.
This should work:
echo $customerList[0]->derivedResources['cards'];
Based on the print_r:
Array
(
[0] => Customer Object
(
[status:protected] =>
[creation_date:protected] => 2016-01-14T12:07:07-06:00
[balance:protected] =>
[clabe:protected] => 321654qweasd
[derivedResources:protected] => Array
(
[cards] => 123123123123
//a lot of code
We can simulate:
<?php
$CustomerObject = new \stdClass;
$derivedResources = new \stdClass();
$CustomerObject->derivedResources = ["cards" => 123123123123];
$customerList = [$CustomerObject];
// print_r($customerList);
echo $customerList[0]->derivedResources['cards'];
Which, uncommenting the print_r results in:
Array
(
[0] => stdClass Object
(
[derivedResources] => Array
(
[cards] => 123123123123
)
)
)
123123123123

How to access sub array in JSON - PHP

I have posted parts of this.. but this a different question for it
i have the below
foreach ($results['comments'] as $item) {
echo 'Date: '. $item['created_at'] .'<br/>';
echo 'Description : '. $item['html_body'] .'<br/>';
echo 'Attachments : '. $item['attacments->url'] .'<br/>';
echo 'Filename : '. $item['file_name'] .'<br/>';
echo "<br>";
}
So basically, my Date and Description work, BUT the attachments wont work, b/c i dont think thats the correct way to get an object thats within an array of an array? hope i explained it correctly.
the comments array has all the date as a single object and so is description, then it has this trailing.
[public] => 1 [trusted] => 1 [attachments] => Array ( [0] => Array ( [url] => https://url/api/v2/attachments/IDHERE.json [id] => ID#[file_name] => name of file here
Take a look at your array dump
[public] => 1
[trusted] => 1
[attachments] => Array (
[0] => Array (
[url] => https://url/api/v2/attachments/IDHERE.json
[id] => ID#
[file_name] => name of file here
Get the values like this:
$Attachments = $item['attachments'];
$AttachmentsUrl = $Attachments[0]['url'];
$Attachmentsid = $Attachments[0]['id'];
$AttachmentsFileName = $Attachments[0]['file_name'];

Categories