PHP JSON Array Loop - php

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.

Related

Find duplicate elements in an array

I got an array of coins with many details, that looks partially like that:
array(360) {
["VEN/USDT"]=>
array(15) {
["tierBased"]=>
bool(false)
}
["id"]=>
string(7) "VENUSDT"
["symbol"]=>
string(8) "VEN/USDT"
["base"]=>
string(3) "VEN"
["quote"]=>
string(4) "USDT"
["lot"]=>
float(0.01)
["active"]=>
bool(true)
}
All I need is this part:
["id"]=>
string(7) "VENUSDT"
["symbol"]=>
string(8) "VEN/USDT"
["base"]=>
string(3) "VEN"
["quote"]=>
string(4) "USDT"
if "base" is more often than once in the entire array.
The final code was:
$base_array = array();
foreach ($markets as $key=>$value) {
echo "1. Key = " . $key . "\n";
foreach ($value as $key => $value) {
if ($key == "base") {
echo "Base = " . $value . "\n";
array_push($base_array, $value);
}
}
}
// Duplicates we need only!
$unique = array_unique($base_array);
$duplicates1 = array_diff_assoc($base_array, $unique);
$duplicates = array_unique($duplicates1);
var_dump($duplicates);

Php Json Array Unset Not Working

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

parse, filter and display multi-level json using php

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.

PHP Echo array values loop (no data displayed)

I have an array ... Here is the structure / data:
array(1) {
[0]=> object(SimpleXMLElement)#1 (18)
{
["data_123"]=> object(SimpleXMLElement)#3 (29)
{
["field1"]=> string(7) "123"
["field2"]=> string(2) "10"
["field3"]=> string(19) "2013-03-05 17:00:00"
["field4"]=> string(19) "2013-03-05 18:00:00"
}
["data_234"]=> object(SimpleXMLElement)#4 (29)
{
["field1"]=> string(7) "234"
["field2"]=> string(2) "10"
["field3"]=> string(19) "2013-03-05 17:40:00"
["field4"]=> string(19) "2013-03-05 18:10:00"
}
}
}
I am trying to create a loop to display the data but nothing is showing up:
foreach ($result as $key => $list) {
echo "key.: " . $key . "\n";
echo "field1: " . $list['field1'] . "\n";
echo "field2: " . $list['field2'] . "\n";
}
It's just not returning any data.
I'm guessing that the loop might be wrong for this array structure?
How can I get the data echoed for this array?
$list is an array of objects so you need two loops and appropriate syntax. e.g.:
foreach($list as $objects) {
foreach($objects as $key => $obj) {
echo "key.: " . $key . "\n";
echo $obj->field1 . "\n";
echo $obj->field2 . "\n";
echo $obj->field3 . "\n";
echo $obj->field4 . "\n";
}
}

Traverse non-numerical indexes of an array

Think I'm missing a basic concept. I want to generate html by traversing through a few different arrays of data. They don't use numbers as indexes so numerical looping doesn't work. I cant figure out how to use a foreach() here either. How can I traverse $price and $description when the indexes aren't numbers?
Sample:
$traverser= 0;
while($traverser < $number_of_records)
{
print $traverser . " - " . $price[$traverser] . "<br />";
print $description[$traverser];
$traverser++;
}
Partial Sample of the Array Structure:
object(phpQueryObject)#2799 (13) { ["documentID"]=> string(32) "1d62be942498df890cab4ccb78a007a2" ["document"]=> &object(DOMDocument)#3 (0) { } ["charset"]=> &string(5) "utf-8" ["documentWrapper"]=> &object(DOMDocumentWrapper)#2 (17) { ["document"]=> &object(DOMDocument)#3 (0) { } ["id"]=> string(32) "1d62be942498df890cab4ccb78a007a2" ["contentType"]=> string(9) "text/html" ["xpath"]=> &object(DOMXPath)#4 (0) { } ["uuid"]=> int(0) ["data"]=> array(0) { } ["dataNodes"]=> array(0) { } ["events"]=> array(0) { } ["eventsNodes"]=> array(0) { } ["eventsGlobal"]=> array(0) { } ["frames"]=> array(0) { } ["root"]=> &object(DOMElement)#5 (0) { } ["isDocumentFragment"]=> &bool(true) ["isXML"]=> bool(false) ["isXHTML"]=> bool(false) ["isHTML"]=> bool(true) ["charset"]=> &string(5) "utf-8" } ["xpath"]=> &object(DOMXPath)#4 (0) { } ["elements"]=> array(560) { [0]=> object(DOMElement)#2239 (0) { } [1]=> object(DOMElement)#2240 (0) { } [2]=> object(DOMElement)#2241 (0) { } [3]=> object(DOMElement)#2242 (0) { } [4]=> object(DOMElement)#2243 (0) { } [5]=> object(DOMElement)#2244 (0) { } [6]=> object(DOMElement)#2245 (0) { } [7]=> object(DOMElement)#2246 (0) { } [8]=> object(DOMElement)#2247 (0) { }
Since it looks like you need the array keys as well, since you're referencing multiple different arrays, you want the $a as $k => $v syntax for foreach:
foreach($description as $key => $desc)
{
print $key . " - " . $price[$key] . "<br />";
print $desc;
}
You can take your pic as to how you want to iterate them:
<?php
$ary = array( // demo array
'apple' => 'Apple',
'orange' => 'Orange',
'grape' => 'Grape'
);
// show the structure
var_dump($ary); echo "\r\n";
// use a foreach with the key and value
foreach ($ary as $key => $val)
printf("%s => %s\r\n", $key, $val);
echo "\r\n";
// just get the raw keys
$keys = array_keys($ary);
var_dump($keys); echo "\r\n";
output:
array(3) {
["apple"]=>
string(5) "Apple"
["orange"]=>
string(6) "Orange"
["grape"]=>
string(5) "Grape"
}
apple => Apple
orange => Orange
grape => Grape
array(3) {
[0]=>
string(5) "apple"
[1]=>
string(6) "orange"
[2]=>
string(5) "grape"
}
There's always array_map & array_walk.
I'm not sure I get the question, but it's really as simple as:
<?php
$array = array('foo', 'bar');
foreach ($array as $element) {
echo "{$element}\n";
}
This should output "foo" and "bar".

Categories