Progrower
Progrower

Reputation: 383

explode and process php dynamic arrays

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.

Upvotes: 2

Views: 1011

Answers (3)

Tomanow
Tomanow

Reputation: 7377

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
        )

)

Upvotes: 0

Ja͢ck
Ja͢ck

Reputation: 173662

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.

Upvotes: 1

Phil
Phil

Reputation: 165065

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
    }
}

Upvotes: 4

Related Questions