PHP-在处理查找和分组数组项时

PHP-在处理查找和分组数组项时,php,arrays,csv,Php,Arrays,Csv,我正在处理电话簿列表;现在,当我从我们的电话目录下载.CSV文件时,它打印的内容只会给我用户ID、扩展名、名称和一些其他不太相关的角色 我的问题是我想按办公室对扩展/人员进行分组,因为电子表格没有提供任何这方面的提示,所以我想创建一个数组并将其与.CSV元素匹配 office扩展不太一致,因此对于office A,您可能有扩展:103、215和105。办公室B可能是:150104 现在,我创建了一个数组,在该数组中,我按office对扩展进行分组,结果如下所示: $offices = array

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

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

office扩展不太一致,因此对于office A,您可能有扩展:103、215和105。办公室B可能是:150104

现在,我创建了一个数组,在该数组中,我按office对扩展进行分组,结果如下所示:

$offices = array(
    'Office' => array (
        '110',
        '107',
        '137',
        '113'
    ),
    // ...
);
<?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;
}
对于实际阅读电子表格,我使用的是
fgetcsv
,它在没有任何类型的过滤器的情况下工作得非常好,但是每当我尝试在
循环时加入变体以“过滤”
,我就会遇到问题

我尝试过使用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>";
?>

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

知道
循环每行解析一行,除了在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);

希望我能帮上忙。

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

知道
循环每行解析一行,除了在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);

希望我能帮上忙。

经过深思熟虑,在@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_数组[0]['…']
(…用于电子邮件、ext和/或名称)

以一种功能性的方式将两者放在一起,并使用
$offices
$csv_数组
进行排序是一个棘手的部分,它看起来像这样:

$offices = array(
    'Office' => array (
        '110',
        '107',
        '137',
        '113'
    ),
    // ...
);
<?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;
}

经过深思熟虑,在@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_数组[0]['…']
(…用于电子邮件、ext和/或名称)

以一种功能性的方式将两者放在一起,并使用
$offices
$csv_数组
进行排序是一个棘手的部分,它看起来像这样:

$offices = array(
    'Office' => array (
        '110',
        '107',
        '137',
        '113'
    ),
    // ...
);
<?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;
}

为什么不在
中创建一个包含所有数据的数组,然后对其进行排序?@satafaka你的确切意思是什么?我将尝试回答一个可能的解决方案,但我不确定它是否能帮助你。为什么你不在
while
中创建一个包含所有数据的数组,然后对其进行排序?@satafaka你的确切意思是什么?我会尝试回答一个可能的解决方案,但我不确定它是否能帮助你。这确实让它变得清晰了一点。。。我现在只需要弄清楚如何根据一个部分对数组进行分组。如果你能更具体地说明你想要做什么,我可以为你提供更多帮助。请尝试编辑你的帖子我编辑了我的答案,试图更清楚。。。感谢您的帮助。您的
$offices
阵列是多维的吗?比如:
$offices['office1']={105102200},$offices['office2']={302301105}
等等?是的,先生。。。我用办公室的名字作为钥匙;例如:
$offices['office1'][0]
将调用如上所示的
105
这确实让它变得清晰了一点。。。我现在只需要弄清楚如何根据一个部分对数组进行分组。如果你能更具体地说明你想要做什么,我可以为你提供更多帮助。请尝试编辑你的帖子我编辑了我的答案,试图更清楚。。。感谢您的帮助。您的
$offices
阵列是多维的吗?比如:
$offices['office1']={105102200},$offices['office2']={302301105}
等等?是的,先生。。。我用办公室的名字作为钥匙;例如:
$offices['office1'][0]
将如上图所示呼叫
105
好工作伙伴,很高兴我能合作