简体   繁体   English

从sql查询中组合数组

[英]combining arrays from sql query

i know this have been asked here for many times but i really couldn't figure out how to do this. 我知道这里已经多次被问过,但我真的无法弄清楚如何做到这一点。 im getting this from wordpress sql request 我从wordpress sql请求得到这个

$name = $wpdb->get_results("SELECT Chipset FROM gpu", ARRAY_N);

Array
(
    [0] => Array
        (
            [0] => GeForce GTX 670
        )

    [1] => Array
        (
            [0] => fasdasd
        )

    [2] => Array
        (
            [0] => test
        )

    [3] => Array
        (
            [0] => GeForce GTX 670
        )

    [4] => Array
        (
            [0] => GeForce GTX 560 Ti
        )

)

and for price 并为价格

$price = $wpdb->get_results("SELECT Price FROM gpu", ARRAY_N);

Array
(
    [0] => Array
        (
            [0] => 403
        )

    [1] => Array
        (
            [0] => 50
        )

    [2] => Array
        (
            [0] => 288
        )

    [3] => Array
        (
            [0] => 288
        )

    [4] => Array
        (
            [0] => 288
        )

)

i need to create this format of array 我需要创建这种格式的数组

Array
(
    [0] => Array
        (
            [text] => GeForce GTX 670
            [value] => 403 
        )

    [1] => Array
        (
            [text] => fasdasd
            [value] => 50
        )

    [2] => Array
        (
            [text] => test
            [value] => 288
        )

)

If you know how to get results from two columns in one query and work with that array you can. 如果您知道如何从一个查询中的两列获取结果并使用该数组,则可以。 Also i wanna ask you guys if you could recommend me some good learning material where can i learn this type of combining arrays or how to use bunch of foreach's because i can't get my head around this 另外我想问你们,如果你能推荐我一些很好的学习资料,我可以在哪里学习这种类型的组合阵列或如何使用一堆foreach的,因为我无法理解这个

Ok solution to my problem is to change sql query but i want to know how to combine these arrays anyway for future. 好的解决我的问题的方法是更改​​sql查询但我想知道如何组合这些数组以备将来使用。

All you need to do is SELECT Chipset, Price FROM gpu to fetch both columns in a single query. 您需要做的就是SELECT Chipset, Price FROM gpu以在单个查询中获取两列。 Since you're working with Wordpress, a good place to start is just looking through their documentation . 既然您正在使用Wordpress,那么一个好的起点就是浏览他们的文档 It's going to be helpful to get a good grasp on SQL as well. 掌握SQL也很有帮助。 I'd recommend Tizag's MySQL Tutorial for starters. 我推荐Tizag的MySQL教程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM