Telegram Group & Telegram Channel
Сегодня я хочу показать вам простой, но очень полезный приём для работы с массивами в PHP — использование функции array_column().

Очень часто бывает задача: из многомерного массива "вытащить" только один столбец данных. Например, у вас есть массив пользователей:


$users = [
['id' => 1, 'name' => 'Иван', 'email' => '[email protected]'],
['id' => 2, 'name' => 'Ольга', 'email' => '[email protected]'],
['id' => 3, 'name' => 'Павел', 'email' => '[email protected]'],
];


И нужно получить только список всех email'ов. Многие делают это через foreach, но есть более элегантное решение:


$emails = array_column($users, 'email');


Результат будет таким:


[
'[email protected]',
'[email protected]',
'[email protected]'
]


А ещё array_column() позволяет указать третий параметр — ключ массива. Например, сделать email по id:


$emailsById = array_column($users, 'email', 'id');


И тогда получится:


[
1 => '[email protected]',
2 => '[email protected]',
3 => '[email protected]'
]


Это удобный способ быстрого преобразования данных без лишнего кода.

Пользуетесь ли вы array_column() в своих проектах? Или предпочитаете что-то другое? Напишите в комментариях!

👉 @php_lib



tg-me.com/php_lib/743
Create:
Last Update:

Сегодня я хочу показать вам простой, но очень полезный приём для работы с массивами в PHP — использование функции array_column().

Очень часто бывает задача: из многомерного массива "вытащить" только один столбец данных. Например, у вас есть массив пользователей:


$users = [
['id' => 1, 'name' => 'Иван', 'email' => '[email protected]'],
['id' => 2, 'name' => 'Ольга', 'email' => '[email protected]'],
['id' => 3, 'name' => 'Павел', 'email' => '[email protected]'],
];


И нужно получить только список всех email'ов. Многие делают это через foreach, но есть более элегантное решение:


$emails = array_column($users, 'email');


Результат будет таким:


[
'[email protected]',
'[email protected]',
'[email protected]'
]


А ещё array_column() позволяет указать третий параметр — ключ массива. Например, сделать email по id:


$emailsById = array_column($users, 'email', 'id');


И тогда получится:


[
1 => '[email protected]',
2 => '[email protected]',
3 => '[email protected]'
]


Это удобный способ быстрого преобразования данных без лишнего кода.

Пользуетесь ли вы array_column() в своих проектах? Или предпочитаете что-то другое? Напишите в комментариях!

👉 @php_lib

BY Библиотека PHP программиста 👨🏼‍💻👩‍💻


Warning: Undefined variable $i in /var/www/tg-me/post.php on line 283

Share with your friend now:
tg-me.com/php_lib/743

View MORE
Open in Telegram


Библиотека PHP программиста ‍‍ Telegram | DID YOU KNOW?

Date: |

How Does Bitcoin Work?

Bitcoin is built on a distributed digital record called a blockchain. As the name implies, blockchain is a linked body of data, made up of units called blocks that contain information about each and every transaction, including date and time, total value, buyer and seller, and a unique identifying code for each exchange. Entries are strung together in chronological order, creating a digital chain of blocks. “Once a block is added to the blockchain, it becomes accessible to anyone who wishes to view it, acting as a public ledger of cryptocurrency transactions,” says Stacey Harris, consultant for Pelicoin, a network of cryptocurrency ATMs. Blockchain is decentralized, which means it’s not controlled by any one organization. “It’s like a Google Doc that anyone can work on,” says Buchi Okoro, CEO and co-founder of African cryptocurrency exchange Quidax. “Nobody owns it, but anyone who has a link can contribute to it. And as different people update it, your copy also gets updated.”

How To Find Channels On Telegram?

There are multiple ways you can search for Telegram channels. One of the methods is really logical and you should all know it by now. We’re talking about using Telegram’s native search option. Make sure to download Telegram from the official website or update it to the latest version, using this link. Once you’ve installed Telegram, you can simply open the app and use the search bar. Tap on the magnifier icon and search for a channel that might interest you (e.g. Marvel comics). Even though this is the easiest method for searching Telegram channels, it isn’t the best one. This method is limited because it shows you only a couple of results per search.

Библиотека PHP программиста ‍‍ from us


Telegram Библиотека PHP программиста 👨🏼‍💻👩‍💻
FROM USA