array(1) {
["value"] => array(1000) {
[0]=> array(9) {
["PartitionKey"]=> string(11)"AWT-GPI.com"
["RowKey"]=> string(36) "0024a6ac-6cf1-454a-91b2-15bfec3a3d86"
["Timestamp"]=> string(28) "2016-09-09T20:16:26.8674483Z"
["Email"]=> string(20) "ginyoung30#gmail.com"
["First_Name"]=> string(8) "Jennifer"
["Hash"]=> string(32) "d656d0c21b8f3c14fe03232bb68d1b53"
["IP_1"]=> string(0) ""
["Last_Name"]=> string(5) "Young"
["Username"]=> string(9) "flacobell"
}
[1]=> array(9) {
["PartitionKey"]=> string(11) "AWT-GPI.com"
["RowKey"]=> string(36) "002c00c4-e064-43e8-9dd8-319c8d6663bd"
["Timestamp"]=> string(28) "2016-09-09T20:19:54.5500874Z"
["Email"]=> string(22) "Glenn#flavorleague.com"
["First_Name"]=> string(1) "G"
["Hash"]=> string(32) "1444a7b2c86506158013d1175137eede"
["IP_1"]=> string(0) "" ["Last_Name"]=> string(6) "Wilson"
["Username"]=> string(13) "misterspeed76"
}
}
}
That is the array using this code
$count = count($null_check);
for ($i = 0; $data < $count; $i++) {
foreach ($null_check['value'][$i] as $key => $data) {
$parsed_key = str_replace('_', ' ', $key);
echo $parsed_key.': '.$data.'<br>';
}
echo '<br><br>';
}
I am able to get this output
PartitionKey: AWT-GPI.com
RowKey: 0024a6ac-6cf1-454a-91b2-15bfec3a3d86
Timestamp: 2016-09-09T20:16:26.8674483Z
Email: ginyoung30#gmail.com
First Name: Jennifer
Hash: d656d0c21b8f3c14fe03232bb68d1b53
IP 1:
Last Name: Young
Username: flacobell
PartitionKey: AWT-GPI.com
RowKey: 002c00c4-e064-43e8-9dd8-319c8d6663bd
Timestamp: 2016-09-09T20:19:54.5500874Z
Email: Glenn#flavorleague.com
First Name: G
Hash: 1444a7b2c86506158013d1175137eede
IP 1:
Last Name: Wilson
Username: misterspeed76
Now I want to unset RowKey and Timestamp, however when I do in the foreach statement
unset($null_check['RowKey'];
It doesn't work, I create a separate for each outside or inside, doesn't work, I use the value assigned in the foreach doesn't work. Literally nothing works. This is only a part I have about 30 more just like this. All same format, I just want to remove the RowKey and Timestamp Key, how would I do this?
Use this code
$count = count($null_check);
for ($i = 0; $i <= $count; $i++) {
foreach ($null_check['value'][$i] as $key => $data) {
$parsed_key = str_replace('_', ' ', $key);
echo $parsed_key.': '.$data.'<br>';
if(in_array($key,array('RowKey','Timestamp'))){
unset($null_check['value'][$i][$key]);
}
}
echo '<br><br>';
}
echo '<pre>';
print_r($null_check);
echo '</pre>';
You can use unset() or array_diff_key(), here's how: (just change the \n's to <br>'s)
(Demo)
Input:
$array=[
"value"=>[
["PartitionKey"=>"AWT-GPI.com",
"RowKey"=>"0024a6ac-6cf1-454a-91b2-15bfec3a3d86",
"Timestamp"=>"2016-09-09T20:16:26.8674483Z",
"Email"=>"ginyoung30#gmail.com",
"First_Name"=>"Jennifer",
"Hash"=>"d656d0c21b8f3c14fe03232bb68d1b53",
"IP_1"=>"",
"Last_Name"=>"Young",
"Username"=>"flacobell"
],
["PartitionKey"=>"AWT-GPI.com",
"RowKey"=>"002c00c4-e064-43e8-9dd8-319c8d6663bd",
"Timestamp"=>"2016-09-09T20:19:54.5500874Z",
"Email"=>"Glenn#flavorleague.com",
"First_Name"=>"G",
"Hash"=>"1444a7b2c86506158013d1175137eede",
"IP_1"=>"",
"Last_Name"=>"Wilson",
"Username"=>"misterspeed76"
]
]
];
Method #1:
foreach($array['value'] as $subarray){
foreach(array_diff_key($subarray,['RowKey'=>'','Timestamp'=>'']) as $k=>$v){ // does not modify $array
echo str_replace('_',' ',$k)," : $v\n";
}
echo "\n";
}
Method #2:
echo "\n---\n";
foreach($array['value'] as $subarray){
unset($subarray['RowKey'],$subarray['Timestamp']); // does not modify $array
foreach($subarray as $k=>$v){
echo str_replace('_',' ',$k)," : $v\n";
}
echo "\n";
}
Output:
PartitionKey : AWT-GPI.com
Email : ginyoung30#gmail.com
First Name : Jennifer
Hash : d656d0c21b8f3c14fe03232bb68d1b53
IP 1 :
Last Name : Young
Username : flacobell
PartitionKey : AWT-GPI.com
Email : Glenn#flavorleague.com
First Name : G
Hash : 1444a7b2c86506158013d1175137eede
IP 1 :
Last Name : Wilson
Username : misterspeed76
---
PartitionKey : AWT-GPI.com
Email : ginyoung30#gmail.com
First Name : Jennifer
Hash : d656d0c21b8f3c14fe03232bb68d1b53
IP 1 :
Last Name : Young
Username : flacobell
PartitionKey : AWT-GPI.com
Email : Glenn#flavorleague.com
First Name : G
Hash : 1444a7b2c86506158013d1175137eede
IP 1 :
Last Name : Wilson
Username : misterspeed76
Related
I have a JSON file I want to fill in a table, but can't quite figure out how to retrieve the data inside and loop it through an array to get in a table I made.
This is my JSON file: test.JSON:
{
"data":{
"Chair":{
"id":24,"key":"Chair","name":"Chair","title":"oak home made"
},
"Table":{
"id":37,"key":"Table","name":"Table","title":"round white table"
},
"Closet":{
"id":18,"key":"Closet","name":"Closet","title":"big and red"
},
"Sofa":{
"id":110,"key":"Sofa","name":"Sofa","title":"room for five persons"
}
},
"type":"furniture","version":"1.1.0"
}
Then with PHP I used this: test.PHP:
$url = 'test.json';
$result=file_get_contents($url);
$decoded=json_decode($result, true);
var_dump($decoded);
This is what I get:
array(3) { ["data"]=> array(4)
{
["Chair"]=> array(4)
{ ["id"]=> int(24) ["key"]=> string(5) "Chair" ["name"]=> string(5) "Chair" ["title"]=> string(13) "oak home made" }
["Table"]=> array(4)
{ ["id"]=> int(37) ["key"]=> string(5) "Table" ["name"]=> string(5) "Table" ["title"]=> string(17) "round white table" }
["Closet"]=> array(4)
{ ["id"]=> int(18) ["key"]=> string(6) "Closet" ["name"]=> string(6) "Closet" ["title"]=> string(11) "big and red" }
["Sofa"]=> array(4)
{ ["id"]=> int(110) ["key"]=> string(4) "Sofa" ["name"]=> string(4) "Sofa" ["title"]=> string(21) "room for five persons" }
}
["type"]=> string(9) "furniture" ["version"]=> string(5) "1.1.0"
}
Then I am trying to retrieve "Chair", "Table", "Closet" and "Sofa", but I don't know how to do it. When I try this:
foreach ($decoded as $key => $value) {
echo "key: ".$key;
echo "</br></br>";
echo "value: ".$value;
echo "</br></br>";
return;
}
I get:
key: data
value: Array
Can someone help me get "Chair", "Table", "Closet" and "Sofa", including each of thoses' id, key, name and title?
I have received guides, but they won't help me because I feel like this type of "array" is different then the links for guides I receive.
Thanks!
You are looping through the array on the top most level, while you want to be a level lower than that to be able to loop through all the elements of data, like this:
foreach ($summonerDecoded2['data'] as $key => $value)
{
echo "key: ".$key."</br>";
echo "value id: ".$value['id']."</br>";
echo "value key: ".$value['key']."</br>";
echo "value name: ".$value['name']."</br>";
echo "value title: ".$value['title']."</br>";
}
You can just do this:
$url = 'test.json';
$result=file_get_contents($url);
$decoded = json_decode($result, true);
foreach ( $decoded["data"] as $key => $value) {
echo $value[ "key" ];
echo "<br />";
//You can access data by:
/*
echo $value[ "id" ];
echo $value[ "key" ];
echo $value[ "name" ];
echo $value[ "title" ];
*/
}
This will result to:
Chair
Table
Closet
Sofa
array(1) {
["value"] => array(1000) {
[0]=> array(9) {
["PartitionKey"]=> string(11)"AWT-GPI.com"
["RowKey"]=> string(36) "0024a6ac-6cf1-454a-91b2-15bfec3a3d86"
["Timestamp"]=> string(28) "2016-09-09T20:16:26.8674483Z"
["Email"]=> string(20) "ginyoung30#gmail.com"
["First_Name"]=> string(8) "Jennifer"
["Hash"]=> string(32) "d656d0c21b8f3c14fe03232bb68d1b53"
["IP_1"]=> string(0) ""
["Last_Name"]=> string(5) "Young"
["Username"]=> string(9) "flacobell"
}
[1]=> array(9) {
["PartitionKey"]=> string(11) "AWT-GPI.com"
["RowKey"]=> string(36) "002c00c4-e064-43e8-9dd8-319c8d6663bd"
["Timestamp"]=> string(28) "2016-09-09T20:19:54.5500874Z"
["Email"]=> string(22) "Glenn#flavorleague.com"
["First_Name"]=> string(1) "G"
["Hash"]=> string(32) "1444a7b2c86506158013d1175137eede"
["IP_1"]=> string(0) "" ["Last_Name"]=> string(6) "Wilson"
["Username"]=> string(13) "misterspeed76"
}
}
}
This is what I get when I call my API, now this is just one sample. If you see there is [0] which indicates entry 1 and than [1] which indicates entry two. There are more but I have shortened it. Right now I can do this
foreach ($null_check['value'] as $key => $data) {
// Get rid of '_' in Website Names
$new = str_replace('_', ' ', $data['PartitionKey']);
echo 'Website: '.$new.'<br>';
echo 'Email: '.$data['Email'].'<br>';
}
Which allows me to get ALL of the Email and such
But what I want is to be able to take [0] and format that so doing a loop. It has something to do with i++ but I don't really understand it.
Here is what I really want:
PartitionKey: AWT-GPI.com
Email: ginyoung30#gmail.com
First Name: Jennifer
Last Name: Young
Hash: d656d0c21b8f3c14fe03232bb68d1b53
As you can see I've taken out some of the things and rearranged it. I was wondering if this is possible by using foreach. However, each entry might be different, so [1] might not have 'email' or 'first_name'
Thank you in advanced.
Update:
After some coding I got until here
$count = count($null_check);
for ($i = 0; $data < $count; $i++) {
foreach ($null_check['value'][$i] as $key => $data) {
$parsed_key = str_replace('_', ' ', $key);
echo $parsed_key.': '.$data.'<br>';
}
echo '<br><br>';
}
This allows me to just echo all the details but I have no idea which data is which and how to organize it.
Output:
PartitionKey: AWT-GPI.com
RowKey: 0024a6ac-6cf1-454a-91b2-15bfec3a3d86
Timestamp: 2016-09-09T20:16:26.8674483Z
Email: ginyoung30#gmail.com
First Name: Jennifer
Hash: d656d0c21b8f3c14fe03232bb68d1b53
IP 1:
Last Name: Young
Username: flacobell
PartitionKey: AWT-GPI.com
RowKey: 002c00c4-e064-43e8-9dd8-319c8d6663bd
Timestamp: 2016-09-09T20:19:54.5500874Z
Email: Glenn#flavorleague.com
First Name: G
Hash: 1444a7b2c86506158013d1175137eede
IP 1:
Last Name: Wilson
Username: misterspeed76
See if this helps:
foreach($jsonArray as $jsonObj)
{
foreach($jsonObj as $key => $value)
{
if ($key === "PartitionKey" ||
$key === "Email" ||
$key === "First_Name" ||
$key === "Last_Name" ||
$key === "Hash") {
echo $key . ": " . $value . "<br>";
}
}
}
Assuming you have json_decode($json) of JSON array of objects.
foreach ($null_check['value'] as $key => $data) {
if(isset($data['PartitionKey'])){
$parsed_website = str_replace('_', ' ', $data['PartitionKey']);
echo 'Website: '.$parsed_website.'<br>';
}
if(isset($data['Username'])){
echo 'Username: '.$data['Username'].'<br>';
}
if(isset($data['Email'])){
echo 'Email: '.$data['Email'].'<br>';
}
if(isset($data['Hash'])){
echo 'Hash: '.$data['Hash'].'<br>';
}
if(isset($data['Salt'])){
echo 'Salt: '.$data['Salt'].'<br>';
}
if(isset($data['First_name'])){
echo 'First Name: '.$data['First_Name'].'<br>';
}
if(isset($data['Last_Name'])){
echo 'Last Name: '.$data['Last_Name'].'<br>';
}
if(isset($data['IP_1'])){
$null_ip = array('0000-00-00', null);
if(!in_array($data['IP_1'], $null_ip)) {
echo 'Registered IP: '.$data['IP_1'].'<br>';
}
}
echo '<br><br>';
}
I just did a isset to chcek if there was a key. This will go through all the array. Thank you for helping but this worked for me.
I am trying to retrieve my city visitor from my website using codeigniter
Here is code to show city
$city = $this->geolocation->get_city();
Here the output from geolocation is like
array(11) { ["statusCode"]=>string(2) "OK" ["statusMessage"]=> string(0) "" ["ipAddress"]=> string(12) "202.60.21.15" ["countryCode"]=> string(2) "ID" ["countryName"]=> string(9) "Indonesia" ["regionName"]=> string(10) "Jawa Timur" ["cityName"]=> string(11) "Pacarkeling" ["zipCode"]=> string(5) "60132" ["latitude"]=> string(6) "-7.258" ["longitude"]=> string(7) "112.758" ["timeZone"]=> string(6) "+07:00" } OK202.67.41.25IDIndonesiaJawa
I am using
$cities = json_decode($city, true);
foreach ($cities as $c) {
echo $c;
}
and the output is like this
OK202.67.01.25IDIndonesiaJawa TimurPacarkeling60132-7.258112.758+07:00
How can I get each data from the output? I want to print each data from Geolocation library in Codeingiter like
Your Visitor Country : Indonesia
Your IP Visitor : 202.67.01.25
Your City Name : Pacarkeling
Thank you very much :)
Are you asking for the keys of the array? If so, you can do something like this:
$city = $this->geolocation->get_city();
$city_info = json_decode($city, true);
foreach ($city_info as $key=>$value) {
echo 'Your ' . $key . ' : ' . $value . '<br>';
}
I have a quiz DB which has particular arrangement as shown below:
"question" : "What are the two binary numbers?",
"answers" : {
"ch1" : "1 and 0",
"ch2" : "1 and 2",
"ch3" : "1 to 9"
},
"rightanswer" : "ch1" }
There are n number of such entries in the DB quiz. Now I will have loop through the entire DB and print each value.
how can this can done in for loop; I'm just looking like question[i], answer.ch1[i], answer.ch2[i] .... how to retrieve?
var-dump Results:
array(4) {
["_id"]=> object(MongoId)#7 (1) { ["$id"]=> string(24)"56bb13aef9f36fe751eecfe4" }
["question"]=> string(32) What are the two binary numbers?"
["answers"]=> array(3) {
["ch1"]=> string(7) "1 and 0"
["ch2"]=> string(7) "1 and 2"
["ch3"]=> string(6) "1 to 9"
}
["rightanswer"]=> string(3) "ch1"
}
array(4) {
["_id"]=> object(MongoId)#8 (1) { ["$id"]=> string(24) "56bb1714f9f36fe751eecfe5" }
["question"]=> string(51) "It is a standard specifying a power saving feature?"
["answers"]=> array(3) { .....
I assume you will connect to collection, and in code collectionName is the name of table.
$connection = new MongoClient();
$collection = $connection->database->collectionName;
$cursor = $collection->find();
foreach ( $cursor as $id => $value )
{
echo "$id: ";
var_dump( $value );
//echo $value->question_id;
//echo $value->answers->ch1;
}
You can replace the code and print field name inside the loop the way you want.
Thanks
Amit
Since you managed to print $obj["question"] with expected result, iteration over answers must be:
foreach($obj["answers"] as $key=>$answer) {
echo "$key: $answer"; //or whatever format you need
}
Completely new to JSON and am tasked with filtering/sorting a remote JSON using php and embedding formatted results into a CMS.
The data structure looks like this:
"Categories":[
{
"Name":"Americas",
"ID":"12345",
"Countries":[
{
"Name":"Argentina",
"Partners":[
{
"Country":"Argentina",
"ID":"4321",
"LogoUrl":"logo1.jpg",
"Title":"Company A",
"AddressBlock":"123 Main Street",
"Phone":"444-555-1212",
"TollFree":"",
"Email":"info#CompanyA.com",
"Url":"http://www.CompanyA.com/",
"IsVisible":true,
"IsDistributor":false
}
]
},
{
"Name":"Brazil",
"Partners":[
{
"Country":"Brazil",
"ID":"5432",
"LogoUrl":"logo2.jpg",
"Title":"Company B",
"AddressBlock":"54 Center Street",
"Phone":"234-567-3600",
"TollFree":"",
"Email":"info#CompanyB.com",
"Url":"http://www.CompanyB.com",
"IsVisible":true,
"IsDistributor":false
},
"Name":"Canada",
"Partners":[
{
"Country":"Canada",
"ID":"Company C",
"LogoUrl":"logo3.Company C",
"AddressBlock":"1 Mll Road Floor 27\r\nCanton, ON",
"Phone":"555-66-7777",
"TollFree":"",
"Email":"info#CompanyC.com",
"Url":"http://www.CompanyC.com",
"IsVisible":true,
"IsDistributor":false
},
]
}
]
}
]
Ideally I would like to store the key/value pairs in an array and then print them out as a list sorted alphabetically. Each Country could have multiple entries and those set to "IsVisible:false" would need to be hidden.
I did some searching here and I could get to the data source but the array is not
'exploded' or looped through its dimensions by php and get this returned:
Categories:Array
using this code:
$string = file_get_contents("https://myURL.securekey");
foreach ($json_a as $key => $value)
{
foreach($value as $v)
{
echo $v." ";
}
}
Any help would be GREATLY appreciated.
Thanks!
Edited: So by using the below:
$string = file_get_contents('https://secure.json');
$json = json_decode($string, true);
foreach($json as $fieldIndex => $fields) {
foreach($fields as $valueIndex => $envelope) {
foreach($envelope as $valueEntry) {
foreach($valueEntry as $key => $value) {
if ($key == "Name") {
echo $value;
}
printf("%d - %d - %s: '%s'\n", $key, $value);
$build[$valueIndex][$key]=$value;
}
}
}
}
var_dump($build);
I get the following output:
0 - 0 - 0: 'Array' 0 - 0 - 1: 'Array' 0 - 0 - 2: 'Array' 0 - 0 - 3: 'Array' 0 - 0 - 4: 'Array' 0 - 0 - 5: 'Array' array(1) { [0]=> array(6) { [0]=> array(2) { ["Name"]=> string(9) "Argentina" ["Partners"]=> array(1) { [0]=> array(11) { ["Country"]=> string(9) "Argentina" ["ID"]=> string(36) "4d93" ["LogoUrl"]=> string(52) "http://www.aaa.com/images/partners/aaa.jpg" ["Title"]=> string(14) "Company S.A." ["AddressBlock"]=> string(118) "Main Street - Dock 8 12435- Anytown USA" ["Phone"]=> string(17) "(444) 123-4567" ["TollFree"]=> string(0) "" ["Email"]=> string(28) "info#aaa.com" ["Url"]=> string(35) "http://www.aaaa.com/" ["IsVisible"]=> bool(true) ["IsDistributor"]=> bool(false) } } }
But still unsure as to how to reference the specific name/values to echo them where needed. I assume maybe something like:
if ($key == "Title") {
echo "Title: " . $key . "<br />";
} esleif ($key == "Country") {
echo $value;
}
but am stuck on a) where to place it in the loop so it actually grabs the values at the right level and b) the syntax (which must be off because I can still only see anything with a var_dump at the end rather than echo out the values from within the loop.