...

View Full Version : Insert an associative array into another array



Hayyel
03-18-2009, 02:23 AM
I have been slamming my head against a wall.. how do I turn this:


Array
(
[note] =>
[careerID] => 20
[lastLogin] => 1231125665
[bearerStatus] => 0
[lastLoginNumberHour24] => 22
[dept] => 0
[lastLoginNumberDay] => 4
[statusNumber] => 1
[lastLoginNumberMonth] => 1
[lastLoginNumberYear] => 2009
[memberID] => 28178

)

Array
(
[note] =>
[careerID] => 20
[lastLogin] => 1224125665
[bearerStatus] => 0
[lastLoginNumberHour24] => 22
[dept] => 0
[lastLoginNumberDay] => 4
[statusNumber] => 1
[lastLoginNumberMonth] => 1
[lastLoginNumberYear] => 2009
[memberID] => 28178

)

into this:


Array (
[1] => Array
(
[note] =>
[careerID] => 20
[lastLogin] => 1231125665
[bearerStatus] => 0
[lastLoginNumberHour24] => 22
[dept] => 0
[lastLoginNumberDay] => 4
[statusNumber] => 1
[lastLoginNumberMonth] => 1
[lastLoginNumberYear] => 2009
[memberID] => 28178

)
Array (
[2] => Array
(
[note] =>
[careerID] => 20
[lastLogin] => 1224125665
[bearerStatus] => 0
[lastLoginNumberHour24] => 22
[dept] => 0
[lastLoginNumberDay] => 4
[statusNumber] => 1
[lastLoginNumberMonth] => 1
[lastLoginNumberYear] => 2009
[memberID] => 28178

)

Php manual does not seem t discuss this that I can find.

oesxyl
03-18-2009, 02:35 AM
$a1 = array( [note] =>
[careerID] => 20
[lastLogin] => 1231125665
[bearerStatus] => 0
[lastLoginNumberHour24] => 22
[dept] => 0
[lastLoginNumberDay] => 4
[statusNumber] => 1
[lastLoginNumberMonth] => 1
[lastLoginNumberYear] => 2009
[memberID] => 28178 );
$a2 = array( [note] =>
[careerID] => 20
[lastLogin] => 1224125665
[bearerStatus] => 0
[lastLoginNumberHour24] => 22
[dept] => 0
[lastLoginNumberDay] => 4
[statusNumber] => 1
[lastLoginNumberMonth] => 1
[lastLoginNumberYear] => 2009
[memberID] => 28178 );
$aa1[1] = $a1;
$aa2[2] = $a2;


best regards

Hayyel
03-18-2009, 03:16 AM
I kind of understand what you are saying.. still cloudy though.

I am currently messing around with:


$query = "SELECT * FROM current_roster";

$result = mysql_query($query) or die(mysql_error());
while($current= mysql_fetch_assoc($result)) {
$roster=array($current);
echo "<pre>";
print_r($roster);
echo "</pre>";
}

The above works perfect except the key for the first array is always [0].


Array
(
[0] => Array
(
[note] =>
[careerID] => 20
[lastLogin] => 1231125665
[bearerStatus] => 0
[lastLoginNumberHour24] => 22
[dept] => 0
[lastLoginNumberDay] => 4
[statusNumber] => 1
[lastLoginNumberMonth] => 1
[lastLoginNumberYear] => 2009
[memberID] => 28178 )

)

Array
(
[0] => Array
(
[note] =>
[careerID] => 20
[lastLogin] => 1224125665
[bearerStatus] => 0
[lastLoginNumberHour24] => 22
[dept] => 0
[lastLoginNumberDay] => 4
[statusNumber] => 1
[lastLoginNumberMonth] => 1
[lastLoginNumberYear] => 2009
[memberID] => 28178
)

)

oesxyl
03-18-2009, 04:22 AM
you define a new $roster each time and because array index start from 0, will be 0

$query = "SELECT * FROM current_roster";

$result = mysql_query($query) or die(mysql_error());
while($current= mysql_fetch_assoc($result)) {
$roster=array($current);
echo "<pre>";
print_r($roster);
echo "</pre>";
}

this will build a array of arrays which start from index 0

$query = "SELECT * FROM current_roster";

$result = mysql_query($query) or die(mysql_error());
$roster = array();
while($current= mysql_fetch_assoc($result)) {
$roster[] =array($current);
}
echo "<pre>";
print_r($roster);
echo "</pre>";


and this same thing starting from 1 or any other value you want

$query = "SELECT * FROM current_roster";

$result = mysql_query($query) or die(mysql_error());
$roster = array();
$pos = 1;
while($current= mysql_fetch_assoc($result)) {
$roster[$pos++] =array($current); // force key to be $pos and increment
}
echo "<pre>";
print_r($roster);
echo "</pre>";


best regards

Hayyel
03-18-2009, 01:41 PM
As always oesxyl thanks for your help.

I ended up with this after trying your suggestion and seeing the result.


$query = "SELECT * FROM current_roster";

$result = mysql_query($query) or die(mysql_error());
$roster = array();
$pos = 1;
while($current= mysql_fetch_assoc($result)) {
$roster[$pos++] =$current; // force key to be $pos and increment
}
echo "<pre>";
print_r($roster);
echo "</pre>";

Is there anything wrong with this code?

I am hoping that since it is in the same format as my other array I can run a array_diff_assoc .

oesxyl
03-18-2009, 09:43 PM
As always oesxyl thanks for your help.

I ended up with this after trying your suggestion and seeing the result.


$query = "SELECT * FROM current_roster";

$result = mysql_query($query) or die(mysql_error());
$roster = array();
$pos = 1;
while($current= mysql_fetch_assoc($result)) {
$roster[$pos++] =$current; // force key to be $pos and increment
}
echo "<pre>";
print_r($roster);
echo "</pre>";

Is there anything wrong with this code?
no, that if I really understand what you want.


I am hoping that since it is in the same format as my other array I can run a array_diff_assoc .
I don't understand what you mean, can you give some examples or samples and details?

best regards

Hayyel
03-19-2009, 03:09 PM
What I am ultimately trying to do is compare $roster, the array made from the query, and $finalarr, the array comprising of the new data being inserted into the table $roster came from. I need to pull out of $roster all of the members that are no longer members.

array_dif does not do multidimensional arrays... so I am looking for a way to walk the arrays if you will. I have tried various methods but none seem to work correctly.

Currently I am looking at:


//Compare Arrays
function my_serialize(&$arr,$pos){
$arr = serialize($arr);
}

function my_unserialize(&$arr,$pos){
$arr = unserialize($arr);
}

//make a copy
$first_array_s = $roster;
$second_array_s = $finalarr;

// serialize all sub-arrays
array_walk($first_array_s,'my_serialize');
array_walk($second_array_s,'my_serialize');

// array_diff the serialized versions
$diff = array_diff($first_array_s,$second_array_s);

// unserialize the result
array_walk($diff,'my_unserialize');

echo "<pre>";
print_r($diff);
echo "</pre>";
With this $diff == $finalarr which is not correct.

oesxyl
03-20-2009, 08:08 AM
in mysql, that table, do you have a field which is uniq, a primary key? in this case you can use it to build a array with it's value as elements and then use array_filter, array_diff or other things to do what you want.

best regards



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum