array_push confusion adding extra content

71 views Asked by At

I am trying to add some information to an array that I am working on. I found some information on this site that has allowed me to do it ( array_push() with key value pair ) but I am getting the information in the array twice and I don't understand why.

here is my array

array_push($networks[0], $networks[0]['class']='fa fa-facebook');
print_r($networks);


        array(4) {
[0]=>
array(5) {
["name"]=>
string(1) "3"
["url"]=>
string(14) "facebook.com/#"
["icon_title"]=>
string(8) "facebook"
["class"]=>
string(14) "fa fa-facebook"
[0]=>
string(14) "fa fa-facebook"
}

here is what I want to get

        array(4) {
[0]=>
array(4) {
["name"]=>
string(1) "3"
["url"]=>
string(14) "facebook.com/#"
["icon_title"]=>
string(8) "facebook"
["class"]=>
string(14) "fa fa-facebook"
}

as you can see. The array_push is adding a second string to the end with the fa fa-facebook info. I don't wan that there. Just the last bit should be there.

 ["class"]=> string(14) "fa fa-facebook"
1

There are 1 answers

0
Nigel Ren On BEST ANSWER

Whats happening is that as part of your array_push() your actually doing the assignment you need.

array_push($networks[0], $networks[0]['class']='fa fa-facebook');

So this is doing an assignment...

$networks[0]['class']='fa fa-facebook'

And then the result (the value of the assignment) is being added to the array.

So just use

 $networks[0]['class']='fa fa-facebook';
 print_r($networks);