K
K
kr_ilya2020-02-04 18:05:43
PHP
kr_ilya, 2020-02-04 18:05:43

How to add value to array while iterating over it in foreach?

Hello, I have 2 arrays that need to be iterated over and compared with some of their values. If the value of the first = the value of the second, then you need to write a new value to the first array, under the new key (type). I've tried different ways and can't figure it out.

This option returns

Illegal offset type in

$oneArray = ;
    $twoArray = ;

    foreach ($oneArray as $one) {
      foreach ($twoArray as $two) {

        if($one['2'] == $two['bb']){
          $oneArray[$one]['type'] = 'b';
        }else{
          $oneArray[$one]['type'] = 'c'
        }

      }
    }

//должно получиться
    $oneArray = ;

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dubrovin, 2020-02-04
@kr_ilya

$oneArray = ;
$twoArray = ;

foreach ( $oneArray[] as $one ) {
  foreach ( $twoArray[] as $two ) {
    if( $one === $two ){
      $oneArray[]['type'] = $one;
    }
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question