我有一个像这样的多维数组
Array =>
[0] => Array
(
[address] => Zaa 6
[category_ids] => Array
(
[0] => 100
[1] => 101
)
[category_labels] => Array
(
[0] => Array
(
[0] => value1
[1] => value2
[2] => value3
[3] => value4
)
[1] => Array
(
[0] => value5
[1] => value6
[2] => value7
)
)
[city] => gg
[lat] => 37.964652
[lng] => 23.708208
[name] => New Place
[1]=> Array the same as above
每个数组都是一条记录。
我想将所有元素放入 mysql 数据库中。但在category_ids 列中,我想插入“100, 101”,在category_labels 中,我只想插入每个数组的最后一个值,例如“value4, value7”。我怎样才能做到这一点?
我知道我可以使用 end()
、count()
和 implode()
但我不知 Prop 体如何使用。
最佳答案
<?php
$data=array(array(
"address" => "Zaa",
"category_ids" => array(100,101),
"category_labels" => array(array("value1","value2","value3","value4"),array("value5","value6","value7")),
"city" => "gg",
"lat" => "37.964652",
"lng" => "23.708208",
"name" => "New Place"));
foreach($data as $row)
{
echo $row['address']."<br>";
$ids=implode(",",$row['category_ids']);
echo $ids."<br>";
$l_array=array();
foreach($row["category_labels"] as $cat_label)
{
$count_l=count($cat_label);
array_push($l_array,$cat_label[$count_l -1]);
}
echo implode(",",$l_array)."<br>";
echo $row['city']."<br>";
echo $row['lat']."<br>";
echo $row['lng']."<br>";
echo $row['name']."<br>";
//write your query here by passing above parameters
}
?>
关于PHP:在mysql数据库中插入多维数组,仅特定值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41825178/