Current location: Hot Scripts Forums » Programming Languages » PHP » Serializing a set of arrays


Serializing a set of arrays

Reply
  #1 (permalink)  
Old 10-10-04, 01:07 PM
dannyallen dannyallen is offline
Newbie Coder
 
Join Date: Jun 2004
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Serializing a set of arrays

I am trying to serialize a table from my database, which is named "fcc_templates". Now, I want the output from the database to be like this:


Array
(
[1] => Array
(
[template_1] => Array
(
[tid] => 1
[skinid] => 1
[name] => Installed Correctly
[template] => testing $var
[callname] => install_correct
)
[template_2] => Array
(
[tid] => 1
[skinid] => 1
[name] => Installed Correctly
[template] => testing $var
[callname] => install_correct
)
[template_3] => Array
(
[tid] => 1
[skinid] => 1
[name] => Installed Correctly
[template] => testing $var
[callname] => install_correct
)

)

[2] => Array
(
[template_1] => Array
(
[tid] => 1
[skinid] => 1
[name] => Installed Correctly
[template] => testing $var
[callname] => install_correct
)
[template_2] => Array
(
[tid] => 1
[skinid] => 1
[name] => Installed Correctly
[template] => testing $var
[callname] => install_correct
)
[template_3] => Array
(
[tid] => 1
[skinid] => 1
[name] => Installed Correctly
[template] => testing $var
[callname] => install_correct
)

)

)

Each number [1] etc is a skin set id, and the templates are by their name. Now, I can get my script to output this:

Array
(
[1] => Array
(
[install_correct] => Array
(
[tid] => 1
[skinid] => 1
[name] => Installed Correctly
[template] => testing $var
[callname] => install_correct
)

)

)

But, as you can see, only one template is shown. I want to be able to show all the templates in the [1] array or [2] array or whatever number the skin id is. Here is the code I have so far:

$value = $db->query("SELECT * FROM fcc_templates");
while ($cache1 = $db->fetch_array_assoc($value)){

$newval = array ($cache1[skinid] => array($cache1[callname] => $cache1));

$val .= serialize($newval);

}

echo $val;

echo "<br><br><pre>";
$new1 = unserialize($val);
print_r($new1);
echo "</pre>";

and the serialization seperates each template into its own array. I want each template to be part of the entire skinid array, not it's own big array. Here is what it looks like:


Array
(
[1] => Array
(
[install_correct] => Array
(
[tid] => 1
[skinid] => 1
[name] => Installed Correctly
[template] => testing $var
[callname] => install_correct
)

)

)


Array
(
[1] => Array
(
[next_template] => Array
(
[tid] => 1
[skinid] => 1
[name] => Installed Correctly
[template] => testing $var
[callname] => install_correct
)

)

)

see, each template is in its own array. How can I fix this?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 10-10-04, 01:19 PM
Eclipse's Avatar
Eclipse Eclipse is offline
Coding Addict
 
Join Date: May 2004
Location: Long Island, New York
Posts: 356
Thanks: 0
Thanked 0 Times in 0 Posts
So basically you just need serialized nested arrays?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 10-11-04, 04:04 AM
moronovich moronovich is offline
Junior Code Guru
 
Join Date: Oct 2004
Posts: 460
Thanks: 0
Thanked 0 Times in 0 Posts
hello danny,
from your example i think it will be harder to serialize and unserialize the array. it's because your records in the database is not properly nested.
from your query:
PHP Code:

while ($cache1 $db->fetch_array_assoc($value)){


$newval = array ($cache1[skinid] => array($cache1[callname] => $cache1)); 
for each iteration, it would yield following result:
Code:
$newval = array([skin_id] => array([callname] => array([skin_id] =>skinValue, [tid] =>tvalue, [name] => nameValue, template => templateValue, [callname] => callValue)))
here $newval is an array but its not properly indexed as you want to.

desired array structure:
Code:
$newval[0] = array([skin_id][0] => array([callname][0] => array([skin_id] =>skinValue, [tid] =>tvalue, [name] => nameValue, template => templateValue, [callname] => callValue), [callname][1] => array([skin_id] =>skinValue, [tid] =>tvalue, [name] => nameValue, template => templateValue, [callname] => callValue),[callname][2] => array([skin_id] =>skinValue, [tid] =>tvalue, [name] => nameValue, template => templateValue, [callname] => callValue)))
from your table structure:
[skin_id] is not primary index
which means that you can't automatically assign proper depth of your array since there would be the same level of your array.

solution:
serialization should be done after the loop. PHP function "array_key_exists" would be a nice friend in expanding your array to your desired structure.

example comes later. i'm sure you can solve this problem..
__________________
just an ignorant noob with moronic solution...
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to set usr/pswd for localhost phpMyAdmin steezjjc PHP 3 10-11-04 07:52 AM
Understanding Arrays rjwebgraphix PHP 11 09-27-04 03:06 PM
java set random weight in instance joebloggs2004 Everything Java 1 09-24-04 10:37 AM
Set List Prediction Request ToasterBoy Script Requests 3 09-23-04 02:56 AM
is there command like SET FILTER TO websmart PHP 3 09-07-04 12:53 PM


All times are GMT -5. The time now is 06:43 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.