У меня есть файл xxx.gz, он содержит несколько текстовых символов ниже:
Array
(
[f_name] => YOKICHI
[l_name] => KOSHIZAWA
[name] => YOKICHI KOSHIZAWA
[street_address] => 7164 fake
[city] => CANTON
[state] => MI
[zip] => 48187
[country] => United States
[phone] => 734-354-6599
[email] => [email protected]
[discount_percent] => 0.00
[discount_amount] => 0
[discount_items] =>
[shipping_region] => USA
[shipping_type] =>
[shipping_priority] =>
[shipping_options] =>
[shipping_total] => 0
)
Мне нужен исходный код PHP для запуска на локальном хосте, может фильтровать и экспортировать информацию ниже:
| f_name | l_name | street_address | city | state | zip | phone | country
например:
| YOKICHI | KOSHIZAWA | 7164 fake | CANTON | MI | 48187 | 734-354-6567 | United States
Я сделал код для вас, не просто скопируйте это, попробуйте учиться с ним 🙂
// $source = file_get_contents($file);
$source = 'Array
(
[f_name] => YOKICHI
[l_name] => KOSHIZAWA
[name] => YOKICHI KOSHIZAWA
[street_address] => 7164 fake
[city] => CANTON
[state] => MI
[zip] => 48187
[country] => United States
[phone] => 734-354-6599
[email] => [email protected]
[discount_percent] => 0.00
[discount_amount] => 0
[discount_items] =>
[shipping_region] => USA
[shipping_type] =>
[shipping_priority] =>
[shipping_options] =>
[shipping_total] => 0
)';
// convert input to array
$arr = explode(PHP_EOL, $source);
// clean top and bottom of the array
array_shift($arr);
array_shift($arr);
array_pop($arr);
// init final array
$final = array();
foreach($arr as $v)
{
$o = array();
$v = preg_match_all('#\[([a-zA-Z0-9 _.-]+)\] => (.*)#', $v, $o);
$key = $o[1][0];
$val = (isset($o[2][0])) ? $o[2][0] : '';
// fill final
$final[$key] = $val;
}
// init the filtered array
$sort = array();
// keys you want to have in
$keys = array('f_name','l_name','street_address','city','state','zip','phone','country');
foreach($final as $k => $v)
{
if(in_array($k, $keys))
{
$sort[$k] = $v;
}
}
var_dump($sort);
Других решений пока нет …