循环遍历数组并创建一个匹配参数名称的新数组

时间:2014-06-11 12:40:04

标签: php arrays

我正在尝试遍历一个多维数组,如果一个条件匹配,那么该数组的所有元素都应存储到一个以匹配条件命名的新数组中。

     $unique_states = array_unique($allstates);
       $total = count($states);
       for($i = 0; $i < $total; $i++){
        foreach($unique_states as $keys =>  $values){
             if(($states[$i][3] == $values)){
          $values[] = $states[$i];
        }
        }
    }

$states是包含所有内容的巨型数组。当我遍历数组时,如果匹配此条件if(($states[$i][3] == $values)),则应创建一个名为$values的新数组。

当我运行代码print_r($values)时,我得到Fatal error: [] operator not supported for strings。当我将名称更改为完全不同的名称时,我只需在$states中获取所有数组。

不知道该怎么做。

编辑:

当我删除foreach循环并插入$values的实际值时

  if(($states[$i][3] == 'state1')){
          $states1[] = $states[$i];
        }

  if(($states[$i][3] == 'state2')){
          $states2[] = $states[$i];
        }

效果很好。但是在我有几个状态的情况下,上面的代码不够高效,并且每次添加新状态时都要求我进行更改。 我需要返回整个元素的数组.i.e $ states [$ i]其中$ states [$ i] [3]匹配。

enter image description here

来自$ states输入的数据示例:

Array
(
[0] => Array
    (
        [0] => firstname1
        [1] => lastname1
        [2] => Armstrong Landscaping
        [3] => state1
        [4] => email1
        [5] => address1
    )

[1] => Array
    (
        [0] => firstname2
        [1] => lastname2
        [2] => Armstrong Landscaping
        [3] => state1
        [4] => email2
        [5] => address2
    )

[2] => Array
    (
        [0] => firstname3
        [1] => lastname3
        [2] => Armstrong Landscaping
        [3] => state1
        [4] => email3
        [5] => address3
    )

[3] => Array
    (
        [0] => firstname4
        [1] => lastname4
        [2] => Cannon Heathcare Center
        [3] => state2
        [4] => email4
        [5] => address4
    )

[4] => Array
    (
        [0] => firstname5
        [1] => lastname5
        [2] => Cannon Heathcare Center
        [3] => state2
        [4] => email5
        [5] => address5
    )

示例输出应该看起来像这样

Array
(
[state1] => Array
    (
        [0] => Array
            (
                [0] => firstname1
                [1] => lastname1
                [2] => Armstrong Landscaping
                [3] => state1
                [4] => email1
                [5] => address1
            )

        [1] => Array
            (
                [0] => firstname2
                [1] => lastname2
                [2] => Armstrong Landscaping
                [3] => state1
                [4] => email2
                [5] => address2
            )

Array
(
[state2] => Array
    (
        [0] => Array
            (
                [0] => firstname1
                [1] => lastname1
                [2] => Armstrong Landscaping
                [3] => state2
                [4] => email1
                [5] => address1
            )

        [1] => Array
            (
                [0] => firstname2
                [1] => lastname2
                [2] => Armstrong Landscaping
                [3] => state2
                [4] => email2
                [5] => address2
            )

3 个答案:

答案 0 :(得分:1)

试试这个:

${$values} = array();
${$values}[] = $states[$i];

输出用途:print_r(${$values});

有关详细信息,请阅读:Dynamic variable names in PHP

答案 1 :(得分:1)

使用array_filter

可以提高效率
//ussing inline function requires PHP 5.3
$values = array_filter($states, function($state) {
   return (in_array($state[3], $unique_states));
});

答案 2 :(得分:0)

刚想出来了。当每个条件匹配时,它将存储在一个新的多维数组中。

$unique_states = array_unique($allstates);
      $total = count($states);
      for($i = 0; $i < $total; $i++){
          foreach($unique_states as $keys =>  $values){
            if($states[$i][3] ==$values){
               $new[$values][$i] = $states[$i];
            }
        }
    }