数组变量键/值PHP

时间:2016-07-27 14:03:30

标签: php arrays

我目前正在尝试使用PHP从csv文件中获取一些信息。我使用以下代码并获得以下输出;

function readCSV($csvFile){
    $file_handle = fopen($csvFile, 'r');
    while (!feof($file_handle) ) {
        $line_of_text[] = fgetcsv($file_handle, 1024);
    }
    fclose($file_handle);
    return $line_of_text;
}

$csvFile = '500.csv';
$csv = readCSV($csvFile);
$keys = $csv[0];
$step = $csv[1];
foreach ($step as $k=>$v)
{
    $a = array("$keys[$k]");
    $b = array("$v");
    $c = array_combine($a, $b);
    echo '<pre>';
    print_r($c);
    echo '</pre>';
}

我得到各个数组中的输出;

Array
(
    [first_name] => bob
)
Array
(
    [last_name] => smith
)
Array
(
    [company_name] => bobs logs
)

我希望输出在一个单独的数组中,显示如下;

Array
(
    [first_name] => bob
    [last_name] => smith
    [company_name] => bobs logs
)

如果有人能够指出我出错的地方,那就会受到关注!

3 个答案:

答案 0 :(得分:3)

array_combine功能

  

如果每个数组的元素数量不相等,则返回组合的数组 FALSE

您的代码在每次循环迭代时创建一个新数组。
要获得单个数组,请更改您的循环代码,如下所示:

...
$c = [];
foreach ($step as $k => $v)
{
    $c[$keys[$k]] = $v;    
}
echo '<pre>';
print_r($c);
echo '</pre>';

答案 1 :(得分:2)

变化:

$a = array("$keys[$k]");
$b = array("$v");
$c = array_combine($a, $b);

要:

$c[$keys[$k]] = $v;

在循环之后执行此操作:

echo '<pre>';
print_r($c);
echo '</pre>';

答案 2 :(得分:1)

<!---
Building a spreadsheet that looks like:

+-----+-----+-----+
| FOO | BAR | BAZ |
+-----+-----+-----+
| 101 | 102 | 103 |
+-----+-----+-----+
| 201 | 202 | 203 |
+-----+-----+-----+

--->


<!--- Create a new spreadsheet. --->
<cfset objSpreadsheet = SpreadsheetNew()>

<!--- Create and format the header row. --->
<cfset SpreadsheetAddRow( objSpreadsheet, "FOO,BAR,BAZ" )>
<cfset SpreadsheetFormatRow( objSpreadsheet, {bold=TRUE, alignment="center"}, 1 )>

<!--- Populate the spreadsheet. --->
<!--- In a real situation, this would be looped programmatically; it is done cell-by-cell here for readability. --->
<cfset SpreadsheetSetCellValue( objSpreadsheet, 101, 2, 1, "NUMERIC" ) >
<cfset SpreadsheetSetCellValue( objSpreadsheet, 102, 2, 2, "NUMERIC" ) >
<cfset SpreadsheetSetCellValue( objSpreadsheet, 103, 2, 3, "NUMERIC" ) >
<cfset SpreadsheetSetCellValue( objSpreadsheet, 201, 3, 1, "NUMERIC" ) >
<cfset SpreadsheetSetCellValue( objSpreadsheet, 202, 3, 2, "NUMERIC" ) >
<cfset SpreadsheetSetCellValue( objSpreadsheet, 203, 3, 3, "NUMERIC" ) >

<cfheader name="Content-Disposition" value="attachment; filename=MySpreadsheet.xls"> 
<cfcontent type="application/vnd.ms-excel" variable="#SpreadsheetReadBinary( objSpreadsheet )#"> 

我做了哪些改变? 我把那些echo和print_r带到了foreach外面并添加了

<?php
function readCSV($csvFile)
{
    $file_handle = fopen($csvFile, 'r');
    while (!feof($file_handle)) {
        $line_of_text[] = fgetcsv($file_handle, 1024);
    }
    fclose($file_handle);
    return $line_of_text;
}

$csvFile = '500.csv';
$csv     = readCSV($csvFile);
$keys    = $csv[0];
$step    = $csv[1];
$output = array();
foreach ($step as $k => $v) {
    $a = array("$keys[$k]");
    $b = array("$v");
    $c = array_combine($a, $b);
    $output = array_merge($output, $c);
}
echo '<pre>';
print_r($output);
echo '</pre>';

将每个新数组合并为$ output数组中的新元素

然后最后我们打印$ output数组。

这应该按照你的意愿工作,无论如何你将来需要改变一些东西你可以检查array_merge函数here