0

In for loop how to check current value with each and every previous value using php

my array:

In array list [prolabelpos] =>0 having three times, how to execute a [prolabelpos] =>0 only one time in for loop . how to check current array with all previous value and [prolabelpos] =>0 execute once in the for loop

Array ( 
    [0] => Array ( [productlabel_id] => 6 [prolabelpos] => 0  ) 
    [1] => Array ( [productlabel_id] => 5  [prolabelpos] => 6  )
    [2] => Array ( [productlabel_id] => 4  [prolabelpos] => 0 )
    [3] => Array ( [productlabel_id] => 3  [prolabelpos] => 5  )
    [4] => Array ( [productlabel_id] => 2 [prolabelpos] => 0  )
)

my code:

<?php  
$prev = null;
foreach ($result as $key => $value) {
    $label_position = $value['prolabelpos'];
    if ($prev != $label_position) {
        echo "my code";
    }
    $prev = $label_position;
}
5
  • I'm not quite clear on what you're trying to achieve.. You want only one result with prolabelpos = 0? What do you want to do with the other cases (where the ID is 4 and 2)? Commented May 3, 2019 at 6:08
  • previous value of [prolabelpos] =>0 (where id 4 and 2) not need to execute, i need to execute only higher order id 6 value of [prolabelpos] =>0 Commented May 3, 2019 at 6:12
  • Okay. Then you need to show how you create this array. Commented May 3, 2019 at 6:14
  • okay wait i will post a code Commented May 3, 2019 at 6:17
  • i was post my code above Commented May 3, 2019 at 6:25

1 Answer 1

1

You can approach this in foreach OR array_map

$arr = 
 Array ( 
 '0' => Array ( 'productlabel_id' => 6, 'prolabelpos' => 0  ),
 '1' => Array ( 'productlabel_id' => 5,  'prolabelpos' => 6  ),
 '2' => Array ( 'productlabel_id' => 4,  'prolabelpos' => 0 ),
 '3' => Array ( 'productlabel_id' => 3,  'prolabelpos' => 5  ),
 '4' => Array ( 'productlabel_id' => 2 ,'prolabelpos' => 0  )
);
$traversed = array();
foreach($arr as $value){
  if(in_array($value['prolabelpos'], $traversed)){
    //This has been traversed before
  }else{
    /* Apply your Logic */
    $traversed[] = $value['prolabelpos'];
  }
}

Using array_map

$arr = Array ( 
  '0' => Array ( 'productlabel_id' => 6, 'prolabelpos' => 0  ),
  '1' => Array ( 'productlabel_id' => 5,  'prolabelpos' => 6  ),
  '2' => Array ( 'productlabel_id' => 4,  'prolabelpos' => 0 ),
  '3' => Array ( 'productlabel_id' => 3,  'prolabelpos' => 5  ),
  '4' => Array ( 'productlabel_id' => 2 ,'prolabelpos' => 0  )
);
$traversed = array();
array_map(function($v) use (&$traversed){
  if(in_array($v['prolabelpos'], $traversed)){
    //This has been traversed before
  }else{
    /* Apply your Logic */
    $traversed[] = $v['prolabelpos'];
  }
}, $arr);
Sign up to request clarification or add additional context in comments.

Comments

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.