Welcome to WebmasterWorld Guest from 54.167.0.111

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

unsetting a row of a multidimensional array

     

optik

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

5+ Year Member



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

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

10+ Year Member



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

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

5+ Year Member



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

NomikOS

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

10+ Year Member



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

optik

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

5+ Year Member



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

optik

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

5+ Year Member



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

optik

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

5+ Year Member



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

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

5+ Year Member



I've resorted to creating a new array for now

NomikOS

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

10+ Year Member



# 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

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

5+ Year Member



that's it, thanks for your help.

NomikOS

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

10+ Year Member



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

Featured Threads

Hot Threads This Week

Hot Threads This Month