0

My arrays result is like this one

Array
(
    [0] => Array
        (
            [id] => Bank Transfer
            [ec] => 1000
            [accounts] => Array
                (
                    [0] => Array
                        (
                            [name] => Account WD
                            [value] => 
                        )

                    [1] => Array
                        (
                            [name] => Keterangan
                            [value] => 
                        )

                )

        )

    [1] => Array
        (
            [id] => Wired
            [ec] => 1001
            [accounts] => Array
                (
                    [0] => Array
                        (
                            [name] => Account WD
                            [value] => 
                        )

                    [1] => Array
                        (
                            [name] => Keterangan
                            [value] => 
                        )

                    [2] => Array
                        (
                            [name] => Account ID
                            [value] => 
                        )

                )

        )

)

It's weird because 2nd array of accounts contains same value as first array.

                    [0] => Array
                        (
                            [name] => Account WD
                            [value] => 
                        )

                    [1] => Array
                        (
                            [name] => Keterangan
                            [value] => 
                        )

How to prevent this duplicated so the 2nd array of accounts will only show

                    [0] => Array
                        (
                            [name] => Account ID
                            [value] => 
                        )

Here's my code

    $arr = $arr_pay = array();
      foreach($site_payment as $key => $value){
        if($value['status'] && $value['ec']>=1000){
          $payment_data_cust = unserialize(crypts($value['auto_wd_data'],'d'));
          foreach ($payment_data_cust as $ke => $va) {
            $arr[] = array("name"=>$va,"value"=>'');
          }
            $spc[] = array(
              "id"=>$value['id'],
              "ec"=>$value['ec'],
              "accounts"=>$arr
            );
        }
      }

Array of $site_payment contains

[Bank Transfer] => Array
    (
        [id] => Bank Transfer
        [ec] => 1000
        [status] => 1
        [auto_wd_data] => IjZRcWp1aGtzNmZHbjVPZTlkeStGZVNPaWdPY0lrZ0UyQnd6eFhxQUZoR1VEeU82TzVJZkdMelJrZzJKS3lxXC9yTm5meFBndFRlUDQ9Ig==
    )
[Dana] => Array
    (
        [id] => Wired
        [ec] => 1001
        [status] => 1
        [auto_wd_data] => IkNDek9IY1BtelVEeFFxZEtMc0hvalBkbVBRdENEZEJWakZoaFBJWkNBUk09Ig==
    )

I want to show the auto_wd_data of $site_payments with different array so it's became the result, but not duplicating in each array Please help me to solve the problem

1 Answer 1

1

Duplication is due to the $arr is not being reset

$arr_pay = array();
      foreach($site_payment as $key => $value){
        $arr = array(); // Resetting
        if($value['status'] && $value['ec']>=1000){
          $payment_data_cust = unserialize(crypts($value['auto_wd_data'],'d'));
          foreach ($payment_data_cust as $ke => $va) {
            $arr[] = array("name"=>$va,"value"=>'');
          }
            $spc[] = array(
              "id"=>$value['id'],
              "ec"=>$value['ec'],
              "accounts"=>$arr
            );
        }
      }
Sign up to request clarification or add additional context in comments.

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.