0

I have string that looks like this:

Array ( [0] => movies [1] => games [2] => art [3] => books [4] => cars [5] => business [6] => lifestyle )

I want to make an array out of this string. I generate this string dynamically and I won't know what the values are. I get this string from an array that looks like this:

Array ( [0] => Array (movies [0] => games [1] => art [2] => books [3] => cars [4] => business [5] => lifestyle )) 

I am trying to extract the nested array like this: $interests = $_POST['interests'][0]; (The array interests is from a form with checkboxes) But when I do that, it treats the nested array as a string not an array. I know this because I use the in_array function next and it throws me this error:

Warning: in_array() expects parameter 2 to be array, string given in /Application/...

I don't know why it's treating the nested array as a string, but it is. So how do I convert the string back into an array.

*UPDATE: *

My HTML looks like this:

<form action = 'signup.php?step=2' method='post'>
    <h3>Interests</h3>
    <div class="interest_chooser">
        <label><input type="checkbox" value="music" name="interest[]"> Music<br></label>
        <label><input type="checkbox" value="movies" name="interest[]"> Movies<br></label>
        <label><input type="checkbox" value="games" name="interest[]"> Games<br></label>
        <label><input type="checkbox" value="art" name="interest[]"> Art<br></label>
        <label><input type="checkbox" value="books" name="interest[]"> Books<br></label>
        <label><input type="checkbox" value="cars" name="interest[]"> Cars<br></label>
        <label><input type="checkbox" value="ideas" name="interest[]"> Ideas<br></label>
        <label><input type="checkbox" value="business" name="interest[]"> Business<br></label>
        <label><input type="checkbox" value="comedy" name="interest[]"> Comedy<br></label>
        <label><input type="checkbox" value="technology" name="interest[]"> Technology<br></label>
        <label><input type="checkbox" value="lifestyle" name="interest[]"> Lifestyle<br></label>
    </div>
    <input type = 'submit'>
</form>

And my signup.php?step=2 looks like this:

<form action = 'signup.php?step=3' method='post'>
    <input type="hidden" name="interests" value="<?php print_r($_POST['interest']); ?>">
    <!-- More Inputs that I will not show because they are irrelevant -->
    <input type='submit'>
</form>

And my signup.php?step=3 submits the data and the part where I get the array looks like this:

$interests = $_POST['interests'][0];
if (in_array('music', $interests)) {
         $music = 'music';
} else {
         $music = 'no_val';
}

And I perform more of the statements above.

4
  • 1
    <input type="hidden" name="interests" value="<?php print_r($_POST['interest']); ?>"> is the wrong way to do this for a couple of reasons. Use sessions instead. Commented Feb 8, 2014 at 18:13
  • 2
    print_r is non-reversible. Use something like implode/explode, json_encode()/json_decode() or serialize()/unserialize(), (and beware of XSS attacks... escape those with htmlspecialchars()). But John Conde's remark that it shouldn't be in the form in the first place is even more correct. Commented Feb 8, 2014 at 18:14
  • @JohnConde I don't understand what you mean. Please answer the question if you know the answer Commented Feb 8, 2014 at 18:15
  • @user3112869 I have provided an answer Commented Feb 8, 2014 at 18:49

1 Answer 1

1

As Wrikken mentioned in the comments print_r() is not reversible. So the data you have in that array cannot be repopulated back into a variable once you have echo'd it using print_r(). If your goal is to put those values into your form for later submission and processing you need to choose a format that is reversible. There are a few ways to do it:

Use json_encode()/json_decode()

This will transform your array into json format (and back again):

$array = array('movies', 'games', 'art', 'books', 'cars', 'business', 'lifestyle' );
echo json_encode($array);
// output: ["movies","games","art","books","cars","business","lifestyle"]

// Reading back in
$array = json_decode($_POST['interests'], true);

Use serialize()/unserialize()

$array = array('movies', 'games', 'art', 'books', 'cars', 'business', 'lifestyle' );
echo serialize($array);
// output: a:7:{i:0;s:6:"movies";i:1;s:5:"games";i:2;s:3:"art";i:3;s:5:"books";i:4;s:4:"cars";i:5;s:8:"business";i:6;s:9:"lifestyle";}

// Reading back in
$array = unserialize($_POST['interests']);

Use a comma separated string with implode()/explode()

For very basic data like yours, just putting the values into a string separated by commas can also be effective:

$array = array('movies', 'games', 'art', 'books', 'cars', 'business', 'lifestyle' );
echo serialize($array);
// output: movies,games,art,books,cars,business,lifestyle

// Reading back in
$array = explode($_POST['interests']);

However, a better solution would be to use sessions. Sessions allow you to carry data from page-to-page without having to send the data to the client to only be sent back to the server.

On page 1:

<?php
session_start();
$interests = array('movies', 'games', 'art', 'books', 'cars', 'business', 'lifestyle' );
$_SESSION['interests'] = $array;

On page 2:

<?php
session_start();
$interests = $_SESSION['interests'];
print_r($interests);
// Output: Array ( [0] => movies [1] => games [2] => art [3] => books [4] => cars [5] => business [6] => lifestyle )
Sign up to request clarification or add additional context in comments.

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.