Php 简化阵列并另存为CSV
我有一个非常复杂的数组:Php 简化阵列并另存为CSV,php,arrays,clio-api,Php,Arrays,Clio Api,我有一个非常复杂的数组: stdClass Object ( [matters] => Array ( [0] => stdClass Object ( [id] => 1050370768 [client] => stdClass Object (
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
)
我只是想得到一份回报
排列(
[显示编号]=>00001巴尔特父子
[显示\u编号]=>00002无缝解决方案
)
然后将其保存为CSV
00001,巴尔特父子,
00002,无缝解决方案
任何帮助都会很棒。
我知道必须有一个简单的方法来做到这一点
有人要PHP。这里有点难,但我会努力的。它是CLIO法律软件API的一部分
//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;
}
我将尝试解决问题的两个部分:(1)简化数组以隔离特定元素;(2)将其导出到
.csv
文件
简化阵列
为此,您需要迭代原始对象的所有matters
元素,并以适当的格式将希望导出的值推送到新数组中
$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'
);
}
然后,您将得到一个数组,该数组看起来应该有点像:
Array [
0 => Array [
0 => '00001'
1 => 'Balter and Son'
]
1 => Array [
0 => '00002'
1 => 'Seamless solutions'
]
]
或者,您可以使用array\u map()
获得类似的结果,而不是在数组上循环。如果您不熟悉array\u map()
您可以找到官方文档
导出到CSV
这一部分实际上非常简单,因为PHP有一个专门用于此()的函数
将两者混合在一起
在要导出两次的元素上循环是很乏味的,并且会影响可读性和可维护性。这就是为什么您可能应该在一个循环中将这些示例混合在一起
$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);
为了简单起见,我假设您的目标文件是空的。如果您在开始使用之前不知道如何确保文件是空的,我建议您看看这个问题,它总结得非常好。请展示您的实际php代码。DUDE。我是说伙计。你太棒了。第一部分很棒。给我拿了阵列。导出有一个错误。警告:fopen(/tmp/matternumbers.csv):无法打开流:C:\Sites\project中没有这样的文件或目录。我修复了我的错误。我在一个windows盒子上,所以我必须给它一个最佳路径。
$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 ;)
$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);