I am trying to delete all the data from within a specific key in my array, but cant work out why my code isn't working. I have used print_r to check the code works and I can see that the correct value is being printed for each array when I click the 'remove' button, but it isn't removing the data from the key.
My current array looks like this:
Array
(
[0] => Array
(
[0] => S30813-Q100-X303
[1] => 5
[2] => Refurbished
)
[1] => Array
(
[0] => JX-1T1-LTU
[1] => 8
[2] => New
)
)
I am outputting the data to a table with:
for ($row = 0; $row < $totalcount; $row++) {
echo "<tr>";
for ($col = 0; $col < 3; $col++) {
echo "<td>".$contents[$row][$col]."</td>";
}
for ($col = 0; $col < 1; $col++) {
$del = $row;
echo "<td><form action='' method='post'><input type='text' name='del' value=".$row."></input><input type='submit' name='deletepart' value='Remove'></input></form></td>";
}
echo "</tr>";
}
Front-end:
My php to unset the array key (and where I am guessing the problem lies) is:
<?php
if (isset($_POST['deletepart'])) {
$value_to_delete = $_POST['del'];
$value_to_delete = $key;
unset($_SESSION['arr'][$key]);
$_SESSION["arr"] = array_values($_SESSION["arr"]);
}
?>
Any help on where I am going wrong here would be very much appreciated!
try this
write $key = $value_to_delete; instead of $value_to_delete = $key
<?php
if (isset($_POST['deletepart'])) {
$value_to_delete = $_POST['del'];
//$value_to_delete = $key; //instead of this use
$key = $value_to_delete;
unset($_SESSION['arr'][$key]);
$_SESSION["arr"] = array_values($_SESSION["arr"]);
}
?>
Related
I have an array in PHP which is established from my database, which will be formatted as such:
[ "Folder1",
["Content1", "Content2", "Content3"],
"Folder2",
["Content1", "Content2", "Content3"]
]
I have the current code for this process
<?php
$sql = ("SELECT FlashCardFolderName, FlashCardSetName FROM FlashCardFolders, FlashCardSets WHERE FlashCardFolderUserID = " . $_SESSION["id"] . " AND FlashCardSetFlashCardFolderID = FlashCardFolderID ORDER BY FlashCardFolderName");
$result = $db->get_con()->query($sql);
if($result->num_rows > 0){
$temp = "";
$foldersAndSets = array();
$tempSet = array();
while ($row = $result->fetch_assoc()){
if($temp===$row["FlashCardFolderName"]){
array_push($tempSet, $row["FlashCardSetName"]);
} else{
array_push($foldersAndSets, $tempSet);
$tempSet = array();
array_push($foldersAndSets, $row["FlashCardFolderName"]);
array_push($tempSet, $row["FlashCardSetName"]);;
$temp = $row["FlashCardFolderName"];
}
}
array_push($foldersAndSets, $tempSet);
array_shift($foldersAndSets);
echo json_encode($foldersAndSets);
} else{
echo "<h6>Looks like there's nothing here...</h6>";
}
$length = sizeof($foldersAndSets);
for ($i = 0; $i < $length; $i++){
$secondDimension = sizeof($foldersAndSets[$i+1]);
for($j = 0; $j < $foldersAndSets; $j++) {
echo "$foldersAndSets[$i][$j+1]";
}
}
?>
But it seems it's not working the way I want it to. Any ideas as to what I could be doing wrong?
First of all I'm suggestion to you to make a useful format of your input data, I mean it would be better to reformat your input array. For example, with structure like Folder -> array of Contents:
$reformatted = [];
$tmp = '';
foreach($foldersAndSets as $ind=>$txt){
if ($ind % 2 == 0){ // if this is a 1-st, 3-rd, 5-th etc value -> Folders
$reformatted[$txt] = [];
$tmp = $txt;
} else { // if this is a 2-nd, 4-th etc value -> Content
$reformatted[$tmp] = $txt;
}
}
This will create an array like:
Array
(
[Folder1] => Array
(
[0] => Content1
[1] => Content2
[2] => Content3
)
[Folder2] => Array
(
[0] => Content1
[1] => Content2
[2] => Content3
)
)
With this array you can work in further. Now you can get any value you need with help of a simple foreach loop:
foreach($reformatted as $fold=>$cont){
echo $fold.PHP_EOL.PHP_EOL;
foreach($cont as $item){
echo $item.PHP_EOL;
}
echo PHP_EOL;
}
Output:
Folder1
Content1
Content2
Content3
Folder2
Content1
Content2
Content3
Demo
Note: you can replace your second part(from $length = sizeof($foldersAndSets); and below) of code with this.
I have one array with values: $array_metaValue
Array ( [0] => php [1] => ajax [2] => my [3] => profile [4] => java )
and second array contains: $search_res[$e]
php
ajax
But the problem is that the count value is always one which is wrong. It should be 2.
print_r( $array_metaValue);
for($e=0;$e<=count($search_res);$e++){
echo '<br>'.$search_res[$e].'<br>';
echo '<pre>';
$key = array_search($search_res[$e],$array_metaValue);
if(!$key==0)
{
$count=$count+1;
}
$count right now is saving 1.
Use
$count = count(array_intersect($array_metaValue, $search_res));
array_intersect returns an array containing the elements that are in both of the input arrays.
The problem with your code is that you need to test
if ($key !== false)
Try this
$arrInp = array('php','ajax','my','profile','java');
$arrSearch = array('php','ajax');
$count = 0;
foreach ($arrSearch as $key => $value) {
if(in_array(trim($value), $arrInp))
$count++;
}
echo $count;
<?php
$array_metaValue=array('php','ajax','profile','java');
$search_res=array('php','ajax');
print_r($array_metaValue);
$count=0;
for($e=0;$e<count($search_res);$e++){
echo '<br>'.$search_res[$e].'<br>';
$key = array_search($search_res[$e],$array_metaValue);
echo 'Key value =>'.$key. " ";
if($key>=0)
{
$count=$count+1;
echo 'Count value =>'.$count;
}
}
?>
I have a problem. I want to put dates into an array but when I make print_r() I get only the last value from checkbox.
My code is:
$id = Input::get('id');
$aObjects = Input::get('aObjects');
$iCount = count($aObjects);
for($i=0; $i < $iCount; $i++)
{
$test = array ($aGoupes = array(
'idGroupe' => $id,
'idObject' => $aObjects[$i]
));
}
echo '<pre>';
print_r($test);
echo '</pre>';
The output is:
Array
(
[0] => Array
(
[idGroupe] => 6
[idObject] => 8
)
)
So the problem is that only the last value checked from checkbox is put in this table. Please help me!! Thnx
Your problem is that you're resetting $test each time.
Try this:
$id = Input::get('id');
$aObjects = Input::get('aObjects');
$iCount = count($aObjects);
$test = array();
for ($i = 0; $i < $iCount; $i++) {
$test[] = array (
'idGroupe' => $id,
'idObject' => $aObjects[$i]
);
}
echo '<pre>';
print_r($test);
echo '</pre>';
I'm not too sure what your code is supposed to do, but the idGroupe will always be the same in each array, as you're setting it to $id which is never changed. That may well be correct, though.
Hi All I have 2 arrays for example in PHP as seen below:
[users] => Array ( [0] => Array ( [username] => Timothy ) [1] => Array ( [username] => Frederic ) )
[users2] => Array ( [0] => Array ( [username] => Johnathon ) [1] => Array ( [username] => Frederic ) [] => Array ( [username] => Peter))
I am trying to compare the contents of each array against each other in order to put a html element, I tried using a nested foreach as seen below:
foreach($users as $user){
foreach ($users2 as $user2){
if($user['username'] == $user2['username']){
echo "<option value=' ".$user['username']."' selected = 'selected'>".$user['username']."</option>";
break;
} else{
echo "<option value=' ".$user['username']."'>".$user['username']."</option>";
}
}
}
my issue is that the items are being echoed more than once which is ruining my select element. Any ideas on how to compare the contents of each?
I want to achieve a list of each name eg:
-Timothy
-Frederic (this should be highlighted as it is in both arrays)
-Johnathon
- Peter
I would take it in a different way.
//Create user array one
$users = array();
$users[] = array('username'=>'Timothy');
$users[] = array('username'=>'Frederic');
//create user array 2
$users2 = array();
$users2[] = array('username'=>'Johnathon');
$users2[] = array('username'=>'Frederic');
$users2[] = array('username'=>'Peter');
$temp_array = array();
foreach($users as $key => $value) {
$temp_array[$value['username']] = '';
}
foreach($users2 as $key => $value) {
$temp_array[$value['username']] = array_key_exists($value['username'], $temp_array) ? 'DUPLICATE' : null;
}
echo '<select>';
foreach($temp_array as $key_value => $status) {
echo "<option value='{$key_value}' ".(($status == 'DUPLICATE') ? 'selected style="background-color: yellow;"' : '').">{$key_value}</option>";
}
echo '</select>';
I'll let the array take care of it self, if it shares the same key, it will merge, then just flag it with "duplicate".
If there is never any duplicates as you say in each array, the following works for me. This may look a bit complicated, but read through my comments.
You can copy the code and run it in it's own page to see if it works the way you want.
<?php
//Create user array one
$users = array();
$users[] = array('username'=>'Timothy');
$users[] = array('username'=>'Frederic');
//create user array 2
$users2 = array();
$users2[] = array('username'=>'Johnathon');
$users2[] = array('username'=>'Frederic');
$users2[] = array('username'=>'Peter');
//create a new array to combine all of the data, yes, there will be duplicates
$allData = array();
//add to allData array
foreach ($users as $user) {
$allData[] = $user['username'];
}
//add to allData array
foreach ($users2 as $user2) {
$allData[] = $user2['username'];
}
//create an array that will hold all of the duplicates
$dups = array();
//add any duplicates to the array
foreach(array_count_values($allData) as $val => $c) {
if($c > 1) $dups[] = $val;
}
//remove the duplicates from the allData array
$allData = array_unique($allData);
//echo out form
echo '<select>';
foreach ($allData as $user) {
if (in_array($user, $dups)) {
echo "<option value=' ".$user."' selected = 'selected'>".$user."</option>";
}
else {
echo "<option value=' ".$user."'>".$user."</option>";
}
}
echo '</select>';
?>
However, I'm not sure what your intentions are since IF you had "Peter" and "Frederic" in BOTH arrays, you are not going to get the form you want. But, this works for what you wanted.
How can I only echo the information in a session that people added by them self? My script:
<?php
session_start();
$array = $_SESSION["wenslijst"];
if (isset($_POST["item"]) && (int)$_POST["item"] > 0)
{
if (!isset($_SESSION["wenslijst"]))
{
$_SESSION["wenslijst"] = array();
$_SESSION["aantal"] = array();
}
$i = 0 ;
while ($i < count($_SESSION["wenslijst"]) && $_SESSION["wenslijst"][$i] != $_POST["item"]) $i++;
if ($i < count($_SESSION["wenslijst"]))
{
$_SESSION["aantal"][$i]++;
}
else
{
$_SESSION["wenslijst"][] = $_POST["item"];
$_SESSION["aantal"][] = 1;
}
}
?>
When I add a product my array looks like this, I only echo the bold message: (see the long code as my spefic number for a product)
Array
(
[0] => Array
(
[f805862bbd430d0673f3f949249326f3] =>
)
)
Thanks all for your help.
Aantal means: count
Wenslijst means: wishlist
Do i understand you correctly, that your array looks like this?!
[0] => array(
"your_super_long_product_key" => array(),
"another_super_long_product_key" => array(),
"awesome_super_long_product_key" => array(),
)
So you could access the data like this:
foreach(array_keys($array[0]) as $key) {
echo $key . "\n";
}
This will give you:
your_super_long_product_key
another_super_long_product_key
awesome_super_long_product_key
To loop through your whole array like this use:
foreach($array as $dataset) {
foreach(array_keys($dataset) as $key) {
echo $key . "\n";
}
}