0

I'm experimenting with a multidimensional array in PHP with strings as values. I want to echo all the values of one single column, instead of all values of all columns.

First i create the array!

$test = array
(
  array("hoge", "bomen", "vangen"),
  array("moeten", "we", "rekening"),
  array("voor", "deze", "tijd")
  );    ``

This foreach outputs all the values!

foreach ($test as $val) {

    echo "$val" . "<br/>";
    }

How can i output only the values of column 2? instead of the values of all the columns.

I want this output:

bomen
we
deze
3
  • Please post what the expected output would be in this example Commented Jan 29, 2015 at 19:39
  • echo $val[0] for the first one, etc... Commented Jan 29, 2015 at 19:42
  • If you didn't already saw it you can take a tour here: stackoverflow.com/tour and see how this site works :D (Welcome on SO) Commented Jan 29, 2015 at 19:49

6 Answers 6

1

This should work for you:

$test = array(
          array("hoge", "bomen", "vangen"),
          array("moeten", "we", "rekening"),
          array("voor", "deze", "tijd")
        );   

               //vv Go trough each innerArray
foreach($test as $v) 
    echo $v[1] . "<br />";
       //^^^^^ Print the second element of each innerArray

Output:

bomen
we
deze

Also for more information about arrays see the manual: http://php.net/manual/en/language.types.array.php (The manual is always a good reference to search for things and learn it :D)

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

2 Comments

@Fille You're welcome! Enjoy your day :D (FYI: If you have a php problem just do a quick google search with keywords of the problem, search it in the manual (php.net) and or try a little bit debugging :D)
@Fille Click here and read this comment left under your question.
0

If you wanted to display the elements of the first inner array, then

foreach($test[0] as $value) echo $value."\n";

2 Comments

I think he wants the first element of EVERY inner array.
i think its 50\50 between the two possibilities
0

This code can solve the problem:

$test = array
(
  array("hoge", "bomen", "vangen"),
  array("moeten", "we", "rekening"),
  array("voor", "deze", "tijd")
  );
  for($i=0; $i<count($test);$i++) {
    echo $test[$i][1];
}

If foreach is not your choice.

Comments

0

It depends what do you mean by column, but this is probably what you are looking for

$column = 0;
foreach ($test as $val) {
    echo $val[$column];
}

Comments

0

Prints the value for the column number, which is set in the $column variable.

<?php
    $test = array(
        array("hoge", "bomen", "vangen"),
        array("moeten", "we", "rekening"),
        array("voor", "deze", "tijd")
    );

    $column = 1; // number of the column you want
    array_map(function($v) use ($column) {
        echo $v[$column-1] . " ";
    },$test);
?>

Comments

0

Try:

$test2 = array_map( 
                    function($element){ 
                        return $element[0]; 
                    },  
                    $test 
          );

$element[0] is the element you want it from each array, and do a foreach for $test2 or print inside closure.

Or use:

$arr = array_column($test, 0);

0 is the index key you want to keep. do a foreach on $arr.

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.