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

Guhh
  • 410
  • 7
  • 15
  • By iterating through the array and checking the `['id_menu']` key? – h2ooooooo Mar 06 '14 at 15:15
  • Make your array output more readable – hindmost Mar 06 '14 at 15:16
  • You can get a formatted output by wrapping the `print_r()` statement in `
    ` tags like so: `echo '
    '.print_r($yourArray, TRUE).'
    ';`.
    – Amal Murali Mar 06 '14 at 15:17
  • This has been answered before, have a look: http://stackoverflow.com/a/1019126/407697 – sprain Mar 06 '14 at 15:20
  • You write a function that identifies the index of the `array` where `array[index]['id_menu'] === 7` and then you return `array[index]['consultar']` – Dima McGreen Mar 06 '14 at 15:22
  • 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. – Surreal Dreams Mar 06 '14 at 15:30

3 Answers3

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);
hindmost
  • 7,125
  • 3
  • 27
  • 39
0
foreach($arr as $key=>$value){
    if(isset($arr[$key]["id_menu"]) && $arr[$key]["id_menu"] == 7){
         echo $arr[$key]["consultar"];
    }
}
Burhan Çetin
  • 676
  • 7
  • 16
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']}";
}
vogomatix
  • 4,856
  • 2
  • 23
  • 46