0

I am php noob. I have searched Google very thoroughly past couple of days and can't figure that out.

I have multidimensional array I have to convert to radio buttons with unique ID and values, but I can't seem to do it.

The json array:

Array
(
[0] => Array
    (
        [0] => Array
            (
                [available] => 1
                [courier] => 1
                [type] => 1
                [price] => 42.89
                [transitDays] => 3
            )

        [1] => Array
            (
                [available] => 1
                [courier] => 1
                [type] => 3
                [price] => 50.50
                [transitDays] => 4
            )

        [2] => Array
            (
                [available] => 0
            )

        ...

    )

[1] => Array
    (
        [0] => Array
            (
                [available] => 1
                [courier] => 2
                [type] => 1
                [price] => 111
                [transitDays] => 11
            )

        [1] => Array
            (
                [available] => 0
                [courier] => 2
                [type] => 4
                [price] => 22
                [transitDays] => 22
            )

        ...
    )
)

I need to make every output some of the values of every ['available']==1 array into radio buttons and on select be able to retrieve the data after form submission.

<p class="row"><input type="radio" id="option-<?php echo $i ?> value="service-<?php echo $i ?>" name="type" "> <?php echo $service['type']; ?> will cost <?php echo $service['price']; ?> and take <?php echo $service['days']; ?></p>

I have tried flattening arrays and spew available results, but I can't assign unique ID's then. I tried

  foreach ($providers as $provider) {
  $mergeProvider = array_merge($provider);
  foreach ($provider as $services){
    $service = array_merge($services);
      if( $service['available'] == 0 ) { unset($service); }
      $serviceCount = count($service);
      else {
          include('offer.php'); //where is input type="button"
      }

but this does not allow me unique ID's.

If I do:

foreach ($providers as $provider) {
  $mergeProvider = array_merge($provider);
  foreach ($provider as $services){
    $service = array_merge($services);
        $serviceCount = count($services);
        for( $i = 1; $i < $serviceCount; $i++ ) {
        echo "<pre>";
        echo $serviceCount . "</pre>";

it spews out $serviceCount amount of different options where same option has different ID within it.

What can I do?

2 Answers 2

1

As an answer to the question in your comment:

You mean how to map the service-10 back to the array? You then you need a way to get the '10' from the string 'service-10'. But that can go wrong when the numbers become greater than 10. For example 110 (1 and 10). So I've added another example of how to could do this. I've updated the code with a pipe to separate the $key and the $subkey: $uniqueKey = $key . '|' . $subKey;

I've also added a var_dump so you can see the mapped data that it matches.

// for example, this is your index.php

<html>
<head></head>
<body>
<form id="theForm" name="theForm" method="POST" action="submit.php">
    <?php
    $items = array(
        0 => array(
            0 => array(
                "available" => 1,
                "courier" => 1,
                "type" => 1,
                "price" => 42.89,
                "transitDays" => 3
            ),
            1 => array(
                "available" => 1,
                "courier" => 1,
                "type" => 3,
                "price" => 50.50,
                "transitDays" => 4
            ),
        ),
        1 => array(
            0 => array(
                "available" => 1,
                "courier" => 2,
                "type" => 1,
                "price" => 111,
                "transitDays" => 11
            ),
            1 => array(
                "available" => 0,
                "courier" => 2,
                "type" => 4,
                "price" => 22,
                "transitDays" => 22
            ),
        )
    );

    foreach($items as $key => $item) {
        foreach($item as $subKey => $subItem) {
            if ($subItem["available"] === 1) {
                $uniqueKey = $key . '|' . $subKey;
                echo sprintf(
                    '<p class="row"><input type="radio" id="option-%1$s" value="service-%1$s" name="type">%2$s will cost %3$s and take %4$s</p>',
                    $uniqueKey,
                    $subItem["type"],
                    $subItem["price"],
                    $subItem["transitDays"]
                );
            }
        }
    }
    ?>
    <input type="submit" name="submit" value="submit">
</form>
</body>
</html>

For example this is your submit.php

<?php
$items = array(
    0 => array(
        0 => array(
            "available" => 1,
            "courier" => 1,
            "type" => 1,
            "price" => 42.89,
            "transitDays" => 3
        ),
        1 => array(
            "available" => 1,
            "courier" => 1,
            "type" => 3,
            "price" => 50.50,
            "transitDays" => 4
        ),
    ),
    1 => array(
        0 => array(
            "available" => 1,
            "courier" => 2,
            "type" => 1,
            "price" => 111,
            "transitDays" => 11
        ),
        1 => array(
            "available" => 0,
            "courier" => 2,
            "type" => 4,
            "price" => 22,
            "transitDays" => 22
        ),
    )
);

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (isset($_POST['type'])) {
        $type = $_POST['type'];
        $positionDash = strpos($type, '-');
        $positionPipe = strpos($type, '|');
        if (false !== $positionDash && false !== $positionPipe) {
            $tail = substr($type, $positionDash+1);
            $tree = explode('|', $tail);
            $mappedData = $items[$tree[0]][$tree[1]];
            var_dump($mappedData);
        }
    }
}
Sign up to request clarification or add additional context in comments.

8 Comments

I don't understand why am I getting NULL all the time. Everything seems to work up until $mappedData
Just checking, did you copy all the code or only the part where the post values are being checked? I've also changed the way the $uniqueKey is being created.
Just to be sure, I've replaced the code with a test setup. Can you tell me if this setup works on your machine?
The test is working fine now, but it gives me null whenever I copy if ($_SERVER['REQUEST_METHOD'] === 'POST') { ... } } } to another file for action="submit.php", which is odd
When you are posting to another file (in your case submit.php), you should also copy the $items array to submit.php . On the first page you are generating the selectboxes using the $items array as the data source. When you post to another file and try to map the posted data back, that other file should also have the data source to which you are trying to map it.
|
0

Maybe you can create a unique key based on the keys of the foreach loops.

Then when you post the form, the name field will contain a unique value like service-00, service-01, service-10

For example:

$items = array(
    0 => array(
        0 => array(
            "available" => 1,
            "courier" => 1,
            "type" => 1,
            "price" => 42.89,
            "transitDays" => 3
        ),
        1 => array(
            "available" => 1,
            "courier" => 1,
            "type" => 3,
            "price" => 50.50,
            "transitDays" => 4
        ),
    ),
    1 => array(
        0 => array(
            "available" => 1,
            "courier" => 2,
            "type" => 1,
            "price" => 111,
            "transitDays" => 11
        ),
        1 => array(
            "available" => 0,
            "courier" => 2,
            "type" => 4,
            "price" => 22,
            "transitDays" => 22
        ),
    )
);

// then loop through the $items and create unique key

foreach($items as $key => $item) {
    foreach($item as $subKey => $subItem) {
        if ($subItem["available"] === 1) {
            $uniqueKey = $key . $subKey;
            echo sprintf(
                '<p class="row"><input type="radio" id="option-%1$s" value="service-%1$s" name="type">%2$s will cost %3$s and take %4$s</p>',
                $uniqueKey,
                $subItem["type"],
                $subItem["price"],
                $subItem["transitDays"]
            );
        }
    }
}

2 Comments

Whoa! That is what I needed to start my workweek! I wouldn't be able to come up with this on my own, yet! Thanks! But I have a question now - what next can I do with received value? How can I assign according array to that value?
I've added an answer to your question.

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.