3

我从mysql数据中得到:

$starcik_o = mysql_query("select id, mapa, miasto, nazwa_obiektu from oferty where id = 28");
        while($data = mysql_fetch_array($starcik_o, MYSQL_ASSOC)) $punkty[] = $data;

我想在这个数组中添加另一个表,我试图这样做:

foreach ($punkty as $item)
        {
        $deserialized = unserialize($item['mapa']);
        $punkty['long'] = $deserialized['lng'];
        $punkty['lat'] = $deserialized['lat'];
        }

但它不像我想要的那样工作,因为 var_dump($punkty); 给我看

array(3) 
{ 
    [0]=> array(4) 
    { 
    ["id"]=> string(2) "28" 
    ["mapa"]=> string(97) "a:3:{s:3:"lat";s:17:"49.21103723075132";s:3:"lng";s:18:"22.330280542373657";s:4:"zoom";s:2:"17";}" 
    ["miasto"]=> string(5) "Cisna" 
    ["nazwa_obiektu"]=> string(44) "Cisna - noclegi u Mirosławy w Bieszczadach" 
    } 
    ["long"]=> string(18) "22.330280542373657" 
    ["lat"]=> string(17) "49.21103723075132" 
}
4

4 回答 4

3
    foreach ($punkty as &$item)
    {
        $deserialized = unserialize($item['mapa']);
        $item['long'] = $deserialized['lng'];
        $item['lat'] = $deserialized['lat'];
    }
于 2012-07-27T12:59:47.800 回答
1

采用

foreach ($punkty as $key => $item)
        {
        $deserialized = unserialize($item['mapa']);
        $punkty[$key]['long'] = $deserialized['lng'];
        $punkty[$key]['lat'] = $deserialized['lat'];
        }
于 2012-07-27T13:00:44.490 回答
1
$starcik_o = mysql_query("select id, mapa, miasto, nazwa_obiektu from oferty where id = 28");
            while($data = mysql_fetch_array($starcik_o, MYSQL_ASSOC))
{
$punkty1[] = $data[' id'];
$punkty2[] = $data['mapa'];
$punkty3[] = $data['miasto'];
$punkty4[] = $data['nazwa_obiektu'];
.....
}
try this code
于 2012-07-27T13:01:16.287 回答
0

你的循环正在做它被告知要做的事情:它从每个项目中获取latandlng字段并将其写入$punkty数组。

我假设您希望latandlng字段位于每个单独的项目中,而不是数组的顶层。在这种情况下,将循环更改为:

foreach ($punkty as &$item) {   // added the '&' to reference the item
    $deserialized = unserialize($item['mapa']);
    $item['long'] = $deserialized['lng'];   // saving "long" to item
    $item['lat'] = $deserialized['lat'];    // saving "lat" to item
}
于 2012-07-27T13:00:56.747 回答