Jump to content

[SOLVED] I think this is simple


scuff

Recommended Posts

Ok I have a code:

<?php
$array1 = array();
$array1[0] = "hey hey";
$array1[1] = "hey hey";

$counter = count($array1);

for ($p = 0; $p < $counter; $p++) {
$arraynew.$p = explode(" ", $array1[$p]);
}

?>

Basically what I want it to do is create a new array for each one of the array values. But the problem is that $arraynew.$p isn't working, it just sets the array to $p instead of what I want: arraynew0 then arraynew1 etc.. If you have any questions or anything just ask. thanks!

Link to comment
Share on other sites

you have to make a variable variable out of it, for example.

 

Here's how.

<?php
$array1 = array();
$array1[0] = "hey hey";
$array1[1] = "hey hey";

$counter = count($array1);

for ($p = 0; $p < $counter; $p++) {
$varname = 'arraynew' . $p;
$$varname = explode(" ", $array1[$p]);
}

?>

 

This way you'll have an varible of $arraynew0 that contains array with [0] = hey and [1] = hey.

 

Hope this helps!

 

 

Link to comment
Share on other sites

And yea, use thorpe's, it lookes a ton faster to parse aswell.

 

It would actually be quicker to simply create an array of arrays.

 

<?php

$array1 = array();
$array1[0] = "hey hey";
$array1[1] = "hey hey";

$counter = count($array1);

$array2 = array();
foreach ($array1 as $value) {
  $array2[] = explode(' ', $value);
}

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.