2

I have cloned HTML prepared with input group. Some checkbox and radio are not selected. I want to push into the selected email.

I've tried many methods.

array_merge()

array_combine()

array_push()

array_merge_recursive()

But none of these worked. Or I don't know how to get them to work.

HTML

<div class="form-group">
    <label for="name">E-posta</label>
    <div class="input-group">
        <input type="email" class="form-control email-address" name="email[]" placeholder="E-Posta giriniz">
        <div class="btn-group">
            <label class="btn btn-default">
                <input class="primary-radio" type="radio" name="primary[]" checked autocomplete="off"> <span
                    class="fas fa-star"></span>
            </label>
            <label class="btn btn-default">
                <input class="ban-checkbox" type="checkbox" name="ban[]" autocomplete="off"> <span
                    class="fas fa-ban"></span>
            </label>
            <label class="btn btn-default">
                <input class="invalid-checkbox" type="checkbox" name="invalid[]" autocomplete="off"> <span
                    class="fas fa-exclamation-circle"></span>
            </label>
        </div>
    </div>
</div>

PHP

public function add(Request $request)
{
    $emails = $request->input('email');
    $primary = $request->input('primary');
    $ban = $request->input('ban');
    $invalid = $request->input('invalid');

    $emailAddresses = array();

    foreach ($emails as $emailKey => $emailValue) {
        $emailAddresses[$emailKey] = [
            'emailAddress' => $emailValue,
            'invalid' => $invalid,
            'lower' => $emailAddresses,
            'ban' => $ban,
            'primary' => $primary
        ];
    }
    dd($emailAddresses);
}

Output

array:3 [▼
  0 => array:5 [▼
    "emailAddress" => "dodis@live.com"
    "invalid" => null
    "lower" => "dodis@live.com"
    "ban" => array:2 [▼
      0 => "on"
      1 => "on"
    ]
    "primary" => array:1 [▼
      0 => "on"
    ]
  ]
  1 => array:5 [▼
    "emailAddress" => "test@live.com"
    "invalid" => null
    "lower" => "test@live.com"
    "ban" => array:2 [▼
      0 => "on"
      1 => "on"
    ]
    "primary" => array:1 [▼
      0 => "on"
    ]
  ]
  2 => array:5 [▼
    "emailAddress" => "bundayok@live.com"
    "invalid" => null
    "lower" => "bundayok@live.com"
    "ban" => array:2 [▼
      0 => "on"
      1 => "on"
    ]
    "primary" => array:1 [▼
      0 => "on"
    ]
  ]
]

Such an output comes. It shouldn't be like that. Here's the example I want it to be.

0: {
        emailAddress: "bilgi@ekinciler.com.tr"
        invalid: false
        lower: "bilgi@ekinciler.com.tr"
        optOut: false
        primary: true
    }
1: {
        emailAddress: "muhasebe@ekinciler.com"
        invalid: false
        lower: "muhasebe@ekinciler.com"
        optOut: false
        primary: false
    }

I am sorry for my English. I hope I can. I would be glad if you help.

Salim Djerbouh
  • 10,719
  • 6
  • 29
  • 61
Gündoğdu Yakıcı
  • 677
  • 2
  • 10
  • 31
  • 1
    What do you mean about `merge`? You mean you need to transform your array? – nmfzone Nov 05 '19 at 09:17
  • The second array does not look that much different than the first one. As far as I see, the `ban` element vanished and the mail addresses look completely different. How should this be done automatically? – Nico Haase Nov 05 '19 at 09:27

1 Answers1

3

Try below code,

What I have done is added array index to each loop to get correct value of that index, here your

"ban" => array:2 [▼
  0 => "on"
  1 => "on"
]

is array itself so you need 0 index for the first array data. and same applies for all the array.

public function add(Request $request)
{
    $emails = $request->input('email');
    $primary = $request->input('primary');
    $ban = $request->input('ban');
    $invalid = $request->input('invalid');

    $emailAddresses = array();

    $i = 0;

    foreach ($emails as $emailKey => $emailValue) {
        $emailAddresses[$emailKey] = [
            'emailAddress' => $emailValue,
            'invalid' => $invalid[$i]?$invalid[$i]:'',
            'lower' => $emailAddresses,
            'ban' => $ban[$i]?$ban[$i]:'',
            'primary' => $primary[$i]?$primary[$i]:''
        ];
        $i++;
    }

    dd($emailAddresses);
}
Regolith
  • 2,944
  • 9
  • 33
  • 50