我有一个非常复杂的数组:
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
)
我只想返回 Array ( [display_number] => 00001-Balter and Son [display_number] => 00002-Seamless Solutions )
然后将其保存为 CSV 00001,Balter and Son, 00002,Seamless Solutions
任何帮助都是极好的。我知道必须有一个简单的方法来做到这一点。
有人问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;
}