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
            )
8
  • 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] Commented Jun 11, 2014 at 13:06
  • @Andrew The same error still persists. Fatal error: [] operator not supported for strings Commented Jun 11, 2014 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 Commented Jun 11, 2014 at 13:15
  • you have a multidimensional array, are you sure that $keys and $values are from correct level of $unique_states array? Commented Jun 11, 2014 at 13:47
  • what are the contents of $unique_states array? just before first (for) loop, print_r it's data Commented Jun 11, 2014 at 20:10

3 Answers 3

1

Try this:

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

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

For details read: Dynamic variable names in PHP

Sign up to request clarification or add additional context in comments.

2 Comments

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.
Then you are doing something wrong with the structure of your whole mega loop. Try splitting into smaller parts and go step by step
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));
});

Comments

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];
            }
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.