Solved

delete entire row from array php

Posted on 2010-08-16
8
490 Views
Last Modified: 2012-06-21
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
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;

Open in new window

0
Comment
Question by:cbielich
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 2

Expert Comment

by:Tekati68
ID: 33452014

<?php

2

$arr = array("zero","one", "two", "three", "four", "five", "six");

3

 

4

array_splice($arr,3,2);

5

print_r($arr);

6

// result: Array ( [0] => zero [1] => one [2] => two [3] => five [4] => six )

7

?>

Open in new window

0
 
LVL 2

Expert Comment

by:Tekati68
ID: 33452016


<?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 )



?>

Open in new window

0
 
LVL 9

Expert Comment

by:Snarfles
ID: 33452017
Try this.

unset ($array[3]);
array_unshift ($array, array_shift ($array));
0
 
LVL 12

Expert Comment

by:Avinash Zala
ID: 33452032
Hi Just try attached Code:

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

?>

Open in new window

0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 7

Accepted Solution

by:
jhp333 earned 500 total points
ID: 33452115
You almost did it.
if ($cart) {
	$tempcart = array();
	foreach ($cart as $line) {
		if (!($line['id'] == $id && $line['style'] == $style && $line['color'] == $color && $line['size'] == $size)) {
			$tempcart[] = $line;
		}
	}
}
$cart = $tempcart;

Open in new window

0
 
LVL 7

Expert Comment

by:jhp333
ID: 33452126
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);

Open in new window

0
 
LVL 1

Author Comment

by:cbielich
ID: 33452135
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
    )
)
0
 
LVL 1

Author Comment

by:cbielich
ID: 33452226
Posted that last one before I saw the solution. Thank You!!
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now