homepage Welcome to WebmasterWorld Guest from 54.161.175.231
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
unsetting a row of a multidimensional array
optik




msg:4034991
 11:33 pm on Dec 1, 2009 (gmt 0)

I have a multidimensional array in this format, $array[0][0][0],$array[1][0][0],$array[2][0][0] etc,
how can I unset a given row based on the index of the first element and reset the index of the whole array.

So if I deleted $array[1][0][0], $array[2][0][0] and any values in the this index e.g $array[2][0][1] and $array[2][1][0] get changed to having there index to $array[1][0][0]. So if $array[2][0][1] was "foo"
now $array[1][0][1] would be "foo"

 

NomikOS




msg:4035165
 6:13 am on Dec 2, 2009 (gmt 0)

for ($i = $rowToDelete; $i < count($array), $i++) 
{
unset($array[$i]);
$array[$i] = $array[$i + 1];
}

An array of 3x3 now will be 2x3, 3x5 -> 2x5, etc...

optik




msg:4035293
 11:07 am on Dec 2, 2009 (gmt 0)

that's removing the data but not altering the structure, so it leaves the row there but with empty values

NomikOS




msg:4035300
 11:37 am on Dec 2, 2009 (gmt 0)

for ($i = $rowToDelete; $i < count($array); $i++)
{
$array[$i] = $array[$i + 1];
unset($array[$i+1]);
}
Now alter the structure...

optik




msg:4035328
 12:44 pm on Dec 2, 2009 (gmt 0)

still doesn't work, the problem I think is that I can use unset($array[$i+1]); do empty a row completely

optik




msg:4035336
 12:50 pm on Dec 2, 2009 (gmt 0)

actually just tested it out of the main script and it seems to work, I'll just check over the other code.

optik




msg:4035346
 1:06 pm on Dec 2, 2009 (gmt 0)

sorry it doesn't work here is the before and after from a test to remove the last set

Array ( [0] => Array ( [0] => Array ( [0] => true ) ) [1] => Array ( [0] => Array ( [0] => true2 ) ) [2] => Array ( [0] => Array ( [0] => true3 ) ) )

after

Array ( [0] => Array ( [0] => Array ( [0] => true ) ) [1] => Array ( [0] => Array ( [0] => true2 ) ) [2] => )

There is still [2]=> at the end, this needs to be removed.

code...

$image_array[0][0][0]="true";
$image_array[1][0][0]="true2";
$image_array[2][0][0]="true3";

print_r(array_values($image_array));

for ($i = 2; $i < 3; $i++)
{
$image_array[$i] = $image_array[$i + 1];
unset($image_array[$i+1]);
}
echo"<br>";

print_r(array_values($image_array));

optik




msg:4035380
 1:53 pm on Dec 2, 2009 (gmt 0)

I've resorted to creating a new array for now

NomikOS




msg:4035530
 5:17 pm on Dec 2, 2009 (gmt 0)

# make array 
for ($i = 0; $i < 3; $i++)
for ($j = 0; $j < 3; $j++)
for ($k = 0; $k < 3; $k++)
$array[$i][$j][$k] = rand(1, 99);

var_dump($array);

# squeeze it!
$rowToDelete = 0;
for ($i = $rowToDelete; $i < count($array); $i++)
{
$array[$i] = $array[$i + 1];
unset($array[$i+1]);
}

# cut the tail!
unset($array[count($array) - 1]); // <--- d[^^]

echo "\n<hr />\n\n";

# now you got it!
var_dump($array);


optik




msg:4035537
 5:29 pm on Dec 2, 2009 (gmt 0)

that's it, thanks for your help.

NomikOS




msg:4035541
 5:36 pm on Dec 2, 2009 (gmt 0)

fun fun fun... d[^^]b

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved