简体   繁体   English

MySQL将多个结果查询到单个值数组中

[英]MySQL Query multiple results into a single value Array

I have the following array from an API 我从API获得以下数组

$arrs =   array(
    "id"            => $ids,
    "names"         => $names,
    "description"   => $desc,
    "picture_url"   => $p_img;
    );

The API require this information to work unfortunately I can only have one array per request, so I send this question to the developers: 不幸的是,API需要此信息才能工作,每个请求只能有一个数组,因此我将此问题发送给开发人员:

How can I list multiple item such as: 如何列出多个项目,例如:

  $arrs=   array(
"items1" =>   array (
    "id"            => $ids,
    "names"         => $names,
    "description"   => $desc,
    "picture_url"   => $p_img;
    )
"items2" =>   array (
    "id"            => $ids,
    "names"         => $names,
    "description"   => $desc,
    "picture_url"   => $p_img;
    )
"items3" =>   array (
    "id"            => $ids,
    "names"         => $names,
    "description"   => $desc,
    "picture_url"   => $p_img;
    )
));

And they told me that at the moment is not possible, so, the "important" part of this array is the "names", when it is use with a single item there is no problem I get a single name, done, no problem, but what if I have multiple names? 他们告诉我,目前不可能,因此,此数组的“重要”部分是“名称”,当与单个项目一起使用时,没有问题,我得到一个名称,完成了,没问题,但是如果我有多个名字怎么办? I can send multiple request but that will be seen as a flood or something like... just imaging 300 names = 300 request in one second or so... sure I can put a pause per request but is not efficient... 我可以发送多个请求,但是这将被视为洪水或类似...只是在一秒钟左右的时间里成像300个名称= 300个请求...确定我可以为每个请求暂停一下,但效率不高...

the API will read something like this... API会读取类似这样的内容...

"id"            => 654,
"names"         => "John", // <-- Lets look at this...
"description"   => "Fancy desc...",
"picture_url"   => "http"//domain.com/assets/user/654/av_654_dd.jpg";

So before I output the array I have an SQL Query with a while to display the information... 因此,在输出数组之前,我先进行一次SQL查询,以显示信息...

while ($names = $listnames->fetch_assoc()) {echo $names['names']. ', ';}

This will display... John, Karl, Lisa, Mark... so this same structure I'd love to put it into my array... the thing is I can't put a while after the => ... that would be silly and it wont work... 这将显示... John,Karl,Lisa和Mark ...所以我希望将相同的结构放入数组中...问题是我不能在=>之后放置一会儿...那将是愚蠢的,它将无法正常工作...

"id"            => 654,
"names"         => "John, Karl, Lisa, Mark", // <-- Lets look at this...
"description"   => "Fancy desc...",
"picture_url"   => "http"//domain.com/assets/user/654/av_654_dd.jpg";

if I need only one name then there is not problem... but in this case I need to put all of the name as a value, so, how can get the result from a WHILE loop.... so that I can use that result elsewhere... 如果我只需要一个名称,那么就没有问题...但是在这种情况下,我需要将所有名称都放在一个值中,因此,如何从WHILE循环中获取结果...。以便可以使用结果在其他地方...

Thank you for taking the time.. 谢谢您抽出宝贵的时间。

while ($names = $listnames->fetch_assoc()) { 
     $name_array[] = $names['names'];
}

$arrs=array(
"items1" =>   array (
    "id"            => $ids,
    "names"         => implode(', ', $name_array),
    "description"   => $desc,
    "picture_url"   => $p_img;
    )
);

We don't know how you access to the API. 我们不知道您如何访问API。 If this is a REST API, you are able to do only what the developers planned. 如果这是REST API,则您只能执行开发人员计划的工作。 If this is a framework or another library, you may edit it. 如果这是框架或其他库,则可以对其进行编辑。

And you should receive your results like theses: 并且您应该收到如下结果:

$arrs =   array(
    array(
        "id"            => 1,
        "names"         => 'MyName',
        "description"   => 'MyDesc',
        "picture_url"   => 'MyPic',
    ),
    array(
        "id"            => 2,
        "names"         => 'MyName',
        "description"   => 'MyDesc',
        "picture_url"   => 'MyPic',
    ),
);

So if you have full access to the SQL results, to get all results' data, you could do: $results = array(); 因此,如果您具有对SQL结果的完全访问权限,以获取所有结果的数据,则可以执行以下操作:$ results = array(); while( $row = $listnames->fetch_assoc() ) { $results[] = $row; while($ row = $ listnames-> fetch_assoc()){$ results [] = $ row; } }

If you want to set several possible name in input, the developers should develop that is possible with array of values. 如果要在输入中设置多个可能的名称,则开发人员应使用值数组进行开发。 For names, they just have to code: "LIKE '".implode("', LIKE '", $names)."'" They could also add '%' to allow more values, eg 'John' for 'Johnny'. 对于名称,他们只需要编码:“ LIKE'” .implode(“',LIKE'”,$ names)。“'”“他们还可以添加'%'以允许更多值,例如'John'为'Johnny' 。

I think we need more informations to really help you. 我认为我们需要更多信息才能真正为您提供帮助。

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

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