0

i have a result on mysql where i have a lot of lines and a lot of columns. i need to echo a specific line that i don't know.

Example:

Array
(
    [0] => Array
        (
            [0] => 195
            [id_privilegios] => 195
            [1] => 206
            [id_usuario] => 206
            [2] => 10
            [id_menu] => 10
            [3] => S
            [consultar] => S
            [4] => 
            [inserir] => 
            [5] => S
            [alterar] => S
            [6] => 
            [excluir] => 
        )

    [1] => Array
        (
            [0] => 194
            [id_privilegios] => 194
            [1] => 206
            [id_usuario] => 206
            [2] => 9
            [id_menu] => 9
            [3] => S
            [consultar] => S
            [4] => S
            [inserir] => S
            [5] => S
            [alterar] => S
            [6] => S
            [excluir] => S
        )

    [2] => Array
        (
            [0] => 193
            [id_privilegios] => 193
            [1] => 206
            [id_usuario] => 206
            [2] => 1
            [id_menu] => 1
            [3] => S
            [consultar] => S
            [4] => S
            [inserir] => S
            [5] => S
            [alterar] => S
            [6] => S
            [excluir] => S
        )

    [3] => Array
        (
            [0] => 224
            [id_privilegios] => 224
            [1] => 206
            [id_usuario] => 206
            [2] => 56
            [id_menu] => 56
            [3] => S
            [consultar] => S
            [4] => S
            [inserir] => S
            [5] => S
            [alterar] => S
            [6] => 
            [excluir] => 
        )

    [4] => Array
        (
            [0] => 223
            [id_privilegios] => 223
            [1] => 206
            [id_usuario] => 206
            [2] => 52
            [id_menu] => 52
            [3] => S
            [consultar] => S
            [4] => S
            [inserir] => S
            [5] => S
            [alterar] => S
            [6] => S
            [excluir] => S
        )

)

i have this array for example and i want to show the value of "consultar" column where id_menu = 7

how i can echo this ?

thanks

6
  • By iterating through the array and checking the ['id_menu'] key? Commented Mar 6, 2014 at 15:15
  • Make your array output more readable Commented Mar 6, 2014 at 15:16
  • You can get a formatted output by wrapping the print_r() statement in <pre> tags like so: echo '<pre>'.print_r($yourArray, TRUE).'</pre>';. Commented Mar 6, 2014 at 15:17
  • This has been answered before, have a look: stackoverflow.com/a/1019126/407697 Commented Mar 6, 2014 at 15:20
  • 1
    Would it make more sense to change your query so you only got the result where the id_menu value matches what you want? What you are doing may make sense for your situation, but it's often best to query so your result data better matches up with your needs. Commented Mar 6, 2014 at 15:30

3 Answers 3

1

You may use array_filter in conjunction with array_map.

$result = array_map(
    function ($v) {
        return $v['consultar'];
    },
    array_filter($array, function ($v) {
        return $v['id_menu'] == 7;
    })
);
print_r($result);
Sign up to request clarification or add additional context in comments.

3 Comments

it is missing an ")" i change to array_filter($array, function ($v)) { but it doesnt work "/
Parse error: syntax error, unexpected T_FUNCTION, expecting ')'
Update your PHP version to 5.3+
0

If only one item, you'll have to iterate through and find the matching element, if more than one item, one way is to use array_filter to filter out all the matching array elements.

function myFilter( $row) {
   return ($row['id_menu'] == 7);
}

$matches = array_filter($my_array, "myFilter");
foreach ($matches as $element) {
   echo "Menu Id: {$element['id_menu']} Consultar: {$element['consultar']}";
}

Comments

0
foreach($arr as $key=>$value){
    if(isset($arr[$key]["id_menu"]) && $arr[$key]["id_menu"] == 7){
         echo $arr[$key]["consultar"];
    }
}

1 Comment

Warning: Invalid argument supplied for foreach()

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.