没有不值得去解决的问题,也没有不值得去学习的技术!

Refactoring the Reindexing of Multidimensional Arrays in PHP 7.4

Print results of the multidimensional array before and after indexing. Matches expectations

1. Review the existing implementation code below:

PHP

$data = $res['body']['data'];
print_r($data);
$data = array_combine(array_column($data, 'name'), $data);
print_r($data);
exit;

2. Check the print results of the multidimensional array before and after indexing. Use the value of the name column in the array as the new key. See Figure 1.

Print results of the multidimensional array before and after indexing. Use the value of the name column in the array as the new key
Figure 1
Plaintext
Array
(
    [0] => Array
        (
            [id] => 8
            [user_id] => 6659
            [name] => athena
            [status] => published
            [title_en] => Athena
            [title_cn] => 雅典娜
            [description_en] => Athena is a template with massive decoration modules. Its social, festive, interactive elements can make a  strong impact. Meanwhile, it is committed to creating a unique shopping experience on mobile phones. Suitable for all middle and high-end brands.
            [description_cn] => Athena是一款装修模块极其丰富的模板,以社交、节日气氛、页面交互等元素带来极强的冲击,同时致力于打造移动端独特的购物体验,适合中高端品牌站。
            [cover] => http://172.16.12.250:30005/linkun/saas/WA2RbFlR6BxR2Gw6845nWZDtRdsZMZlXbaFBzrl2.png
        )

    [1] => Array
        (
            [id] => 7
            [user_id] => 6659
            [name] => vogue
            [status] => published
            [title_en] => Vogue
            [title_cn] => Vogue
            [description_en] => Vogue adds a video module that can fully display product details. It is a set of templates designed specifically for brand and product stories.
            [description_cn] => Vogue 模板增加了视频模块,可以全面的展示产品细节,是专为品牌和产品故事而设计的一套模板。
            [cover] => http://172.16.12.250:30005/linkun/saas/EXGY62GLqebTEVoEDS19ynVxGmlktoCUFYCZGmom.png
        )

    [2] => Array
        (
            [id] => 6
            [user_id] => 
            [name] => venue
            [status] => published
            [title_en] => Volume
            [title_cn] => Volume
            [description_en] => Volume uses a clean design style and adds a picture and text module, which is very suitable for high-end brand products.
            [description_cn] => Volume模板使用干净的设计风格,增加了图片文字模块,非常适合高端品牌产品。
            [cover] => http://172.16.12.250:30005/linkun/saas/nPVwmFPGMUvOa8hVvlfabKspjzbQ8C0oQHUrm2YP.png
        )

    [3] => Array
        (
            [id] => 5
            [user_id] => 6659
            [name] => showtime
            [status] => published
            [title_en] => showtime
            [title_cn] => showtime
            [description_en] => showtime
            [description_cn] => showtime
            [cover] => http://172.16.12.250:30005/linkun/saas/dKtQRxcmKgbQkyrfstqiWKKtVYQ8zKHNYP6SOh8D.png
        )

    [4] => Array
        (
            [id] => 4
            [user_id] => 6659
            [name] => basic
            [status] => published
            [title_en] => Basic
            [title_cn] => Basic
            [description_en] => Basic is our iconic template, very versatile and concise, suitable for most styles of shops.
            [description_cn] => Basic 是我们标志性的模板,非常通用以及简洁,适合大部分风格的店铺。
            [cover] => http://172.16.12.250:30005/linkun/saas/r9On4359aPYErGWBikYnJNtd2w7qwrEIefmzs7Qp.png
        )

    [5] => Array
        (
            [id] => 3
            [user_id] => 6659
            [name] => showcase
            [status] => published
            [title_en] => showcase
            [title_cn] => showcase
            [description_en] => showcase
            [description_cn] => showcase
            [cover] => http://172.16.12.250:30005/linkun/saas/kAR3fGb1RAWlAoQM88JApY2yg4GJn1ubMX9Duz05.png
        )

    [6] => Array
        (
            [id] => 2
            [user_id] => 6659
            [name] => volume
            [status] => published
            [title_en] => volume
            [title_cn] => volume
            [description_en] => volume
            [description_cn] => volume
            [cover] => http://172.16.12.250:30005/linkun/saas/kcvOpGTaItgeN0DH7Ogs91LWgWx0jTqD2usTn0Wz.png
        )

    [7] => Array
        (
            [id] => 1
            [user_id] => 6659
            [name] => test
            [status] => published
            [title_en] => test
            [title_cn] => test
            [description_en] => test
            [description_cn] => test
            [cover] => http://172.16.12.250:30005/linkun/saas/1v0wvIy5XQNIo2y4yJE4AycO8zt1jpNawBN14NPu.png
        )

)
Array
(
    [athena] => Array
        (
            [id] => 8
            [user_id] => 6659
            [name] => athena
            [status] => published
            [title_en] => Athena
            [title_cn] => 雅典娜
            [description_en] => Athena is a template with massive decoration modules. Its social, festive, interactive elements can make a  strong impact. Meanwhile, it is committed to creating a unique shopping experience on mobile phones. Suitable for all middle and high-end brands.
            [description_cn] => Athena是一款装修模块极其丰富的模板,以社交、节日气氛、页面交互等元素带来极强的冲击,同时致力于打造移动端独特的购物体验,适合中高端品牌站。
            [cover] => http://172.16.12.250:30005/linkun/saas/WA2RbFlR6BxR2Gw6845nWZDtRdsZMZlXbaFBzrl2.png
        )

    [vogue] => Array
        (
            [id] => 7
            [user_id] => 6659
            [name] => vogue
            [status] => published
            [title_en] => Vogue
            [title_cn] => Vogue
            [description_en] => Vogue adds a video module that can fully display product details. It is a set of templates designed specifically for brand and product stories.
            [description_cn] => Vogue 模板增加了视频模块,可以全面的展示产品细节,是专为品牌和产品故事而设计的一套模板。
            [cover] => http://172.16.12.250:30005/linkun/saas/EXGY62GLqebTEVoEDS19ynVxGmlktoCUFYCZGmom.png
        )

    [venue] => Array
        (
            [id] => 6
            [user_id] => 
            [name] => venue
            [status] => published
            [title_en] => Volume
            [title_cn] => Volume
            [description_en] => Volume uses a clean design style and adds a picture and text module, which is very suitable for high-end brand products.
            [description_cn] => Volume模板使用干净的设计风格,增加了图片文字模块,非常适合高端品牌产品。
            [cover] => http://172.16.12.250:30005/linkun/saas/nPVwmFPGMUvOa8hVvlfabKspjzbQ8C0oQHUrm2YP.png
        )

    [showtime] => Array
        (
            [id] => 5
            [user_id] => 6659
            [name] => showtime
            [status] => published
            [title_en] => showtime
            [title_cn] => showtime
            [description_en] => showtime
            [description_cn] => showtime
            [cover] => http://172.16.12.250:30005/linkun/saas/dKtQRxcmKgbQkyrfstqiWKKtVYQ8zKHNYP6SOh8D.png
        )

    [basic] => Array
        (
            [id] => 4
            [user_id] => 6659
            [name] => basic
            [status] => published
            [title_en] => Basic
            [title_cn] => Basic
            [description_en] => Basic is our iconic template, very versatile and concise, suitable for most styles of shops.
            [description_cn] => Basic 是我们标志性的模板,非常通用以及简洁,适合大部分风格的店铺。
            [cover] => http://172.16.12.250:30005/linkun/saas/r9On4359aPYErGWBikYnJNtd2w7qwrEIefmzs7Qp.png
        )

    [showcase] => Array
        (
            [id] => 3
            [user_id] => 6659
            [name] => showcase
            [status] => published
            [title_en] => showcase
            [title_cn] => showcase
            [description_en] => showcase
            [description_cn] => showcase
            [cover] => http://172.16.12.250:30005/linkun/saas/kAR3fGb1RAWlAoQM88JApY2yg4GJn1ubMX9Duz05.png
        )

    [volume] => Array
        (
            [id] => 2
            [user_id] => 6659
            [name] => volume
            [status] => published
            [title_en] => volume
            [title_cn] => volume
            [description_en] => volume
            [description_cn] => volume
            [cover] => http://172.16.12.250:30005/linkun/saas/kcvOpGTaItgeN0DH7Ogs91LWgWx0jTqD2usTn0Wz.png
        )

    [test] => Array
        (
            [id] => 1
            [user_id] => 6659
            [name] => test
            [status] => published
            [title_en] => test
            [title_cn] => test
            [description_en] => test
            [description_cn] => test
            [cover] => http://172.16.12.250:30005/linkun/saas/1v0wvIy5XQNIo2y4yJE4AycO8zt1jpNawBN14NPu.png
        )

)


