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" => "[email protected]"
    "invalid" => null
    "lower" => "[email protected]"
    "ban" => array:2 [▼
      0 => "on"
      1 => "on"
    ]
    "primary" => array:1 [▼
      0 => "on"
    ]
  ]
  1 => array:5 [▼
    "emailAddress" => "[email protected]"
    "invalid" => null
    "lower" => "[email protected]"
    "ban" => array:2 [▼
      0 => "on"
      1 => "on"
    ]
    "primary" => array:1 [▼
      0 => "on"
    ]
  ]
  2 => array:5 [▼
    "emailAddress" => "[email protected]"
    "invalid" => null
    "lower" => "[email protected]"
    "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: "[email protected]"
        invalid: false
        lower: "[email protected]"
        optOut: false
        primary: true
    }
1: {
        emailAddress: "[email protected]"
        invalid: false
        lower: "[email protected]"
        optOut: false
        primary: false
    }

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

2
  • 1
    What do you mean about merge? You mean you need to transform your array? Commented Nov 5, 2019 at 9: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? Commented Nov 5, 2019 at 9:27

1 Answer 1

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);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for help but get this error "ErrorException Undefined offset: 1"
Okay, I figured it out. It is necessary to use the isset function. @Regolith

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.