Simplify an array and save as CSV [closed] - php

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have the really complex array:
stdClass Object
(
[matters] => Array
(
[0] => stdClass Object
(
[id] => 1050370768
[client] => stdClass Object
(
[id] => 939940280
[url] => /api/v2/contacts/939940280
[name] => Balter and Son
)
[display_number] => 00001-Balter and Son
[description] => Sueing for pain of having to program
[status] => Open
[open_date] => 2017-07-26
[close_date] =>
[pending_date] =>
[location] =>
[client_reference] => 34241
[responsible_attorney] => stdClass Object
(
[id] => 345011996
[url] => /api/v2/users/345011996
[name] => jon balter
[email] => jbalter#seamlesssolutions.com
)
[originating_attorney] =>
[practice_area] =>
[billable] => 1
[maildrop_address] => ecd6d7b60+matter1050370768#maildrop.clio.com
[created_at] => 2017-07-26T20:46:14+00:00
[updated_at] => 2017-07-26T20:46:14+00:00
[custom_field_values] => Array
(
)
[billing_method] => hourly
[group_id] => 1654280
[permission] => stdClass Object
(
[id] => 1654280
[url] => /api/v2/groups/1654280
[name] => Firm
)
[activity_rates] => Array
(
)
)
[1] => stdClass Object
(
[id] => 1050770508
[client] => stdClass Object
(
[id] => 940983330
[url] => /api/v2/contacts/940983330
[name] => Seamless Solutions
)
[display_number] => 00002-Seamless Solutions
[description] => This is a matter of life and death
[status] => Open
[open_date] => 2017-08-09
[close_date] =>
[pending_date] =>
[location] =>
[client_reference] =>
[responsible_attorney] =>
[originating_attorney] =>
[practice_area] =>
[billable] => 1
[maildrop_address] => ecd6d7b60+matter1050770508#maildrop.clio.com
[created_at] => 2017-08-09T21:37:28+00:00
[updated_at] => 2017-08-09T21:37:28+00:00
[custom_field_values] => Array
(
)
[billing_method] => hourly
[group_id] => 1654280
[permission] => stdClass Object
(
[id] => 1654280
[url] => /api/v2/groups/1654280
[name] => Firm
)
[activity_rates] => Array
(
)
)
)
[records] => 2
[limit] => 200
[next_offset] => 1050770508
[order_dir] => asc
[total_records] => 2
[published_at] => 2017-08-09T21:37:38+00:00
)
I just want to get a return of
Array (
[display_number] => 00001-Balter and Son
[display_number] => 00002-Seamless Solutions
)
Then take this and save this as a CSV
00001,Balter and Son,
00002,Seamless Solutions
Any help would be awesome.
I know there has to be an easy way to do this.
Someone asked for the PHP. Little hard to put here but I will try. It is part of an API for CLIO legal software.
//Get Matters
$matterarry = matter_numbers ($token);
//get array to just matter numbers
$matternumbers = array(); // initialize the array to be used for the export
foreach($matterarry->matters as $key => $matter) { // loop through all the top level element
// isolate the display number '00001' from '00001-Balter and Son'
$displayNumber = explode('-', $matter->display_number);
$displayNumber = $displayNumber[0];
// push the element the export array using the display_number as the key
$matternumbers[$key] = array(
$displayNumber, // '00001'
$matter->client->name // 'Balter and Son'
);
}
Print_r ($matternumbers);
//export to CSV
$f = fopen('/tmp/matternumbers.csv', 'a'); // open the destination file handler
fputcsv($f, array('display_number', 'name')); // start by adding the column headers
// this can also be done by using named keys in your array,
// or having the first element be the value of the headers
// I'm appending manually here for the sake of simplicity
foreach($matternumbers as $key => $element) {
fputcsv($f, $element); // append each element to the file
}
fclose($f); // don't forget to close the file ;)
function matter_numbers ( $token ) {
//$header = array('Authorization: bearer '.$token);
//print_r ($header);
$header = 'Authorization: bearer '.$token;
echo $header."\r\n";
$ch = curl_init();
//curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_URL, 'https://app.goclio.com/api/v2/matters');
curl_setopt($ch, CURLOPT_HTTPHEADER, array($header));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$resp = curl_exec($ch);
if( !$resp ) {
die('Error: "' . curl_error( $ch ) . '" - Code: ' . curl_errno( $ch ) );
}
else if ( 200 != curl_getinfo( $ch, CURLINFO_HTTP_CODE ) ) {
echo "Bad Response Code!";
echo "\n";
echo "Response HTTP Status Code : " . curl_getinfo( $ch, CURLINFO_HTTP_CODE );
echo "\n";
echo "Response HTTP Body : " . $resp;
}
//print "curl response is:" . $resp;
$resp = json_decode($resp);
//print_r ($resp);
curl_close($ch);
return $resp;
}

