Trabajar con matrices asociativas, formularios HTML y PHP foreach y Expression Engine

Tengo un formulario HTML que se parece a esto...

<input type="checkbox" name="comp[{url_title}][check]" value="Yes" class="checkbox" />
{if question}<input type="text" name="comp[{url_title}][answer]" value="" />{/if}

<input type="checkbox" name="comp[{url_title}][check]" value="Yes" class="checkbox" />
{if question}<input type="text" name="comp[{url_title}][answer]" value="" />{/if}

<input type="checkbox" name="comp[{url_title}][check]" value="Yes" class="checkbox" />
{if question}<input type="text" name="comp[{url_title}][answer]" value="" />{/if}

mi vardump en $_POST se ve así...

array(3) {
  ["some-competition-title"]=>
  array(1) {
    ["check"]=>
    string(3) "Yes"
  }
  ["third-competition"]=>
  array(2) {
    ["check"]=>
    string(3) "Yes"
    ["answer"]=>
    string(6) "asdasd"
  }
  ["another-comp-title"]=>
  array(1) {
    ["check"]=>
    string(3) "Yes"
  }
}

y mi bucle foreach se ve así

foreach ($_POST['comp'] as $topkey => $input) {
    foreach ($input as $key => $value) {

        echo "<div style=\"background:#fff; padding:10px;\">";
        echo "$topkey | $value ";
        echo "<br>";
        echo "</div>";

    }
}

que volverá

some-competition-title | Yes 
third-competition | Yes 
third-competition | asdasd 
another-comp-title | Yes 

Bien, estoy intentando pasar el bucle foreach para que combine la casilla de verificación y la respuesta si el nombre de la matriz (título de la URL) es el mismo, por lo que básicamente me gustaría que devuelva esto.

some-competition-title | Yes 
third-competition | Yes | asdasd 
another-comp-title | Yes 

Opuesto a lo que tengo arriba. Cualquier ayuda sería realmente apreciada. Gracias.

preguntado el 12 de junio de 14 a las 10:06

2 Respuestas

Simplemente necesita combinar los valores del subarreglo en un solo valor.

foreach ($_POST['comp'] as $topkey => $input) 
{
    // get the values of the subarray and assign them
    // to $value 
    $value = implode(' | ', array_values($input));

    echo "<div style=\"background:#fff; padding:10px;\">";
    echo "$topkey | $value ";
    echo "<br>";
    echo "</div>";
}

Respondido el 12 de junio de 14 a las 10:06

¿Qué tal si usamos un bucle for?

foreach ($_POST['comp'] as $topkey => $input) {
    echo "<div style=\"background:#fff; padding:10px;\">";
    $value = '';
    if (isset($input['check']) && isset($input['answer'])) {
        $value = $topkey . " | " . $input['check'] . " | " . $input['answer'];
    } elseif (isset($input['check'])) {
        $value = $topkey . " | " . $input['check'];
    }
    echo "$topkey | $value ";
    echo "<br />";
    echo "</div>";
}

Compruebas si ambos check y answer está presente en la matriz, en caso afirmativo, el primero if la condición es verdadera y se toma el valor; de lo contrario, solo check está presente, el elseif ejecuta.

Respondido el 12 de junio de 14 a las 12:06

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.