2

I have a form that is built dynamically. Here's the form input

echo'<input class="input stickyinput" type="number"  name="'.$pestname['scoutlogpestname'].'#'.$obj['card_id'].'" >

The $pestname['scoutlogpestname'] can always be different. The $obj['card_id'] can be any value from 1-50. I placed the # in the name figuring I would need a delimiter to explode with. The array that prints looks like this. Numbers for any values entered and blank for any not entered.

Array
(
    [Aphids#1] => 11
    [Thrips#1] => 5
    [White-Fly#1] => 7
    [Aphids#2] => 
    [Thrips#2] => 1
    [White-Fly#2] => 22
    [Aphids#3] => 4
    [Thrips#3] => 1
    [White-Fly#3] => 
    etc....... possibly to 50
)

Can somebody please give me some insight on how to execute the explode loop so I can process the $pestname['scoutlogpestname'] and the $obj['card_id'] values? Thanks for looking.

3 Answers 3

4

Lose that weird hash thing and just use array notation in your form fields. For example...

<input name="<?= htmlspecialchars($pestname['scoutlogpestname']) ?>[<?= $obj['card_id'] ?>]" ...

This will produce something like

<input name="Aphids[1]" ...
<input name="Aphids[2]" ...

When submitted, this will give you an array of arrays in the $_POST super global, eg

Array
(
    [Aphids] => Array
        (
            [1] => 11
            [2] => 
        )
)

You can then iterate each entry and value array, eg

foreach ($_POST as $pestname => $values) {
    foreach ($values as $card_id => $value) {
        // code goes here
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for that nudge Phil. I just started finding the need for arrays and as usual the help here at Stack overflow gets me pointed in the correct direction. Your code shows me the values that I need. I should be able to process them now. Thanks for your help and thanks to all that replied. I changed my markup for the inputs.;)
I tied to add another array from a hidden input named $cardtype but it isn't doing what I thought it would. I tried a few variations. Am I close. foreach ($_POST as $pestname => $values) { foreach ($values as $cardtype => $card_type) { $card_type; } foreach ($values as $card_id => $pestcount) { $pestname; $card_id; $pestcount; }}
@Progrower Probably best to ask a new question
1

If you need to iterate all fields, then consider using a simple foreach and split the array keys based on "#":

// e.g. ['Aphids#1' => 11]
foreach ($array as $key => $value) {
    list($pest_name, $card_id) = explode('#', $key, 2);
}

The variables $pest_name and $card_id are assigned a value based on the results of the explode() operation.

See also: list

That said, storing values as part of a field name in that way is kind of clunky and it would be better to use an array syntax like Phil suggested in his answer.

Comments

0

Another way:

$array = array("Aphids#1" => 11,
    "Thrips#1" => 5,
    "White-Fly#1" => 7,
    "Aphids#2" => 5,
    "Thrips#2" => 1,
    "White-Fly#2" => 22,
    "Aphids#3" => 4,
    "Thrips#3" => 1);

$data = array();
foreach ($array as $key => $value) {
    $exploded = explode('#', $key);
    $obj = array(
        "pest_name" => $exploded[0],
        "card_id"   => $exploded[1],
        "value"     => $value
    );
    array_push($data, $obj);
}

echo "<pre>", print_r($data), "</pre>";

Will give you:

Array
(
    [0] => Array
        (
            [pest_name] => Aphids
            [card_id] => 1
            [value] => 11
        )

    [1] => Array
        (
            [pest_name] => Thrips
            [card_id] => 1
            [value] => 5
        )

    [2] => Array
        (
            [pest_name] => White-Fly
            [card_id] => 1
            [value] => 7
        )

    [3] => Array
        (
            [pest_name] => Aphids
            [card_id] => 2
            [value] => 5
        )

    [4] => Array
        (
            [pest_name] => Thrips
            [card_id] => 2
            [value] => 1
        )

    [5] => Array
        (
            [pest_name] => White-Fly
            [card_id] => 2
            [value] => 22
        )

    [6] => Array
        (
            [pest_name] => Aphids
            [card_id] => 3
            [value] => 4
        )

    [7] => Array
        (
            [pest_name] => Thrips
            [card_id] => 3
            [value] => 1
        )

)

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.