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
https://forums.phpfreaks.com/topic/132493-solved-i-think-this-is-simple/
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!

 

 

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);
}

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.