tags:

views:

135

answers:

6

I need to have this output

Array
(
   [id:>] => 0
   [isonline] => 0
   [userclass_id] => 3
)

and I am getting this output with this code:

$pr = array('id:>'=>$id,'isonline'=>'0','userclass_id'=>'3');
print_r($params);

This is good. But i want to pop new elements to array one by one. While i am using this code:

$params = array();
array_push($params,array('userclass_id'=>'3'));  //  Members
array_push($params,array('isonline'=>'0')); // Online
array_push($params,array('id:>'=>$id));

I am getting this output:

Array
(
[0] => Array
    (
        [userclass_id] => 3
    )

[1] => Array
    (
        [isonline] => 0
    )

[2] => Array
    (
        [id:>] => 0
    )
)

I want to dynamically add new element but also want to have first output. Thanks for your helps.

+1  A: 
$element = array();
$element['userclass_id'] = 3;
$element['isoline'] = 0;
$element['id:>'] = $id;

$params[] = $element
erenon
But i don't know the key and value that i will add.
uzay95
If your looking for performance, this: $params[] = $element is faster than array_push()
Phill Pafford
"But i don't know the key and value that i will add." Have you tried `$element[$key] = $value`?
Adam Backstrom
"But i don't know the key and value that i will add." You can count the elements in the array, using count($array) and then add 1 to it.So $array[(count($array) + 1)]['userclass_id'] = 3; and so on.
Sbm007
@Sbm007: "$array[(count($array) + 1)]['userclass_id'] = 3" never do that. use the [] operator.
erenon
+1  A: 

You are using array_push which adds elements to the end of the array. You should be using array_unshift, which will prepend new items to the beginning of the array.

When programatically relying on array order in php... array_pop, array_push, array_shift, and array_unshift are your friends. Know them... love them...

php manual entry for array_unshift

ftrotter
+3  A: 

The order of key values in an associative array don't matter. Therefore you don't need to array_push or array_unshift. The only thing you need to do is associate a key to the value you want (or vice-versa). So you would write the following to dynamically add values to the $params array.

$key = "my_id:2";
$value = "23";
$params[$key] = $value;

Yep, no shifting, or pushing... just add the $key between the square brackets and your all good.

null
+1  A: 

erenon's answer is probably better, but if you really want to add the sub-array one element at a time:

$params = array();
array_unshift($params,array('userclass_id'=>'3'));  //  Members
$params[0]['isonline'] = 0;                         //  Online
$params[0]['id:>'] = $id;

This works because array_unshift puts elements at the beginning of the array, therefore the new sub-array will always be element 0.

For performance reasons, you may wish to do this instead, so that the elements are not being constantly renumbered:

$params = array();
$params[] = array('userclass_id'=>'3');  //  Members
end($params); // Move to the new element
$key = key($params); // Get the key of the new element
$params[$key]['isonline'] = 0;           //  Online
$params[$key]['id:>'] = $id;
R. Bemrose
A: 

How about just

$aParams[] = array(
    'userclass_id' => 3,
    'isoline' => 0,
    'id:>' => $id
);

I'm a big fan of assigning all the values in an array entry this way, vs. individual assignment statements.

grantwparks
+1  A: 

What was happening is that you were pushing your a new array onto the current array instead of merging your new array into your old array. $params = array();

$params = array_merge($params, array('userclass_id' => 3));  //  Members
$params = array_merge($params, array('isonline'=>'0')); // Online
$params = array_merge($params, array('id:>' => $id));

Or even better yet you can just call array_merge once and pass it all the values

$params = array_merge($params, array('userclass_id' => 3), array('isonline' => '0'), array('id:>' => $id));

Another way to get your desired output would be just assigning the keys directly on the array.

$params = array();
$params['userclass_id'] = 3;
$params['isoline'] = 0;
$params['id:>'] = $id;

And probably the best way of doing things is just to assign the keys when the array is created.

$params = array(
    'userclass_id' => 3,
    'isoline' => 0,
    'id:>' => $id
);
ajcates