I'll try addressing the two parts of your question (1) simplifying the array to isolate specific elements and (2) exporting that to a .csv file
Simplifying the array
For this you'll need to iterate over all the matters element of your original object and push whatever value(s) you wish to export in a new array with the appropriate format
$exportArray = array(); // initialize the array to be used for the export
foreach($initialObject->matters as $key => $matter) { // loop through all the top level element
// isolate the display number '00001' from '00001-Balter and Son'
$displayNumber = explode('-', $matter->display_number);
$displayNumber = $displayNumber[0];
// push the element the export array using the display_number as the key
$exportArray[$key] = array(
$displayNumber, // '00001'
$matter->client->name // 'Balter and Son'
);
}
You then end up with an array that should look a little something like that:
Array [
0 => Array [
0 => '00001'
1 => 'Balter and Son'
]
1 => Array [
0 => '00002'
1 => 'Seamless solutions'
]
]
Alternatively, instead of looping over the array you could use array_map() and obtain a similar result. If you're not familiar with array_map() you can find the official doc here
$exportArray = array_map(function($matter) {
// isolate the display number '00001' from '00001-Balter and Son'
$displayNumber = explode('-', $matter->display_number);
$displayNumber = $displayNumber[0];
return array(
$displayNumber,
$matter->client->name
);
}, $initialObject->matters)
Exporting to CSV
This part is actually quite easy as PHP has a function specifically for this (Official Doc)
$f = fopen('/tmp/myFile.csv', 'a') // open the destination file handler
fputcsv($f, array('display_number', 'name')) // start by adding the column headers
// this can also be done by using named keys in your array,
// or having the first element be the value of the headers
// I'm appending manually here for the sake of simplicity
foreach($exportArray as $key => $element) {
fputcsv($f, $element); // append each element to the file
}
fclose($f) // don't forget to close the file ;)
Mixing the two together
Looping over the elements you want to export twice is tedious and will affect readability and maintainability. This is why you should probably mix those examples together in a single loop.
$file = fopen('/tmp/myFile.csv', 'a'); // open the destination file handler
fputcsv($file, array('display_number', 'name')); // add the column headers
foreach($initialObject->matters as $key => $matter) { // loop through all the top level element
// isolate the display number '00001' from '00001-Balter and Son'
$displayNumber = explode('-', $matter->display_number);
$displayNumber = $displayNumber[0];
// Add the information you need directly in the file
fputcsv($file, array($displayNumber, $matter->client->name));
}
fclose($file);
For the sake of simplicity, I have assumed that your destination file is empty. If you don't know how to make sure a file is empty before starting working with, I suggest you look at this question that sums it up very well.

Related

Coinmarketcap API call with PHP and choose data set with slug

