简体   繁体   中英

How to select an array within an array PHP

I am trying to perform some array-ception here: Array within an array. I have a string forms which pass on hidden values to the next form. One of these forms submits an array of checkbox values to another form. That form then submits all previous forms into a database. It's working great except for one problem. When passing the array of checkboxes through another form, it puts the array into another array. I want to select the nested array and store it in a local variable. The array is called interests

Array ( [0] => Array ( [0] => movies [1] => art [2] => cars [3] => business [4] => comedy [5] => technology ) )

Right now my array looks like the array above. When I add [0] to $_POST['interests'] like so:

$int = $_POST['interests'][0];

It successfully stores the second array into the variable int. Problem is that it stores it as a string and not an array. It still looks like an array when I echo it out though

Array ( [0] => movies [1] => art [2] => cars [3] => business [4] => comedy [5] => technology )

How do I store the nested array in a variable. Or how do I turn the above string into an array.

Thank you

If I understood what you mean (and it's a huge if), you might use the following:

Original page:

<form method="POST" ... >
    <input type='checkbox' name='passable[interests][]' value='movies'>
    <input type='checkbox' name='passable[interests][]' value='art'>
    <input type='checkbox' name='passable[interests][]' value='cars'>
    <input type='checkbox' name='passable[interests][]' value='business'>
    <input type='checkbox' name='passable[interests][]' value='comedy'>
    <input type='checkbox' name='passable[interests][]' value='technology'>
</form>

Intermediate page:

<?php
function pass_previous_data ()
{
    foreach ($_POST['passable'] as $name => $block)
    foreach ($block as $value)
    {
        echo "<input type='hidden' name='passable[$name][]' value='$value'>";
    }
}
?>

<form method="POST" ... >

    // by default, form fields will not be forwarded
    <input type='text' name='added_data' value='whatever'>

    // inject previous data as hidden fields
    <?php pass_previous_data (); ?> 

    // add more forwardable data
    <input type='checkbox' name='passable[languages][]' value='English'>
    <input type='checkbox' name='passable[languages][]' value='Español'>
    <input type='checkbox' name='passable[languages][]' value='Deutsch'>
    <input type='checkbox' name='passable[languages][]' value='Русский'>
</form>

The final page should see the whole packet of selected values as an array.

The system allows you to forward data across multiple pages by declaring input names as passable .
It makes retrieving data pretty straightforward with minimal PHP code.

All this being said, it's a pretty inefficient way of passing data around:
you generate a whole hidden HTML input to pass each single value.

You could encode your arrays with Json or PHP serialization mechanism instead, but I suppose you have your reasons to do otherwise.


Here is a one-page working demo of the above example (a little obfuscated by PHP/HTML blending though)

<!DOCTYPE html>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
<?php
// recreate hidden fields to forward previous post data
function pass_previous_data ()
{
    foreach ($_POST['passable'] as $name => $block)
    foreach ($block as $value)
    {
        echo "<input type='hidden' name='passable[$name][]' value='$value'>";
    }
}
if (!isset($_POST['page']))
{ // first page
?>

<body onload='document.getElementById("post").submit();'>
    <form method="POST" id="post">
        <input type='hidden' name='page' value='2'>
        <input type='checkbox' name='passable[interests][]' value='movies' checked>
        <input type='checkbox' name='passable[interests][]' value='art' checked>
        <input type='checkbox' name='passable[interests][]' value='cars'>
        <input type='checkbox' name='passable[interests][]' value='business'>
        <input type='checkbox' name='passable[interests][]' value='comedy'>
        <input type='checkbox' name='passable[interests][]' value='technology' checked>
    </form>
</body>

<?php } else switch ($_POST['page']) {
case '2':  // second page 
?>

<body onload='document.getElementById("post").submit();'>
    <form method="POST" id="post">
        <input type='hidden' name='page' value='3'>
        <?php pass_previous_data (); ?>

        <input type='text' name='added_data' value='whatever'>

        <input type='checkbox' name='passable[languages][]' value='English' checked>
        <input type='checkbox' name='passable[languages][]' value='Español'>
        <input type='checkbox' name='passable[languages][]' value='Deutsch' checked>
        <input type='checkbox' name='passable[languages][]' value='Русский' checked>

    </form>
</body>

<?php break; 
case '3': 
    // final page
    echo"<pre>";print_r ($_POST);echo"</pre>";
    break;
}
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM