Can i use ip address as Object array key? [duplicate] - php

This question already has answers here:
PHP json_decode notation issue
(2 answers)
Closed 8 years ago.
I m having json file as
{"103.186.190.216":1398640126}
i read the file and json decode it returs as
stdClass Object ( [203.196.190.226] => 1398640126 )
How can i print simple as
echo $json_arr->203.196.190.226;
Can have ip as key ?
i know can retrieve it by
foreach ($json_arr as $key => $val) {
echo $val;
}
Or how can print it in for loop rather than foreach
And also how can push values inside these type of object array
Coz normal php array_push(); Didn't work.
Or on the lighter note what is best way to create these type of array which we want give the key And value dynamically ?

Use this syntax:
echo $json_arr->{'203.196.190.226'};

Related

I want to read all data from Json array in Php codeigniter 4 [duplicate]

This question already has answers here:
How to extract and access data from JSON with PHP?
(1 answer)
How can I access an array/object?
(6 answers)
Closed 3 months ago.
I have generated html table data to Json array using jquery, it worked for me and I got the array below, I need php code to iterate each value of this array in php codeigniter 4 controller.
array(1){
[
0
]=>string(966)"[{"ID":"\n2","Name":"\nCP","Price":"\n350.20"},{"ID":"\n3","Name":"\nLFT","Price":"\n700.10"},{"ID":"\n4","Name":"\nRFT","Price":"\n200"},{"ID":"\n5","Name":"\nurinetest","Price":"\n1000"}]"
}
It's a php array that contains a json array, if you are sure you will only have one element in the array, you can do so:
$array = json_decode($var[0]); // $var holds the data you mentioned above
foreach ($array as $item) {
//
}
otherwise:
foreach ($var as $array) { // $var holds the data you mentioned above
$array = json_decode($array);
foreach ($array as $item) {
//
}
}

How can i Iterate through data returned in Ajax success? [duplicate]

This question already has answers here:
How to loop through PHP object with dynamic keys [duplicate]
(16 answers)
Closed 5 years ago.
How can I iterate to get id value? This is my array:
[{"email_id":"gayatri.dsf#detedu.org","Id":"216"}]
tried
<?php
foreach($faculty as $value)
{
echo $value['Id'];
}
?>
Gives an error
Use of undefined constant Id - assumed Id
This is a json which is basically a string, to be more precise the given json contains a list (currently 1 element):
[{"email_id":"gayatri.dsf#detedu.org","Id":"216"}]
You need to convert it to an array first:
$jsonValues = json_decode($json, true); //here you will have an array of users (1 now)
foreach($jsonValues as $faculty) //for each user do something
{
echo $faculty['Id'];
}
This is JSON format. First you have to decode it. Example:
$a = '[{"email_id":"gayatri.dsf#detedu.org","Id":"216"}]';
$dec = json_decode($a);
echo $dec[0]->Id;
Result: 216
Decoded you have an array, containing exactly one object. You have to access the object properties with -> then.
With JSON [] brackets means an array, while {} brackets mean objects. Learn more: https://en.wikipedia.org/wiki/JSON

Access to a array wont work [duplicate]

This question already has answers here:
How can I access an array/object?
(6 answers)
Closed 5 years ago.
actually i dont understand why i get no access to the values of a array.
if i print a array i get this result
print_r($e)
Array
(
[FIELDNAME1] => MYTEXT
[FIELDNAME2] => MYTEXT2
)
now i want to access the field directly with
echo"Element 0".$e[0]."<br>";
echo"Element 1".$e[1]."<br>";
Under $e[0] and $e[1] I get no response (empty/nothing).
Why I can't get access to $e[0] etc.?
Is there any way to get access with 0/1/2... for this array, background is that i dont know the names of the elements, so i have to access with 1 and 2.
Because your array is associative. You'd access values by their associated key:
echo"Element 0".$e['FIELDNAME1']."<br>";
echo"Element 1".$e['FIELDNAME2']."<br>";
That's because you have an associative array here, where the array keys are FIELDNAME1 and FIELDNAME2 and not 0, 1 like you stated.
This will work:
echo"Element 0".$e['FIELDNAME1']."<br>";
echo"Element 1".$e['FIELDNAME2']."<br>";
Or if you want to loop through your array, try this:
foreach ($e as $k => $v) {
echo "Element $k : ".$v."<br>";
}

get json date by array in php [duplicate]

This question already has answers here:
How to loop through PHP object with dynamic keys [duplicate]
(16 answers)
Closed 6 years ago.
just sample data of json
{
"title":"Wedding Hair Photo Montage",
"app_id":"com.blue.weddinghairphotomontage", "developer":"Blue Photo Montage",
"developer_id":"Blue Photo Montage",
"image":"https://sampake.net/uda.jpg",
"rating_width":"width: 66.66666507720947%;",
"time":1469293200,"Version":"1.2"
}
my php code
<?php foreach( $recent_download as $key => $recent ) {
get_template( 'templates/app-item', $recent );
}?>
i needed Sort data by time
Looks like something similar was asked before: Sort JSON object in PHP by a key value
In case you are having trouble loading the JSON String into memory, you can use the following:
$jsonArray = json_decode($jsonString,true);

How to make json objects within array in php [duplicate]

This question already has answers here:
How to create an array for JSON using PHP?
(8 answers)
Closed 8 years ago.
I want to make json as followsin php:
{["key1":"value1"],["key2":"value2"]}
Can any one help?
See the PHP manual for json_encode() and json_decode():
http://php.net/manual/en/function.json-encode.php
http://php.net/manual/en/function.json-decode.php
You can do it like this: set it to true, if you want an associative array.
$array = json_decode('[{"var1":"9","var2":"16","var3":"16"},{"var1":"8","var2":"15","var3":"15"}]',true);
if you want to iterate:
foreach($array as $item) { //foreach element in $arr
$uses = $item['var1']; //etc
}

Categories