3

I'm trying to loop through a multidimensional array, whereby, if a condition is matched, all elements of that array should be stored into a new array named after the condition it matched.

     $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 is the mega array that contains everything. When I loop through the array so that if this condition if(($states[$i][3] == $values)) is matched, a new array should be created with the name of $values.

When I run the code and print_r($values), I get Fatal error: [] operator not supported for strings. When I change the name to something totally different, I simply get all the array in $states.

Not sure how to do this anymore.

Edit:

When I remove the foreach loop and insert the actual values of $values like this

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

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

it works very well. But in a case where I have several states, the code above will not be efficient enough and will require that I make changes each time a new state is added. I need to return an array of the whole elements..i.e $states[$i] for which $states[$i][3] matched.

enter image description here

Example of data from $states input:

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
    )

Example out put should look something like this

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
            )
hello
  • 1,168
  • 4
  • 24
  • 59
  • if $values equalls, for example, to 'thevalue' then what you are doing is: thevalue[] = $states[$i], which is wrong. So you need to add '$' at the beginning or in other words $$values[] = $states[$i] – Andrew Jun 11 '14 at 13:06
  • @Andrew The same error still persists. `Fatal error: [] operator not supported for strings` – hello Jun 11 '14 at 13:08
  • if you are using dynamic names for arrays, add brackets arround the dynamic-name: ${$values}[] = ... If you work with single variables only, no need for brackets – Andrew Jun 11 '14 at 13:15
  • you have a multidimensional array, are you sure that $keys and $values are from correct level of $unique_states array? – Andrew Jun 11 '14 at 13:47
  • what are the contents of $unique_states array? just before first (for) loop, print_r it's data – Andrew Jun 11 '14 at 20:10
  • Before the for loop Array ( [0] => state1 [20] => state2 ) – hello Jun 11 '14 at 20:53
  • and you already updated code to use: ${$values}[] = $states[$i]; ? If so, it must work. There is something wrong elsewhere.. – Andrew Jun 11 '14 at 22:00
  • Yes I have. What is get back is just the whole array.. same thing as $states. – hello Jun 11 '14 at 22:10

3 Answers3

1

Try this:

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

for output use: print_r(${$values});

For details read: Dynamic variable names in PHP

Community
  • 1
  • 1
Manwal
  • 23,450
  • 12
  • 63
  • 93
  • When I insert ` ${$values} = array();` inside the `if()` loop, I only get the last element in `$states`..(the mega array). When I insert outside the loops, I get undefined variables. – hello Jun 11 '14 at 12:52
  • Then you are doing something wrong with the structure of your whole mega loop. Try splitting into smaller parts and go step by step – Andrew Jun 11 '14 at 13:17
1

This can be done efficient with array_filter

//ussing inline function requires PHP 5.3
$values = array_filter($states, function($state) {
   return (in_array($state[3], $unique_states));
});
d.raev
  • 9,216
  • 8
  • 58
  • 79
0

Just figured this out. When each condition is matched, it is stored inside a new multidimensional array.

$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];
            }
        }
    }
hello
  • 1,168
  • 4
  • 24
  • 59