1

Hi I have such a structure array. I would like compare in loop such a condition.

if(dataInto[0]>=MyDataTypeFromInput && dataOut[0]<=MyDataTypeFromInput && room[0]==MyIdRoom) {...}

instead index 0, this have to work in loop foreach. So compare index [0] with index[0] [1] with [1] etc

How do this?

[
'dataInto' => [
    0 => '2016-07-14 14:50'
    1 => '2016-03-24 14:00'
    2 => '2016-03-03 06:30'
]
'room' => [
    0 => 13
    1 => 14
    2 => 14
]
'dataOut' => [
    0 => '2016-07-14 18:10'
    1 => '2016-03-24 17:20'
    2 => '2016-03-03 09:50'
   ]
]

2 Answers 2

1

Simply perform a foreach loop based on the length of one of main array items:

foreach( $array['dataInto'] as $key => $val )
{
    if
    (
        $array['dataInto'][$key] >= MyDataTypeFromInput 
        && 
        $array['dataOut'][$key]  <= MyDataTypeFromInput 
        && 
        $array['room'][$key]     == MyIdRoom
    )
    {
    ...
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

I would not do this in a foreach loop, a more sensible approach would be using a for loop since all indexes are numeric.

for($i = 0; $i < count($arr['dataInto']); $i++){
  if($arr['dataInto'][$i] >= MyDataTypeFromInput
  && $arr['dataOut'][$i]  <= MyDataTypeFromInput
  && $arr['room'][$i]     == MyIdRoom){
    // etc..
  }
}

If however you want to loop the main array a foreach loop is in order.

foreach($arr as $key => $value){ 
  echo $key, ', '; // ouput: "dataInto, dataOut, room" 
  foreach($value as $subkey => $subvalue){
    echo $subkey, ' -> ', $subvalue; // will first loop dataInto and display all 3 values with numeric keys 0, 1, 2.
  }
}

1 Comment

I understand your second code, but I need searching value after string index. Is there any other way yet to make it work? I would like see other solution ;)

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.