Jump to content

Remove Element


Doyley

Recommended Posts

Hi all,

 

I have an array I have included below, it is created by whatever is posted from the previous page.  I would like it to remove all of the blank elements if possible.

 

The original array is...

 

Array
(
    [r11score1] => gh
    [r12score1] => ghg
    [r13score2] => 
    [r14score2] => 
    [r15score3] => fgh
    [r16score3] => fgh
    [r17score4] => 
    [r18score4] => 
    [r19score5] => 
    [r110score5] => 
    [r111score6] => 
    [r112score6] => 
    [r113score7] => 
    [r114score7] => 
    [r115score8] => 
    [r116score8] => 
    [r117score9] => fgh
    [r118score9] => gfh
    [r119score10] => 
    [r120score10] => 
    [r121score11] => 
    [r122score11] => 
    [r123score12] => 
    [r124score12] => 
    [r125score13] => 
    [r126score13] => 
    [r127score14] => 
    [r128score14] => 
    [r129score15] => 
    [r130score15] => 
    [r131score16] => 
    [r132score16] => 
    [tid] => 1
)

 

I need it to look like..

 

Array
(
    [r11score1] => gh
    [r12score1] => ghg
    [r15score3] => fgh
    [r16score3] => fgh
    [r117score9] => fgh
    [r118score9] => gfh
    [tid] => 1
)

 

The code that I have wrote to do this doesn't work, it is as follows..

 

$num=count($_POST);
$num=$num-1;
$x=0;

while($x<=$num){

if(is_null($_POST[$x])){

unset($_POST[$x]);
}

$x++;
}

 

Also, is there a way to find out the original variable name?  For example in the above array if I want to know $_POST[4] it will tell me r117score9

 

Many thanks!

Link to comment
https://forums.phpfreaks.com/topic/77129-remove-element/
Share on other sites

OK I have the following array which is shown if I use print_r($_POST)

 

Array
(
    [r131score16] => 3
    [r132score16] => 2
    [tid] => 1
)

 

Now, there is no way I know what them array keys are going to be.  I know it will always start with r and have score in the middle but the numbers are assigned depending on which text input field is filled in and a row ID in the database.  So I need to get them numbers out so I can use them further on in the script.

 

For example I have 16 on the end of both the above keys.  I need it to do something similar to this...

 


$rowid="" // Whatever that number is, it will always be different.
$score=$_POST['0'];

$sql="update table set score='$score' where uid='$rowid'";

Link to comment
https://forums.phpfreaks.com/topic/77129-remove-element/#findComment-390587
Share on other sites

Can you explain what every single number means? Example:

r<num1>score<num2> = <num3>

 

num1 is the row number, num2 is the score and num3 is what??

 

The following script does:

$sql="update table set score='<num2>' where uid='<num1>'";

 

<?php

//Delete the empty ones:
$empty = array_keys($_POST, "");
foreach($empty as $k)
unset($_POST[$k]);

//Take all those that match the r<num>score<num> and do sql-stuff
foreach($_POST as $key => $val)
{
if(preg_match("/r(\d+)score(\d+)/i", $key, $matches))
{
	$score = $matches[2];
	$rowid = $matches[1];
	$sql="update table set score='$score' where uid='$rowid'";
}
}

?>

 

 

Orio.

Link to comment
https://forums.phpfreaks.com/topic/77129-remove-element/#findComment-390612
Share on other sites

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.