1

I have a multidimensional array:

$arr = Array (
            [0] => Array (
                [0] => 1001
                [1] => frank
                [2] => getfrankemail)
            [1] => Array (
                [0] => 1007
                [1] => youi
                [2] => getyouiemail)
            [2] => Array (
                [0] => 1006
                [1] => nashua
                [2] => getnashuaemail)
            );

I want to get the values of each array by using a loop or something so I could then put the values into variables such that $aff = 1001, $desc = frank and $camp = getfrankemail and so on...

Is there a way to achieve this? Thanks in advance!

3
  • There is more than one way to do this. Which one did you already try? Commented Nov 16, 2013 at 12:51
  • Of course there is a way... See what for and foreach can do for you. Commented Nov 16, 2013 at 12:52
  • Read the docs about loops and arrays. You'll discover that it's full of examples! For example here: us2.php.net/manual/en/language.types.array.php Commented Nov 16, 2013 at 12:52

2 Answers 2

2

It depends on what you want to do with the variables but that should give you an idea.

$arr = Array (
            0 => Array (
                0 => 1001,
                1 => 'frank',
                2 => 'getfrankemail'),
            1 => Array (
                0 => 1007,
                1 => 'youi',
                2 => 'getyouiemail'),
            2 => Array (
                0 => 1006,
                1 => 'nashua',
                2 => 'getnashuaemail')
            );




foreach($arr as $array)
{
    foreach($array as $key => $info)
    {
      echo '<p>'.$key.' => '.$info.'</p>';
    }
}

Or

foreach($arr as $array)
{
    foreach($array as $info)
    {
      echo '<p>'.$info.'</p>';
    }
}

Or

foreach($arr as $array)
{
    echo '<p>'.$array[0].'</p>';
}

Or

foreach($arr[0] as $info)
{
    echo '<p>'.$info.'</p>';
}

And more...

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

Comments

0

You can use a nested foreach loop (not very efficient or anything, but it gets the job done).

Here's an example:

foreach($arr as $key => $nested_arr){
    foreach($nested_arr as $key_2 => $value){
        //Do what you want with the values here. For example put them in 1d arrays.
    }
}

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.