【问题标题】:php array combine 3 arrays [closed]php数组组合3个数组[关闭]
【发布时间】:2017-08-30 08:41:51
【问题描述】:

将得到 3 个数组 $_POST 数组。

$_POST["name"]=> array (
    [0] => product_1
    [1] => product_2
    [2] => product_3
)


$_POST["content"]=> array (
    [0] => content_1
    [1] => content_2
    [2] => content_3
)


$_POST["price"]=> array (
    [0] => 100
    [1] => 200
    [2] => 300
)

我需要获取它并添加到 mysql。所以,我认为需要将 3 个数组合并为一个数组。

所以我需要这种格式?我该怎么办? :

$array =>(
    [0] => (product_1,content_1,100)
    [1] => (product_2,content_2,200)
    [2] => (product_3,content_3,300)
)

我试过array_combine(),但那只是支持两个数组

【问题讨论】:

  • 你有没有尝试过?还是只想要代码?
  • 我试过array_combine(),但那只是支持两个数组
  • 然后使用简单的foreach。
  • 或者调整你的表格,这样你就可以直接在$_POST...

标签: php arrays


【解决方案1】:

简单使用foreach

$new_array=array(); //create new array 

foreach($_POST['name'] as $key=>$val){ //loop any one of post 

 $new_array[]=array($_POST['name'][$key],$_POST['content'][$key],$_POST['price'][$key]); //create new array with the help of $key

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-28
    • 2018-11-12
    • 1970-01-01
    • 1970-01-01
    • 2016-03-08
    相关资源
    最近更新 更多