0

I have an array that looks like this, that I'm looping through: Name of Array is $test

Array
(
[ven1] => Array
    (
        [0] => Array
            (
                [0] => ven1-center1
                [1] => ven1
                [2] => Address
            )

        [1] => Array
            (
                [0] => ven1-center2
                [1] => ven1
                [2] => Address
            )

        [2] => Array
            (
                [0] => ven1-center3
                [1] => ven1
                [2] => Address
            )

           .
           .
           .


 [ven2] => Array
    (
        [20] => Array
            (
                [0] => ven2-center1
                [1] => ven2
                [2] => Address
            )

        [21] => Array
            (
                [0] => ven2-center2
                [1] => ven2
                [2] => Address
            )

        [22] => Array
            (
                [0] => ven2-center3
                [1] => ven2
                [2] => Address
            )

I have the php code below to loop the whole array, grab all the values of [0] from the innermost dimension, store them into an array with the name of $k. When I run the code below, I get Notice: Undefined offset: for all the values of $c.

    for($c = 0; $c < 42; $c++){
        foreach($test as $k =>  $v){
          if($test[$k][$c][1] == $k){
           $k = $test[$k][$c][0];
         }
       }
     }

The output am looking for is

$ven1 = array("ven1-center1","ven1-center2","ven1-center3","ven1-center4")
$ven2 = array("ven1-center1","ven1-center2","ven1-center3","ven1-center4")
3
  • Creating numbered variables like that is almost always wrong. Use an array. Commented Jun 13, 2014 at 0:31
  • @Barmar Can you PLEASE explain what you mean? Commented Jun 13, 2014 at 0:33
  • 1
    Instead of $ven1 and $ven2, you should have an array $ven, with $ven['ven1'] = array("ven1-center1","ven1-center2","ven1-center3","ven1-center4") and $ven['ven2'] = array("ven1-center1","ven1-center2","ven1-center3","ven1-center4"). Commented Jun 13, 2014 at 0:37

1 Answer 1

1

I think this will do what you want... assuming I interpreted your question correctly:

foreach($test as $varname => $array){
   foreach($array as $row) {
       if ($row[1] == $varname) ${$varname}[] = $row[0];
   }
}

var_dump($ven1);
var_dump($ven2);
Sign up to request clarification or add additional context in comments.

3 Comments

Both $ven1 and $ven2 echoes out exactly. When I print_r( ${$varname}) I do not get an array of $ven1 and $ven2. Is this normal? Also can what notation is ` ${$varname}` ? Thanks
${$varname}[] means take the variable with name $varname - so in this case, $varname contains "ven1", so we are taking the variable $ven1. Normally you can do $$varname, but since it's an array, we have to use {} because it's ambiguous if the [] refers to $varname or $$varname.

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.