Выбрать данные из JSON (php) при помощи дрогого json
Есть два json
{
"7":{"name_items":"СТЕЙК ИЗ ГОВЯДИНЫ С ОВОЩАМИ","price":"700","description":"","photo":"images\/positions\/1598253756.jpg","type":"Тандыр","calories":"174","weight":null,"restaurant":"","Stop":"0"},
"8":{"name_items":"СТЕЙК ИЗ СВИНИНЫ С ОВОЩАМИ","price":"600","description":"","photo":"images\/positions\/1598253968.jpg","type":"Тандыр","calories":"326","weight":null,"restaurant":"","Stop":"0"},
"9":{"name_items":"СВИНИНА НА КОСТОЧКЕ ЗАПЕЧЕННАЯ","price":"600","description":"","photo":"images\/positions\/1598254091.jpg","type":"Тандыр","calories":"361","weight":null,"restaurant":"","Stop":"0"},
"10":{"name_items":"ШАШЛЫК ИЗ КУРИНЫХ БЕДЕР","price":"350","description":"","photo":"images\/positions\/1598254457.jpg","type":"Тандыр","calories":"412","weight":null,"restaurant":"","Stop":"0"},
"11":{"name_items":"ШААШЛЫК ИЗ КУРИНЫХ КРЫЛЬЕВ","price":"330","description":"","photo":"images\/positions\/1598254623.jpg","type":"Тандыр","calories":"358","weight":null,"restaurant":"Лайм","Stop":"0"},
}
и второй
{"7":1,"9":1,"11":1}
Подскажите пожалуйста как вывести значения свойств позиций используя второй json содержащий их id?
Ответы (2 шт):
Автор решения: www3b
→ Ссылка
<?php
$list = '{
"7":{"name_items":"СТЕЙК ИЗ ГОВЯДИНЫ С ОВОЩАМИ","price":"700","description":"","photo":"images\/positions\/1598253756.jpg","type":"Тандыр","calories":"174","weight":null,"restaurant":"","Stop":"0"},
"8":{"name_items":"СТЕЙК ИЗ СВИНИНЫ С ОВОЩАМИ","price":"600","description":"","photo":"images\/positions\/1598253968.jpg","type":"Тандыр","calories":"326","weight":null,"restaurant":"","Stop":"0"},
"9":{"name_items":"СВИНИНА НА КОСТОЧКЕ ЗАПЕЧЕННАЯ","price":"600","description":"","photo":"images\/positions\/1598254091.jpg","type":"Тандыр","calories":"361","weight":null,"restaurant":"","Stop":"0"},
"10":{"name_items":"ШАШЛЫК ИЗ КУРИНЫХ БЕДЕР","price":"350","description":"","photo":"images\/positions\/1598254457.jpg","type":"Тандыр","calories":"412","weight":null,"restaurant":"","Stop":"0"},
"11":{"name_items":"ШААШЛЫК ИЗ КУРИНЫХ КРЫЛЬЕВ","price":"330","description":"","photo":"images\/positions\/1598254623.jpg","type":"Тандыр","calories":"358","weight":null,"restaurant":"Лайм","Stop":"0"}
}';
$values = '{"7":1,"9":1,"11":1}';
$listArr = json_decode($list, true);
$valuesArr = json_decode($values, true);
$result = [];
foreach($valuesArr as $key => $value) {
$result[] = $listArr[$key];
}
print_r($result);
Автор решения: renich
→ Ссылка
На JS можно конечно использовать методы бегающие по ключам/индексам, но я предпочитаю скорость работы скрипта, поэтому ниже - через создание копии объекта и удаление из него ненужного в один проход.
const source = `{
"7":{"name_items":"СТЕЙК ИЗ ГОВЯДИНЫ С ОВОЩАМИ","price":"700","description":"","photo":"images\/positions\/1598253756.jpg","type":"Тандыр","calories":"174","weight":null,"restaurant":"","Stop":"0"},
"8":{"name_items":"СТЕЙК ИЗ СВИНИНЫ С ОВОЩАМИ","price":"600","description":"","photo":"images\/positions\/1598253968.jpg","type":"Тандыр","calories":"326","weight":null,"restaurant":"","Stop":"0"},
"9":{"name_items":"СВИНИНА НА КОСТОЧКЕ ЗАПЕЧЕННАЯ","price":"600","description":"","photo":"images\/positions\/1598254091.jpg","type":"Тандыр","calories":"361","weight":null,"restaurant":"","Stop":"0"},
"10":{"name_items":"ШАШЛЫК ИЗ КУРИНЫХ БЕДЕР","price":"350","description":"","photo":"images\/positions\/1598254457.jpg","type":"Тандыр","calories":"412","weight":null,"restaurant":"","Stop":"0"},
"11":{"name_items":"ШААШЛЫК ИЗ КУРИНЫХ КРЫЛЬЕВ","price":"330","description":"","photo":"images\/positions\/1598254623.jpg","type":"Тандыр","calories":"358","weight":null,"restaurant":"Лайм","Stop":"0"}
}`;
const select = `{"7":1,"9":1,"11":1}`;
const sourceParsed = JSON.parse(source);
const selectParsed = JSON.parse(select);
const result = {...sourceParsed};
Object.keys(result).forEach(key => !selectParsed[key] && delete(result[key]));
console.log(result);