简体   繁体   English

PHP从多维数组创建查询字符串而不使用循环

[英]PHP Create querystring from multidimensional array without using loop

I have a multidimensional array. 我有一个多维数组。 Like this. 像这样。

Array
(
    [38] => Array
        (
            [quantity] => 1
            [price] => 149
            [product_code] => 4578425
        )

    [39] => Array
        (
            [quantity] => 2
            [price] => 300
            [product_code] => 4578426 
        )

)

I want to create query string from these values like 我想从这些值创建查询字符串,例如

https://www.domain.com/checkout.php?PRODS=4578425,4578426&QTY=1,2&CART=1 https://www.domain.com/checkout.php?PRODS=4578425,4578426&QTY=1,2&CART=1

Without using loops... 不使用循环...

I don't think AFAIK, it is possible, since you have arrays in array, so using implode won't help. 我认为AFAIK不可能,因为数组中有数组,因此使用implode将无济于事。 But, using loops, yea. 但是,使用循环,是的。

Use this code: 使用此代码:

$prods = array();
$qty = array();
foreach ($array as $item)
{
    $prods[] = $item["product_code"];
    $qty[] = $item["quantity"];
}
echo 'https://www.domain.com/checkout.php?PRODS=', implode(',', $prods),'&QTY=', implode(',', $qty),'&CART=1';

you can use implode() method 您可以使用implode()方法

$array = array('lastname', 'email', 'phone');
$comma_separated = implode(",", $array);
echo $comma_separated; // lastname,email,phone

Yes i think there is a way, 是的,我认为有办法

You can use serialize to put it in a string and then unserialize to get it back to an array like this: 您可以使用序列化将其放入字符串中,然后反序列化以将其返回到这样的数组中:

<?php

 $arr = Array
(
38 => Array
    (
        'quantity' => 1,
        'price' => 149,
        'product_code' => 4578425
    ),

39 => Array
    (
        'quantity' => 2,
        'price' => 300,
        'product_code' => 4578426 
    )

);


$newarr = 'https://www.domain.com/checkout.php?string=';
$newarr .= serialize($arr);

?>

then you have this result: 那么您将得到以下结果:

https://www.domain.com/checkout.php?string=a:2 :{i:38;a:3:{s:8:"quantity";i:1;s:5:"price";i:149;s:12:"product_code";i:4578425;}i:39;a:3:{s:8:"quantity";i:2;s:5:"price";i:300;s:12:"product_code";i:4578426;}}a:2:{i:38;a:3:{s:8:"quantity";i:1;s:5:"price";i:149;s:12:"product_code";i:4578425;}i:39;a:3:{s:8:"quantity";i:2;s:5:"price";i:300;s:12:"product_code";i:4578426;}} https://www.domain.com/checkout.php?string=a:2:{i:38 ; a:3:{s:8:“ quantity”; i:1; s:5:“ price”; i:149; s:12:“ product_code”; i:4578425;} i:39; a:3:{s:8:“数量”; i:2; s:5:“价格”; i:300; s:12:“ product_code”; i:4578426;}} a:2:{i:38; a:3:{s:8:“ quantity”; i:1; s:5:“ price”; i: 149; s:12:“ product_code”; i:4578425;} i:39; a:3:{s:8:“ quantity”; i:2; s:5:“ price”; i:300; s: 12:“ product_code”; i:4578426;}}

No loops but it is not pretty!!! 没有循环,但不是很漂亮!!!

If you wish to use this inside url I have to warn you. 如果您想在url中使用此代码,则必须警告您。 The url get method is only intended for short information like id's or other key values. url get方法仅适用于简短信息,例如id或其他键值。 If your url gets over 2000 characters most web-servers would have problems with it. 如果您的网址超过2000个字符,则大多数网络服务器都会遇到问题。 Not sure if that was your intention. 不知道这是否是您的意图。

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

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