A
A
Alexander Sisyukin2016-01-27 16:19:19
PHP
Alexander Sisyukin, 2016-01-27 16:19:19

If inside foreach?

I have this array
Array
(
[0] => Array
(
[ot_id] => 2
[ot_name] => Sisyukin Alexander Valerievich
[ot_email] => [email protected]
[ot_text] => pocoatsol acltsal tsla tsla tsll tsalts ltsl atsal ts
ts vts vtsv ts
[ot_kat] => 2
)
[1] => Array
(
[ot_id] => 1
[ot_name] => Nikishin Alexey Vasilievich
[ot_email] => [email protected]
[ot_text] => Revocation check plain text padding
[ot_kat] => 1
)
[2] =>Array
(
[kat_id] => 2
[kat_otz] => Bad review
)
[3] => Array
(
[kat_id] => 1
[kat_otz] => Good review
)
)
trying to display it like this

foreach ($res as $one)
{
    echo $one['ot_name'] . '<br />';
    echo $one['ot_text'] . '<br />';
    if ($one['ot_kat'] == $one['kat_id'])
    {
        echo $one['kat_otz'] . '<br />';
    } else
    {
        echo 'не работает';
    }


}

As a result, I wanted to see that if [ot_kat] = [kat_id] then displayed the name of the category that is stored in the cell of the [kat_otz] array
, due to its lack of erudition, I don’t understand why it’s impossible to insert a condition in foreach...

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
Outoverlay, 2016-01-27
@Caarl

Where is your kat_id key in the array?
What's with the fucking array anyway?
And if here and. As a construct, this is correct, but $one['ot_kat'] == $one['kat_id'].
Since you didn't specify what is in the $res variable, I can say that one of these: $one['ot_kat'], $one['kat_id'] will return null

$res_1 = array(
array(
'ot_id' => 1,
'ot_name' => 'Сисюкин Александр Валерьевич',
'ot_email' => '[email protected]',
'ot_text' => 'поцоацол ацлцал цла цла цлл цалц лцл ацал ц
ц вц вцв ц',
'ot_kat' => 1
)
);
$res_2 = array(
array(
'kat_id' => 1,
'kat_otz' => 'Хороший отзыв'
)
);
foreach( $res_1 as $val_1 ){
foreach( $res_2 as $val_2 ){
if( $val_1['ot_kat'] == $val_2['kat_id'] ){
//code...

break;
}

}


}

A
Andrey, 2016-01-27
@VladimirAndreev

you don't have a single element in the array in which your condition would be fulfilled.
and so - there are no obstacles, except for common sense, to nesting IFs in cycles

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question