0

I would like to add an array to within an existing array. I am tryin to use array_push which works as long as i dont try to assign a key to the array (if i try to add a key i get a syntax error... :-()

This is my initial array:

$ResultArray = array(
    "TransactionDate" => "$TransactionDate",  
    "tx"=>array(
        "0"=>array(
            "TxIndex" => "$TxIndex", 
            "value" => "$Value",  
            "PaymentConfirmedCount" => "$PaymentConfirmedCount"
        ),
        "1"=>array(
            "TxIndex" => "$TxIndex", 
            "value" => "$Value",  
            "PaymentConfirmedCount" => "$PaymentConfirmedCount"
        )
     ) 
 );

i would then like to add:

$ArrayTOAdd = array(
    "0"=>array(
        "TxIndex" => "$TxIndex", 
        "value" => "$Value",  
        "PaymentConfirmedCount" => 
        "$PaymentConfirmedCount"
    )
);

if I try:

array_push($ResultArray->tx, $ArrayTOAdd); 

BUT this does not work and results in a warning of "array_push() [function.array-push]: First argument should be an array"

if i try this :

array_push($ResultArray, $ArrayTOAdd); 

it just adds the array but not to $ResultArray->tx

Any suggestions would be greatly welcomed!

1 Answer 1

4

You have to access the element in the array with $ResultArray["tx"] and not $ResultArray->tx. The second one is for the access to members in a php class. So an

array_push($ResultArray["tx"], $ArrayTOAdd);

should work.

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

2 Comments

Perfect, thanks! (i was converting the arrays to objects) This seems to work now '$ResultArray = (object)array("TransactionDate" => "$TransactionDate", "tx"=>array((object)array("TxIndex" => "$TxIndex", "value" => "$Value", "PaymentConfirmedCount" => "$PaymentConfirmedCount"),(object)array("TxIndex" => "$TxIndex", "value" => "$Value", "PaymentConfirmedCount" => "$PaymentConfirmedCount")) ); $ArrayTOAdd = (object)array("TxIndex" => "$TxIndex", "value" => "$Value", "PaymentConfirmedCount" => "$PaymentConfirmedCount"); array_push($ResultArray->tx, $ArrayTOAdd);'
sorry for the messy comment and original post!

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.