3

I geting from mysql data:

$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;

And I want to add another table in to this array, I trying to do it like that:

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

But it's not working like I want to, becouse var_dump($punkty); showing me

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" 
}
3
  • 1
    Why are you referencing $punkty within your foreach loop? I have never seen this convention before. Commented Jul 27, 2012 at 13:00
  • @Matt Sounds name of his cat! Commented Jul 27, 2012 at 13:01
  • @ariel Also, this is the obligatory note that mysql_* functions are being deprecated. Use PDO (php.net/manual/en/book.pdo.php) or Mysqli (php.net/manual/en/book.mysqli.php) going forward Commented Jul 27, 2012 at 13:04

4 Answers 4

3
    foreach ($punkty as &$item)
    {
        $deserialized = unserialize($item['mapa']);
        $item['long'] = $deserialized['lng'];
        $item['lat'] = $deserialized['lat'];
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Don't forget to unset($item) after the end of the foreach() loop
1

use

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

2 Comments

foreach($punkty as &$item) is shortly
I agree, it's the way i'd do it too. except i like to suggest solutions that the poster is more likely to have seen or understand. Understanding referencing and when to use them when your new to programming can be difficult, so he's more likely to copy and paste your solution and not actually understand what's going on. However i don't disagree its the better way in most situations
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

Comments

0

Your loop is doing what it's being told to do: it's taking the lat and lng fields from each item and writing it to the $punkty array.

I'm under the assumption that you want the lat and lng fields to be in each individual item though, not the top-level of the array. In that case, change the loop to be:

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
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.