数组的值更改为 sql 或数组直接更改为每个索引列中的 sql =>结果

发布于 2024-11-24 15:56:20 字数 820 浏览 3 评论 0原文

好吧,我找到了如何打印我的全局数组,效果很好,但现在我有一个问题,我想将该数组打印到 sql 中,

代码:

print_r($_SESSION['cart']);

返回:

Array ( [1] => 2 [2] => 1 [3] => 1 )

那太好了,我确切地知道选择了什么,但用户不知道...

所以我的问题是我如何在sql数据库中打印该数组,但更改索引

示例:

Array ( [1] => 2 [2] => 1 [3] => 1 )

for

Array ( [name1] => 2 [name2] => 1 [name3] => 1 )

仅当索引[x]存在于当前数组结果中时才基于

[name1] => 2
[name2] => 1
[name3] => 1

,因此在数据库中将看起来像这样:我可以只需回拨该线路即可,或者如何在不同列上打印同一个数组

示例:

from array : Array ( [1] => 2 [2] => 1 [3] => 1 )
to sql :

id    quantity
[1]     2
[2]     1
[3]     1

是否可以以这种方式在 sql 中打印数组?以及如何

提前做到这一点

Well i found out how to print my global arrays, works great but now i have a problem, i want to print that array into sql

the code :

print_r($_SESSION['cart']);

returns :

Array ( [1] => 2 [2] => 1 [3] => 1 )

thats great, i know exactly what has been select, but the user wont know that...

so my question its how can i print that array in sql database but changing the index

example :

Array ( [1] => 2 [2] => 1 [3] => 1 )

for

Array ( [name1] => 2 [name2] => 1 [name3] => 1 )

based only if the index [x] exist in the current array results so in the database will look somehting like this :

[name1] => 2
[name2] => 1
[name3] => 1

and i can just call that line back, or how to print on diferent columns the same array

example :

from array : Array ( [1] => 2 [2] => 1 [3] => 1 )
to sql :

id    quantity
[1]     2
[2]     1
[3]     1

is it posible to print the array in sql in this way ? and how to do that

Ty in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

随梦而飞# 2024-12-01 15:56:20

您可以从 sql 获取数据:

$id2name=array();
$x=mysql_query("SELECT id,name FROM goods WHERE id IN(".implode(',',array_keys($array)).")");
while($y=mysql_fetch_assoc($x)){
    $id2name[$y['id']]=$y['name'];
}

您可以这样打印:

foreach($array as $k=>$v){
    print "[".$id2name[$k]."]\t".$v."\n";
}

...我想更改名称的索引。 很简单:

foreach($array as $k=>$v)
   $new_arr[get_name($k)]=$v; //where get name = some function, that get name from id(you can get as I see in your site)

保存代码

$str="";
foreach($array as $k=>$v){
    $str.="[".$k."] ".$v."<br/>";
}
//now you can use $str

You can get data from sql:

$id2name=array();
$x=mysql_query("SELECT id,name FROM goods WHERE id IN(".implode(',',array_keys($array)).")");
while($y=mysql_fetch_assoc($x)){
    $id2name[$y['id']]=$y['name'];
}

You can print this way:

foreach($array as $k=>$v){
    print "[".$id2name[$k]."]\t".$v."\n";
}

...and i want to change the index for the name. It's easy

foreach($array as $k=>$v)
   $new_arr[get_name($k)]=$v; //where get name = some function, that get name from id(you can get as I see in your site)

code for saving:

$str="";
foreach($array as $k=>$v){
    $str.="[".$k."] ".$v."<br/>";
}
//now you can use $str
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文