3v4l.org

run code in 500+ PHP versions simultaneously
<?php $arr = [ "a:1:{i:0;i:1;}", "a:3:{i:0;i:2;i:1;i:2;i:2;i:3;}", "a:3:{i:0;i:3;i:1;i:2;i:2;i:3;}", ]; $row = [['temp' => 1], ['temp1' => 2], ['temp3' => 2]]; $temp = array_map('unserialize', $arr); $brand_id = 1; $result = false; foreach ($temp as $key => $value) { if (!empty($value) && in_array($brand_id, $value)) { $result[] = $row[$key]; } } print_r($result); $brand_id = 2; $result = false; foreach ($temp as $key => $value) { if (!empty($value) && in_array($brand_id, $value)) { $result[] = $row[$key]; } } print_r($result);
Output for 8.1.0 - 8.1.34, 8.2.0 - 8.2.30, 8.3.0 - 8.3.30, 8.4.1 - 8.4.18, 8.5.0 - 8.5.3
Deprecated: Automatic conversion of false to array is deprecated in /in/e44aG on line 14 Array ( [0] => Array ( [temp] => 1 ) ) Deprecated: Automatic conversion of false to array is deprecated in /in/e44aG on line 23 Array ( [0] => Array ( [temp1] => 2 ) [1] => Array ( [temp3] => 2 ) )
Output for 7.1.0 - 7.1.33, 7.2.0 - 7.2.34, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30
Array ( [0] => Array ( [temp] => 1 ) ) Array ( [0] => Array ( [temp1] => 2 ) [1] => Array ( [temp3] => 2 ) )

preferences:
93.84 ms | 1743 KiB | 4 Q