3. However, using only the array_column function is sufficient. When column_key is null, the entire array is returned (which is very useful when combined with the index_key parameter to re-index the array). There is no need to use the additional array_combine function.

PHP

$data = $res['body']['data'];
// print_r($data);
// $data = array_combine(array_column($data, 'name'), $data);
$data = array_column($data, null, 'name');
print_r($data);
exit;

4. Check the print results of the multidimensional array before and after indexing. The results match expectations. See Figure 2.

Print results of the multidimensional array before and after indexing. Matches expectations
Figure 2
Plaintext
Array
(
    [athena] => Array
        (
            [id] => 8
            [user_id] => 6659
            [name] => athena
            [status] => published
            [title_en] => Athena
            [title_cn] => 雅典娜
            [description_en] => Athena is a template with massive decoration modules. Its social, festive, interactive elements can make a  strong impact. Meanwhile, it is committed to creating a unique shopping experience on mobile phones. Suitable for all middle and high-end brands.
            [description_cn] => Athena是一款装修模块极其丰富的模板,以社交、节日气氛、页面交互等元素带来极强的冲击,同时致力于打造移动端独特的购物体验,适合中高端品牌站。
            [cover] => http://172.16.12.250:30005/linkun/saas/WA2RbFlR6BxR2Gw6845nWZDtRdsZMZlXbaFBzrl2.png
        )

    [vogue] => Array
        (
            [id] => 7
            [user_id] => 6659
            [name] => vogue
            [status] => published
            [title_en] => Vogue
            [title_cn] => Vogue
            [description_en] => Vogue adds a video module that can fully display product details. It is a set of templates designed specifically for brand and product stories.
            [description_cn] => Vogue 模板增加了视频模块,可以全面的展示产品细节,是专为品牌和产品故事而设计的一套模板。
            [cover] => http://172.16.12.250:30005/linkun/saas/EXGY62GLqebTEVoEDS19ynVxGmlktoCUFYCZGmom.png
        )

    [venue] => Array
        (
            [id] => 6
            [user_id] => 
            [name] => venue
            [status] => published
            [title_en] => Volume
            [title_cn] => Volume
            [description_en] => Volume uses a clean design style and adds a picture and text module, which is very suitable for high-end brand products.
            [description_cn] => Volume模板使用干净的设计风格,增加了图片文字模块,非常适合高端品牌产品。
            [cover] => http://172.16.12.250:30005/linkun/saas/nPVwmFPGMUvOa8hVvlfabKspjzbQ8C0oQHUrm2YP.png
        )

    [showtime] => Array
        (
            [id] => 5
            [user_id] => 6659
            [name] => showtime
            [status] => published
            [title_en] => showtime
            [title_cn] => showtime
            [description_en] => showtime
            [description_cn] => showtime
            [cover] => http://172.16.12.250:30005/linkun/saas/dKtQRxcmKgbQkyrfstqiWKKtVYQ8zKHNYP6SOh8D.png
        )

    [basic] => Array
        (
            [id] => 4
            [user_id] => 6659
            [name] => basic
            [status] => published
            [title_en] => Basic
            [title_cn] => Basic
            [description_en] => Basic is our iconic template, very versatile and concise, suitable for most styles of shops.
            [description_cn] => Basic 是我们标志性的模板,非常通用以及简洁,适合大部分风格的店铺。
            [cover] => http://172.16.12.250:30005/linkun/saas/r9On4359aPYErGWBikYnJNtd2w7qwrEIefmzs7Qp.png
        )

    [showcase] => Array
        (
            [id] => 3
            [user_id] => 6659
            [name] => showcase
            [status] => published
            [title_en] => showcase
            [title_cn] => showcase
            [description_en] => showcase
            [description_cn] => showcase
            [cover] => http://172.16.12.250:30005/linkun/saas/kAR3fGb1RAWlAoQM88JApY2yg4GJn1ubMX9Duz05.png
        )

    [volume] => Array
        (
            [id] => 2
            [user_id] => 6659
            [name] => volume
            [status] => published
            [title_en] => volume
            [title_cn] => volume
            [description_en] => volume
            [description_cn] => volume
            [cover] => http://172.16.12.250:30005/linkun/saas/kcvOpGTaItgeN0DH7Ogs91LWgWx0jTqD2usTn0Wz.png
        )

    [test] => Array
        (
            [id] => 1
            [user_id] => 6659
            [name] => test
            [status] => published
            [title_en] => test
            [title_cn] => test
            [description_en] => test
            [description_cn] => test
            [cover] => http://172.16.12.250:30005/linkun/saas/1v0wvIy5XQNIo2y4yJE4AycO8zt1jpNawBN14NPu.png
        )

)


关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系