我如何将此数组内嵌到url字符串[重复]

问题描述 投票:0回答:1

我有一个像这样的数组:

$aSample = [
   'productName' => [
        'sample',
        'sample2'
    ],

   'productNumber' => [
        'numberOne',
        'numberTwo'
    ]
];

如何使该数组看起来像这样:

productName=sample,sample2&productNumber=numberOne,numberTwo

谢谢您的帮助!

php arrays laravel url multidimensional-array
1个回答
0
投票

我认为这会起作用

$aSample = [
    'productName' => [
         'sample',
         'sample2'
     ],

    'productNumber' => [
         'numberOne',
         'numberTwo'
     ]
 ];

 $array = [];
 foreach($aSample as $key=>$item) {

    $arr = $key.'='.implode(',',$item);


    array_push($array,$arr);

}
echo implode('&',$array);

© www.soinside.com 2019 - 2024. All rights reserved.