cbielich
asked on
delete entire row from array php
I am trying to delete a row from a php 2d array. Here is my following code. It currently does not work because it leaves a blank space in the array instead of completely removing the row and restructuring the array to not include the row at all.
Let's say I have an array like this:
$array[0] = "ball";
$array[1] = "bat";
$array[2] = "glove";
$array[3] = "referee";
$array[4] = "strike";
$array[5] = "home run";
Now, let's say I want to get rid of [3] but move the next values up, automatically, into 3,4.
For example,
$array[0] = "ball";
$array[1] = "bat";
$array[2] = "glove";
$array[3] = "strike";
$array[4] = "home run";
How do I do this in php with my attached code
Let's say I have an array like this:
$array[0] = "ball";
$array[1] = "bat";
$array[2] = "glove";
$array[3] = "referee";
$array[4] = "strike";
$array[5] = "home run";
Now, let's say I want to get rid of [3] but move the next values up, automatically, into 3,4.
For example,
$array[0] = "ball";
$array[1] = "bat";
$array[2] = "glove";
$array[3] = "strike";
$array[4] = "home run";
How do I do this in php with my attached code
if ($cart) {
$tempcart = array();
foreach ($cart as $line) {
if ($line['id'] == $id && $line['style'] == $style && $line['color'] == $color && $line['size'] == $size) {
unset($line);
}
$tempcart[] = $line;
}
}
$cart = $tempcart;
<?php
$arr = array("zero","one", "two", "three", "four", "five", "six");
array_splice($arr,3,2);
print_r($arr);
// result: Array ( [0] => zero [1] => one [2] => two [3] => five [4] => six )
?>
Try this.
unset ($array[3]);
array_unshift ($array, array_shift ($array));
unset ($array[3]);
array_unshift ($array, array_shift ($array));
Hi Just try attached Code:
Hope this helps,
Addy
Hope this helps,
Addy
<?php
$array = array(
0=>'zero',
1=>'One',
2=>'Two',
3=>'Three',
4=>'Four',
5=>'Five',
6=>'Six'
);
unset($array[3]);
$array = reIndex($array);
print_r($array);
function reIndex($array)
{
$newArray = array();
foreach($array as $key=>$value)
{
$newArray[] =$value;
}
return $newArray;
}
?>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Or this works too.
foreach ($cart as $key=>$line) {
if ($line['id'] == $id && $line['style'] == $style && $line['color'] == $color && $line['size'] == $size) {
unset($cart[$key]);
}
}
$cart = array_merge($cart);
ASKER
Ok I dont think I described my array properly, here is the structure and what result I want
Here is the original array
Array (
[0] => Array (
[id] => 1
[style] => T-Shirt
[color] => Black
[size] => XL
[qty] => 1
)
[1] => Array (
[id] => 91
[style] => T-Shirt
[color] => Black
[size] => XL
[qty] => 1
)
)
Then I want it to be
Array (
[0] => Array (
[id] => 1
[style] => T-Shirt
[color] => Black
[size] => XL
[qty] => 1
)
)
Here is the original array
Array (
[0] => Array (
[id] => 1
[style] => T-Shirt
[color] => Black
[size] => XL
[qty] => 1
)
[1] => Array (
[id] => 91
[style] => T-Shirt
[color] => Black
[size] => XL
[qty] => 1
)
)
Then I want it to be
Array (
[0] => Array (
[id] => 1
[style] => T-Shirt
[color] => Black
[size] => XL
[qty] => 1
)
)
ASKER
Posted that last one before I saw the solution. Thank You!!
Open in new window