PHP - 进程查找和分组数组项

时间:2017-07-26 13:39:26

标签: php arrays csv

我正在处理电话号码簿列表;现在,当我从我们的电话目录下载.CSV文件时,它只打印给我用户ID,扩展名,名称和其他一些不太相关的角色。

我的问题是我想按办公室对扩展程序/人员进行分组,因为电子表格没有提供任何提示,我想创建一个数组并将其与我的.CSV元素匹配。

办公室扩展程序不是很一致,因此对于Office A,您可能有扩展名:103,215和105.办公室B可能是:150,104。

现在我创建了一个数组,我按照办公室对扩展进行分组,它看起来像这样:

$offices = array(
    'Office' => array (
        '110',
        '107',
        '137',
        '113'
    ),
    // ...
);

对于实际阅读我使用fgetcsv的电子表格而言,它在没有任何类型的过滤器的情况下运行良好,但每当我尝试将变体添加到"过滤器" while循环我遇到了问题。

我尝试使用MySQL,但这不是一个好方法,因为每当需要更新时,其他人都会这样做,而且他们不想使用PHP或MySQL。

这是要显示的列表的代码:

<?php
$file_name = htmlentities($_GET['file']);
// open CSV file
$handle = fopen("./_files/" . $file_name, "r");
$i = 0;
// gets data from csv file
echo "<div class='row'>";
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {

    // stores dates as variable $date
    $date[$i] = $data[0];
    $i++;

    // create vars
    $name = $data[2];
    $email = $data[0];
    $ext = $data[1];

    if ($ext < 400)
    {
?>

        <div class="col-sm-4">
            <ul class="mdc-list mdc-list--dense">
                <li class="mdc-list-item">
                    <span class="mdc-list-item__text">
                        <?php echo $name; ?>
                    </span>
                    <span class="mdc-list-item__end-detail" aria-label="extention">
                          <?php echo $ext; ?>
                    </span>
                </li>
            </ul>
        </div>

<?php
    }
}
echo "</div>";
?>

重申我需要解决的问题是:

我需要将while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)的结果与$offices数组匹配。

例如

IfWorker A's extension == 102),查看创建办公室匹配的$office数组内部,并在该办公室部分下输出他的$name$ext。< / p>

2 个答案:

答案 0 :(得分:2)

每当我遇到与csv或其他文件类似的情况时,我必须在打印最终结果之前进行一些排序,计算等事情。

知道while循环每行解析一行,除了在PHP数组中添加所有数据并用PHP完成我想做的事情之外,没有任何其他方法。

在你的例子中(不确定这是否会对你有帮助),你的代码可能是:

<?php
$file_name = htmlentities($_GET['file']);
// open CSV file
$handle = fopen("./_files/" . $file_name, "r");
$i = 0;
$my_array = array();

while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
   //add your inputs in your multi-dimensional array, do not echo anything yet
   $my_array[$i]['email'] = $data[0];
   $my_array[$i]['name'] = $data[2];
   $my_array[$i]['ext'] = $data[1];
   $i++;
}
//Here, in $my_array you have your whole csv file, in PHP. You can do whatever you want it
var_dump($my_array);

修改

对于$offices数组,您可以使用此代码

foreach ($offices as $key => $office) { 
   foreach($office as $office_key => $office_val) { 
      if ($office_val == $data[1]) { 
         $my_array['department'] = $office_key; 
         break;
      } 
   }
}

希望我帮助过。

答案 1 :(得分:1)

经过多次思考和思考,我得到了@satafaka的帮助,一位朋友能够针对我的具体问题找到解决办法。

@satafaka将我的所有数据输出为数组是正确的;然后对阵列的管理比我想要的要复杂一点。所以我将$offices数组更改为一个更简单的数组,我可以更好地管理它。

它现在看起来像这样:

$offices = array(
    0 => array (
        'Office1','Office1','Office1','Office2','Office2', ...
    1 => array (
        '110', '107', '137', '113', '181', ...
);

输出应如下所示:

array(2) {
    [0] => array(62) {
        [0] => string(15)
        "Office1" [1] => string(15)
        ...

现在解释数组意味着什么:办公室被多次列出,扩展按照办公室的顺序排列:所以你可以看到我列出Office1 3次,然后前三个扩展将来自Office1

现在,用户给我的.CSV文件提供的数组我使用了@satafaka提供的方法:

// open CSV file
$handle = fopen("./_files/" . $file_name, "r");
$i = 0;
$csv_array = array();
// gets data from csv file
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
{
    // create vars
    $csv_array[$i]['email'] = $data[0];
    $csv_array[$i]['ext'] = $data[1];
    $csv_array[$i]['name'] = $data[2];

    $i++; // increment $i so value will increase

}

鉴于数组是正确的,输出应该是:$csv_array[0]['...'](...用于电子邮件,分机和/或名称)。

array(95) {
    [0] => array(3) {
        ["email"] => string(21)
        "user@example.com" ["ext"] => string(3)
        "100" ["name"] => string(20)
        "Worker100"
    }

将两者放在一起,并使用$offices$csv_array进行排序是棘手的部分,它看起来像这样:

<?php
function myArraySearch($off_list, $ext_list)
{
    /*
    This function is fairly simple; we need to match
    existing extension numbers to their correct office
    --
    To do so, we take two arrays:
    $off_list - this array holds the offices and their extentions (the order array)
    $ext_list - holds the actual user provided array (the array that needs to be arranged)

    With the two arrays ready, we run a `for` loop to loop trough $off_list
    while it does so, we then loop through $ext_list to get the content
    and match it in the correct office.
    */

    // setup $content -- which will hold output
    $content = "<div class='card'><ul class='mdc-list'><li><strong>";
    $office = null;

    // this for loop will take the master list I want to go by
    // then first set $a to zero, count $a till it reaches the
    // limit; which is the number of office extentions.
    for ($a = 0; $a < count($off_list[0]); $a++)
    {
        // check to see if $office is null
        if (!$office)
        {
            // given it is not null create assign
            // $off_list[][] to $office
            $office = $off_list[0][$a];
            // then add $office to $content
            $content .= $office;
        }
        // now check to see if office is not
        // equals to $off_list[][]
        else if ($office != $off_list[0][$a])
        {
            // if indeed it isnt, then assign it
            $office = $off_list[0][$a];
            // then add $office in the context of
            // $content
            $content .= "</strong></li></ul></div><div class='card'><ul class='mdc-list'><li><strong>" . $office . "</strong></li>";                            
        }

        // here we prepare to add our $ext_list array
        $content .= "<ul class='mdc-list mdc-list--dense'>";

        // create the loop for $ext_list
        for ($c = 0; $c < count($ext_list); $c++)
        {
            // to make sure that the array was being looped
            // I had to created a nested `foreach` loop that
            // cycles through the $ext_list array
            foreach ($ext_list as $c => $item)
            {
                // finally check to see if the $off_list extensions
                // match the $ext_list extentions; if so we create
                // `<li>` elements which will hold the content.
                if ($off_list[1][$a] == $ext_list[$c]['ext'])
                {

                    $content .= "<li class='mdc-list-item'>"
                                        . $ext_list[$c]['name'] . 
                                    "<span class='mdc-list-item__end-detail'>" . $ext_list[$c]['ext'] . "</span>
                                </li>";
                }

            }
        }
        // close the `<ul>` element
        $content .= "</ul>";
    }
    // close the surrounding elements as well
    $content .= "</ul></div>";

    // once complete return $content which holds
    // the precious information.
    return $content;
}