0

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
  • I don't recommend the naming of 'id:>' Commented Sep 22, 2009 at 16:20

6 Answers 6

4

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.

Sign up to request clarification or add additional context in comments.

Comments

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

$params[] = $element

5 Comments

But i don't know the key and value that i will add.
If your looking for performance, this: $params[] = $element is faster than array_push()
"But i don't know the key and value that i will add." Have you tried $element[$key] = $value?
"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: "$array[(count($array) + 1)]['userclass_id'] = 3" never do that. use the [] operator.
1

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

Comments

1

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;

Comments

1

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

Comments

0

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.

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.