nitrous
nitrous

Reputation: 1757

How to create an array from a string?

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.

Upvotes: 0

Views: 138

Answers (1)

John Conde
John Conde

Reputation: 219804

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 )

Upvotes: 1

Related Questions