Добрый день
Плохо разбираюсь в php, помогите пожалуйста решить задачку
Нужно найти по sku значения массива и вывести их
// Массив из которого надо получить значения по SKU
$arr = [
["sku" => '1', "name" => '1111', "price" => '1111'],
["sku" => '2', "name" => '2222', "price" => '2222'],
["sku" => '3', "name" => '3333', "price" => '3333'],
["sku" => '4', "name" => '4444', "price" => '4444'],
["sku" => '5', "name" => '5555', "price" => '5555'],
["sku" => '6', "name" => '6666', "price" => '6666'],
["sku" => '7', "name" => '7777', "price" => '7777'],
["sku" => '8', "name" => '8888', "price" => '8888'],
["sku" => '9', "name" => '9999', "price" => '9999'],
["sku" => '10', "name" => '1010', "price" => '1010'],
["sku" => '11', "name" => '1111', "price" => '1111'],
["sku" => '12', "name" => '1212', "price" => '1212'],
["sku" => '13', "name" => '1313', "price" => '1313'],
["sku" => '14', "name" => '1414', "price" => '1414'],
["sku" => '15', "name" => '1515', "price" => '1515'],
["sku" => '16', "name" => '1616', "price" => '1616'],
["sku" => '17', "name" => '1717', "price" => '1717'],
["sku" => '18', "name" => '1818', "price" => '1818']
];
// Массив с нужными SKU
$torgPred = ['3','5','7','11','18'];
// Новый массив состоящий из нужных SKU, NAME, PRICE
$newArr = [];
$i=0;
foreach( $arr as $row ){
if ( $row['sku'] = $torgPred[$i] ) {
array_push($newArr, [ 'sku' => $row['sku'], 'name' => $row['name'], 'price' => $row['price'] ] );
} $i++;
}
Но в итоге получаем вот это:
Array
(
[0] => Array
(
[sku] => 5
[name] => 2222
[price] => 2222
)
[1] => Array
(
[sku] => 7
[name] => 3333
[price] => 3333
)
[2] => Array
(
[sku] => 11
[name] => 4444
[price] => 4444
)
[3] => Array
(
[sku] => 18
[name] => 5555
[price] => 5555
)
)
а должно быть так
Array
(
[0] => Array
(
[sku] => 5
[name] => 5555
[price] => 5555
)
[1] => Array
(
[sku] => 7
[name] => 7777
[price] => 7777
)
[2] => Array
(
[sku] => 11
[name] => 1111
[price] => 1111
)
[3] => Array
(
[sku] => 18
[name] => 1818
[price] => 1818
)
)
Подскажите пожалуйста где ошибка и как исправить?