I tried to do my first API calls which worked finally with help from this great users here in this community. Thanks again. I want to choose data[1] or the currency with symbol. So i could use a $variable from my CMS. Maybe someone can show me a way how i can change this call to symbol. Here is my API call.
$url = "https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest";
$headers = [
'Accepts: application/json',
'X-CMC_PRO_API_KEY: ___YOUR_API_KEY_HERE___'
];
$request = "{$url}"; // create the request URL
$curl = curl_init(); // Get cURL resource
// Set cURL options
curl_setopt_array($curl, array(
CURLOPT_URL => $request, // set the request URL
CURLOPT_HTTPHEADER => $headers, // set the headers
CURLOPT_RETURNTRANSFER => 1 // ask for raw response instead of bool
));
$response = curl_exec($curl); // Send the request, save the response
$json = json_decode($response);
curl_close($curl); // Close request
$price = $json->data[1]->quote->USD->price; echo $price;
You get the data block IDs with array_column(), then you get the symbol's data block ID with array_search():
$data_ids = array_column($json->data, 'symbol');
$symbol_data_id = array_search('ETH', $data_ids);
$price = $json->data[$symbol_data_id]->quote->USD->price;
Or as an oneliner:
$price = $json->data[array_search('ETH', array_column($json->data, 'symbol'))]->quote->USD->price;
LATER UPDATE: OK, let me elaborate on this. Step by step:
You need a proper URL to acces the API. For this you need the API documentation. Your original question mentioned
$url = "https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest";
while for your comment question you need something like
$url = "https://pro-api.coinmarketcap.com/v1/cryptocurrency/info?symbol=ETH";
A proper URL will give you a JSON response structured according to its purpose. You have this part ironed out, so I'll not insist on this. From your question text:
$headers = [
'Accepts: application/json',
'X-CMC_PRO_API_KEY: 1b58ff56-58b2-4fd0-b184-f9c3dd4ff106',
];
$request = "{$url}"; // create the request URL
$curl = curl_init(); // Get cURL resource
// Set cURL options
curl_setopt_array($curl, [
CURLOPT_URL => $request, // set the request URL
CURLOPT_HTTPHEADER => $headers, // set the headers
CURLOPT_RETURNTRANSFER => 1, // ask for raw response instead of bool
]);
$response = curl_exec($curl); // Send the request, save the response
$json = json_decode($response);
curl_close($curl); // Close request
Then you have to decide how to use the response. For me, the fastest way is to look at its structure (var_dump($json) or print_r($json)). Which will give something like this (the original question):
stdClass Object
(
[status] => stdClass Object
(
[timestamp] => 2021-11-06T18:37:59.447Z
[error_code] => 0
[error_message] =>
[elapsed] => 22
[credit_count] => 1
[notice] =>
[total_count] => 7060
)
[data] => Array
(
[0] => stdClass Object
(
[id] => 1
[name] => Bitcoin
[...]
)
[1] => stdClass Object
(
[id] => 1027
[name] => Ethereum
[symbol] => ETH
[slug] => ethereum
[...]
[quote] => stdClass Object
(
[USD] => stdClass Object
(
[price] => 4445.0743486785
[volume_24h] => 14137477206.072
[volume_change_24h] => -9.6622
[percent_change_1h] => 0.2898806
[percent_change_24h] => -1.29677209
[percent_change_7d] => 3.13286959
[percent_change_30d] => 23.49191199
[percent_change_60d] => 28.79913805
[percent_change_90d] => 48.37310902
[market_cap] => 525560956659.07
[market_cap_dominance] => 19.5198
[fully_diluted_market_cap] => 525560956659.07
[last_updated] => 2021-11-06T18:37:03.000Z
)
)
)
[2] => stdClass Object [...]
or this (the question in the comment):
stdClass Object
(
[status] => stdClass Object
(
[timestamp] => 2021-11-06T18:03:05.201Z
[error_code] => 0
[error_message] =>
[elapsed] => 12
[credit_count] => 1
[notice] =>
)
[data] => stdClass Object
(
[ETH] => stdClass Object
(
[id] => 1027
[name] => Ethereum
[symbol] => ETH
[category] => coin
[description] => Ethereum (ETH) is a cryptocurrency . Users are able to generate ETH through the process of mining. Ethereum has a current supply of 118,233,336.749. The last known price of Ethereum is 4,424.33123326 USD and is down -1.39 over the last 24 hours. It is currently trading on 4537 active market(s) with $14,138,162,060.93 traded over the last 24 hours. More information can be found at https://www.ethereum.org/.
[...]
[platform] =>
[date_added] => 2015-08-07T00:00:00.000Z
[twitter_username] => ethereum
[is_hidden] => 0
)
)
)
So data is a property of the $json object.
In the first case, data is an array and its structure suggests using array functions to retrieve specific data.
In the second case, data and ETH are objects, while description is a property of ETH. Which allows me to get the description using object syntax
$description = $json->data->ETH->description;

Yii2 how to save received JSON data into database

I have created an API which returns me an array of data in json
Array ( [0] => stdClass Object ( [MSN] => 002999001207 [PingDateTime] => 2018-05-04T16:33:27 [PingValue] => 22 ) [1] => stdClass Object ( [MSN] => 002999001195 [PingDateTime] => 2018-05-04T16:34:11 [PingValue] => 21 ) [2] => stdClass Object ( [MSN] => 002999001180 [PingDateTime] => 2018-05-04T14:42:40 [PingValue] => 20 ) [3] => stdClass Object ( [MSN] => 002999001157 [PingDateTime] => 2018-05-04T14:42:52 [PingValue] => 30 ) [4] => stdClass Object ( [MSN] => 002999001142 [PingDateTime] => 2018-05-04T16:37:19 [PingValue] => 13 ) [5] => stdClass Object ( [MSN] => 002999001138 [PingDateTime] => 2018-05-04T16:32:22 [PingValue] => 20 ) [6] => stdClass Object ( [MSN] => 002999001114 [PingDateTime] => 2018-05-04T16:32:52 [PingValue] => 22 )
Now, I am trying to save it in my DB
$curl = curl_init($api_url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 1000);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Key'));
$m->start_date_time = date('Y-m-d h:i:s');
$curl_response = curl_exec($curl);
$json = json_decode($curl_response);
$record = $json->data;
foreach ($record as $item){
if($this->isSaved($item->MSN))
{
return false;
}
else if($this->ogpCreated($item->MSN))
{
$m->end_date_time = date('Y-m-d h:i:s');
$m->meter_msn = $item->MSN;
$m->meter_id = Meters::msnmapToid($m->meter_msn);
$m->sub_div_code = Ogpdetail::msnTosubdiv($item->MSN);
$m->sub_div_name = Ogpdetail::subDivToName($m->sub_div_code);
$m->meter_ping_date_time = str_replace('T', ' ', $item->PingDateTime);
$m->save();
}
}
return $this->redirect(['index']);
In above code, there are two if conditions
isSaved($item->MSN)
$meter = MeterPing::find()->where(['meter_msn' => $msn])->one();
if($meter)
return true;
return false;
From the above function, I am trying to check whether the incoming MSN is already saved or not. If it's already present in the table it will not save that particular MSN but yes save all the other MSN that are not saved previously.
ogpCreated($item->MSN)
$meter = Ogpdetail::find()->where(['meter_serial' => $msn])->one();
if($meter)
return true;
return false;
From the above function, I am trying to check that the incoming MSN is OGP created or not. Again it should not save any MSN which is not OGP created.
Now, when I try to run this Create function it only saves one record at a time.
I think there is some issue in if..... elseif that only allows saving one entry. But I am not sure of that.
Update 1
I have tried to remove the checks and then save the incoming data but still, it only saves one record
How can I save the entire received JSON data into my DB with all checks working?
You need to add your model initialization $m=new YourModel(); inside the foreach loop an within the elseif($this->ogpCreated($item->MSN)), that is why it is saving only one record
foreach ($record as $item){
if($this->isSaved($item->MSN)){
return false;
}
else if($this->ogpCreated($item->MSN)){
$m= new YourModel();
You are using the $m in the curl command too it would be better to post all code incase you run into some logical errors you need to adjust the code accordingly, or change the name of the variable from $m inside the foreach
Apart from this you can use this extension Yii2-Curl for curl commands it will allow you more flexibility to your code in a more readable way

Searching in json array for parameter and show correct information

Is it possible to search inside json array which I pull with curl and if match to show only that data?
The array looks like
Array
(
[status] => success
[data] => Array
(
[out] => Array
(
[0] => Array
(
[address] => test address
[amount] => 11
[type] => 1
)
[1] => Array
(
[address] => test address 1
[is_nonstandard] =>
[amount] => 12
[type] => 1
)
)
)
[code] => 200
[message] =>
)
And this is how I pull the array
function get_curl_content_tx($url) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
$result = curl_exec($curl);
curl_close($curl);
return $result;
}
$url=get_curl_content_tx("http://example.com");
$total =json_decode($url,true);
So since there can be more that 1 arrays in [out] => Array as current situation they are two I want to search and match by [address] and when match to give data.
Here is a way to do it :
foreach($arr['data']['out'] as $d){
if(strpos($d['address'], 'testaddress') !== false){
//do something
var_dump($d);
}
}
You need to replace var_dump by whatever you want to do.
I used strpos for the sake of this example, but you might want to use a custom method that would suits your needs better
Hope this helps.
<?php
$outArray = $total['data']['out'];
foreach($outArray as $item) {
// echo $item['address'];
}
?>
Try this:
if(isset($total['data']['out'])){
foreach($total['data']['out'] as $out){
if(isset($out['address'])){
if($out['address'] == "your adress"){
//Your Stuff;
}
}
}
}

Someone Help Me To Print This Array Using Css Or In Tables

Can someone please help me to print this data using tables or css or bootstrap..?
PHP Code Used:
$url = "http://some-website.com/api/message";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$content = curl_exec($ch);
curl_close($ch);
$json = json_decode($content, true);
$count=count($json);
print_r ($json);
Result:
Array
(
[success] => 1
[result] => Array
(
[0] => Array
(
[id] => 12491055
[device_id] => 18398
[message] => hi there!
[status] => received
[send_at] => 0
[queued_at] => 0
[sent_at] => 0
[delivered_at] => 0
[expires_at] => 0
[canceled_at] => 0
[failed_at] => 0
[received_at] => 1456228673
[error] => N/A
[created_at] => 1456271873
[contact] => Array
(
[id] => 3077686
[name] => charan
[number] => 123456789
)
)
)
)
I'm new in this php field so don't try to make your answers bit difficult one for me to understand or execute.
i want it to be printed like:
Name: some one
Message: Hi
Etc.........
just like online desposal mobile numbers & message's services sites
Thanks In Advance!
Just look at the array you already have, and drill down.
echo "Name: " . $json['result'][0]['contact']['name'];
Or:
echo "Message: " . $json['result'][0]['message'];
use foreach() to print your array.example:
$tab=array('name'=>'Jack','last name'=>'sparrow');
foreach($tab as $key=>$elem)
{
echo "$key : $elem <br>";
}
//the result
//name: jack
//last name : sparrow

Parsing JSON Results with PHP - Yahoo Search API

I am able to retrieve results from yahoo with my API key, using the instructions found on the yahoo developers website. http://developer.yahoo.com/boss/search/boss_api_guide/codeexamples.html#
Code:
if ($_POST['query'])
{
$newline="<br />";
$query = urlencode("'{$_POST['query']}'");
require("OAuth.php");
$cc_key = "key goes here";
$cc_secret = "secret goes here";
$url = "http://yboss.yahooapis.com/ysearch/web";
$args = array();
$args["q"] = "$query";
$args["format"] = "json";
$consumer = new OAuthConsumer($cc_key, $cc_secret);
$request = OAuthRequest::from_consumer_and_token($consumer, NULL,"GET", $url, $args);
$request->sign_request(new OAuthSignatureMethod_HMAC_SHA1(), $consumer, NULL);
$url = sprintf("%s?%s", $url, OAuthUtil::build_http_query($args));
$ch = curl_init();
$headers = array($request->to_header());
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$rsp = curl_exec($ch);
$results = json_decode($rsp);
print_r($results);
}
Using print_r($results) as shown above, I get results, such as the following (extract of first three results shown from searching for "elephant"):
PLEASE NOTE I HAVE CHANGED THE URLS TO "WWW" AS I REQUIRE AT LEAST 10 REPUTATION TO POST MORE THAN 2 LINKS.
stdClass Object ( [bossresponse] => stdClass Object ( [responsecode]
=> 200 [web] => stdClass Object ( [start] => 0 [count] => 50 [totalresults] => 36800000 [results] => Array ( [0] => stdClass Object
( [date] => [clickurl] => WWW [url]
=> WWW [dispurl] => en.wikipedia.org/wiki/Elephant [title] => Elephant - Wikipedia, the
free encyclopedia [abstract] => Elephant trunks have multiple
functions, including breathing, olfaction, ... One elephant has been
observed to graze by kneeling on its front legs, ... ) [1] => stdClass
Object ( [date] => [clickurl] =>
WWW [url] =>
WWW [dispurl] =>
www.defenders.org/elephant/basic-facts [title] => Elephant | Basic
Facts About Elephants | Defenders of Wildlife [abstract] => Elephant.
Basic Facts About Elephants More on Elephant: Threats to Elephants ยป
More on Elephant: Basic Facts . Threats. What Defenders Is Doing to
Help. What You Can ... ) [2] => stdClass Object ( [date] => [clickurl]
=> WWW
[url] =>
WWW
[dispurl] => kids.nationalgeographic.com/.../african-elephant [title]
=> African Elephant Facts and Pictures -- National Geographic Kids [abstract] => Kids' feature about elephants, with photographs, video,
audio, fun facts, an e-mail postcard, and links to other animals. )
[3] => stdClass Object ( [date] => [clickurl] =>
WWW [url]
=> WWW [dispurl] => elephant.elehost.com/About_Elephants/about_elephants.htm
[title] => About Elephants [abstract] => All about elephants on the
Elephant Information Repository! This page includes a summary of
elephant related facts to get you inducted in to the world of
elephants. )
I have attempted to output the results, in a legible format, as follows:
Code Attempt 1:
foreach ($results->{ 'results' } as $item )
{
echo "<font color ='blue'>{$item->{ 'title' }}</font>".": "."$newline"."$newline".$item->{ 'abstract' }."\n\n";
}
I also tried the following, without success:
Code Attempt 2:
echo $results['results']['url'];
echo $results['results']['title'];
echo $results['results']['abstract'];
Any ideas on what to do?
Thanks.
I've noticed you just copy-pasted the code from the documentation's code examples, but never mind that.
You're accessing the results array the wrong way:
foreach ($results->bossresponse->web->results as $result)
{
//do stuff
echo $result->title.'<br/>';
}
Or, as cptnk suggested:
$results = json_decode($rsp, true);
//force to assoc-array, which will allow array-access
foreach($results['bossresponse']['web']['results'] as $result)
{
//$result is array here, but do the same stuff
echo $result['title'].'<br/>';
}
Or, combine the two
foreach($results->bossresponse->web->results as $result)
{
$result = (array) $result;//casts stdClass to array
printf('%s<br/>', $result['url'], $result['title']);
}

Categories