I have a SESSION array $_SESSION['cart']
for example it has value as
Array
(
[0] =>
[1] => Array
(
[item_id] => 1420
[item_qty] => 1
)
[2] => Array
(
[item_id] => 1522
[item_qty] => 1
)
)
Now let's say I have item_id = 1420
now I want to increase the item_qty for item_id = 1420 and also I have to set it in that SESSION array.
What I tried it :
foreach($_SESSION['Cartquantity'] as $key => $d)
{
if(isset($d)) {
if($d['item_id'] == $_GET['item_id'])
{
$d['item_quntity'] = $d['item_quantity']+1;
}
}
else{
}
}
It's not working ?
You're iterating over $_SESSION['Cartquantity'] but have told us that the array is stored in $_SESSION['cart'].
Also, this:
$d['tem_quntity'] = $d['item_quantity']+1;
Should be:
$d['item_qty'] = $d['item_qty']+1;
Finally, you'll need to make $d a reference by prepending an ampersand (&) to it in the foreach condition.
foreach($_SESSION['cart'] as $key => &$d)
{
if($d) {
if($d['item_id'] == $_GET['item_id'])
{
$d['item_qty'] = $d['item_qty']+1;
}
}
else{
}
}
Use reference &$d
foreach($_SESSION['Cartquantity'] as $key => &$d)
{
if($d) {
if($d['item_id'] == $_GET['item_id'])
{
$d['tem_quntity'] = $d['item_quantity']+1;
}
}
else{
}
}
or
foreach($_SESSION['Cartquantity'] as $key => $d)
{
if($d) {
if($d['item_id'] == $_GET['item_id'])
{
$_SESSION['Cartquantity'][$key]['item_quntity'] = $d['item_quantity']+1;
}
}
else{
}
}
change this
$d['tem_quntity'] = $d['item_quantity']+1;
to
$d['item_qty'] = $d['item_qty']+1;
you can call by
foreach ($_SESSION['Cartquantity'] as $value))
{
if(isset($value))
{
if($value['item_id'] == $_GET['item_id'])
{
$value['item_quntity'] = $value['item_quantity']+1;
}
}
else
{
}
}
Try to check for !empty($array)
if(!empty($d))
because it array so you need to check for if its do not have any elements inside of it.
If you want to know if the array is defined , then use isset($d).
If you want to know if a particular key is defined, use isset($d['item_id']).
If you want to know if an array has not empty and has key value pairs , use !empty($d).
use it:
foreach($_SESSION['cart'] as $key => $d)
{
if($d) {
if($d['item_id'] == $_GET['item_id'])
{
$d['tem_quntity'] = $d['item_qty']+1;
}
}
else{
}
}
foreach($_SESSION['cart'] as $key=>$val)
{
foreach($val as $subK)
{
if($_GET["item_id"]==$subk["item_id"])
{
$_SESSION['cart'][$key]["item_id"]=$_SESSION['cart'][$key]["item_quantity"]+1;
}
}
}
Related
<?php
function tambah_penumpang($daftar_penumpang, $penumpang_baru){
if(empty($namaArray)==true){
$daftar_penumpang[]=$penumpang_baru;
return $daftar_penumpang;
}else{
for($i=0; $i<count($daftar_penumpang); $i++){
if($daftar_penumpang[$i]== null){
$daftar_penumpang[$i]=$penumpang_baru;
return $daftar_penumpang;
}else{
$daftar_penumpang[] = $penumpang_baru;
return $daftar_penumpang;
}
}
}
}
$daftar_penumpang =["sandhika",null,"carl","keith"];
print_r(tambah_penumpang($daftar_penumpang,"anggoro"))."<br>"
?>
And this are the result: (i want that anggoro name in null's index)
Array (
[0] => sandhika
[1] =>
[2] => carl
[3] => keith
[4] => anggoro
)
as I'm not familiar with your naming convention , i've made a global example using array_walk as follows:
$daftar_penumpang =["sandhika",null,"carl","keith"];
array_walk($daftar_penumpang, function($v, $k, $replacement) use (&$daftar_penumpang) {
if ($v == null) {
$daftar_penumpang[$k] = $replacement;
}
}, 'anggoro');
print_r($daftar_penumpang);
live example: https://3v4l.org/vVnSq
for your case :
$daftar_penumpang =["sandhika",null,"carl","keith"];
function tambah_penumpang($daftar_penumpang, $penumpang_baru) {
array_walk($daftar_penumpang, function($v, $k, $replacement) use (&$daftar_penumpang) {
if ($v == null) {
$daftar_penumpang[$k] = $replacement;
}
}, $penumpang_baru);
return $daftar_penumpang;
}
print_r(tambah_penumpang($daftar_penumpang, 'anggoro'));
this will output:
Array ( [0] => sandhika [1] => anggoro [2] => carl [3] => keith )
Update
using for loop :
your code issue is that you will never ever get into the for loop because it is in else condition , and while the $namaArray is alway empty, so you will never go through the loop , so i've removed this check and prepared this looping to show you how to replace null values using for loop
function tambah_penumpang($daftar_penumpang, $penumpang_baru)
{
$tmpArray = [];
for ($i=0; $i < count($daftar_penumpang); $i++) {
if ($daftar_penumpang[$i]== null) {
$tmpArray[$i] = $penumpang_baru;
} else {
$tmpArray[] = $daftar_penumpang[$i];
}
}
return $tmpArray;
}
$daftar_penumpang =["sandhika",null,"carl","keith"];
print_r(tambah_penumpang($daftar_penumpang,"anggoro"));
Try with this function :
<?php
function tambah_penumpang($daftar_penumpang, $penumpang_baru){
$datas = [];
for($i = 0; $i < count($daftar_penumpang); $i++) {
$item = $daftar_penumpang[$i];
if($item === null) {
$item = $penumpang_baru;
}
$datas[$i] = $item;
}
return $datas;
}
$daftar_penumpang =["sandhika",null,"carl","keith"];
print_r(tambah_penumpang($daftar_penumpang,"anggoro"))."<br>";
I have form with several fields I want to store those values in a session variable. Some of those fields should be 0 if the user doesn't fill them in.
A print_r($_POST) after submitting the form shows:
[report] => Array
(
[a_name] => Array
(
[0] =>
)
[a_id_card] => Array
(
[0] =>
)
[a_total] =>
Yet, after running the following PHP code, it seems that "a_name" and "a_id_card" are not interpreted as arrays. Any ideea why?
if (isset($_POST['submit'])) {
foreach ($_POST as $key => $value) {
if (!is_array($key) && trim($value) == '') {
$value = 0;
$_SESSION['report'][$key] = $value;
} else {
$_SESSION['report'][$key] = $value;
}
}
}
think you want to write this - is_array($value)
$key is the string 'report'. So is_array($key) == false. However $_POST[$key] or $value is an array.
The key is never an array. It is always a scalar or string. The value, on the other hand, can be an array.
Maybe you want look only $_POST['report'], and check if 'value' is or not an array (not key):
if (isset($_POST['submit'])) {
foreach ($_POST['report'] as $key => $value) {
if (!is_array($value) && trim($value) == '') {
$value = 0;
$_SESSION['report'][$key] = $value;
} else {
$_SESSION['report'][$key] = $value;
}
}
}
I have multiple ID's in an mysql database. I would like to know if there are ID's in the database which are not present in an multi dimensional array. For each ID which is not present in the multi dimensional array the row needs te be deleted. The following code is what I have so far.
function multi_array_search($search_for, $search_in) {
foreach ($search_in as $element) {
if ( ($element === $search_for) ) {
return true;
} elseif (is_array($element)) {
$result = multi_array_search($search_for, $element);
if($result == true)
return true;
}
}
return false;
}
$output = mysql_query("SELECT id FROM ads");
while ($g = mysql_fetch_array($output)) {
echo multi_array_search("$g", $arr) ? 'Found' : 'Not found';
}
I don't think the above code is correct for what I want?
Information:
The $arr looks like:
Array (
[0] => Array (
[url] => http://
[id] => 752
)
[1] => Array (
[url] => http://
[id] => 758
)
)
I tryed some solutions now and none of the mare working :(
Every thing seems to be fine. Just few updates to remove unwanted code from elseif and add one more check !empty into elseif condition.
function multi_array_search($search_for, $search_in) {
foreach ($search_in as $element) {
if ($element === $search_for){
return true;
}elseif(is_array($element) && !empty($element)){
$result = multi_array_search($search_for, $element);
}
}
return false;
}
$output = mysql_query("SELECT id FROM ads");
while ($g = mysql_fetch_array($output)) {
echo multi_array_search("$g", $arr) ? 'Found' : 'Not found';
}
Hope will help!
$removeid=array();
$idarray is the multi dimensional array you want to check your database id with.
$result = 'store your databse id here in the form of an array';
foreach ($result as $key => $value) {
$result=$value;
if(!empty($result))
{
foreach ($idarray as $key => $value) {
if ($value["id"] != $result) {
$removeid=$key;
}
}
}
}
now $removeid contains the id to be removed from the databse
$un_array = array();
foreach ($array as $h) {
$id = $h['id'];
array_push($un_array, $id);
}
$db_array = array();
$output = mysql_query("SELECT id FROM account WHERE account='$username'");
while ($g = mysql_fetch_assoc($output)) {
$id = $g['id'];
array_push($db_array, $id);
}
$result = array_diff($db_array, $un_array);
foreach ($result as $r) {
mysql_query("DELETE FROM account WHERE id='$r'");
}
I have two array like
previous:
Array(
[name] => [asdfg]
[city] => [anand]
)
current:
Array(
[name] => [ud]
[state] => [anand]
)
Now i have to compare these two array and want to alter the changed current array key or values and wrap the elements like
Array(
[name] => [<span class='bold_view'> ud </span>]
[<span class='bold_view'> state </span>] => [anand]
)
$current['name']="<span class='bold_view'> ".$current['name']." </span>";
$current['<span class='bold_view'> state </span>']=$current['state'];
I have to say that it doesn't make much sense but here it is..
Copy this code and just execute : and see the view source (Ctrl+u)
<?php
$prev = array("name"=>"[asdfg]","city"=>"[anand]");
$curent = array("name"=>"[ud]","state"=>"[anand]");
$res = array();
foreach($prev as $key=>$val){
$res[$key] = $val;
if(array_key_exists($key,$curent)){
$res[$key] = "[<span class='bold_view'> ".$curent[$key]." </span>]";
}
if($new_key = array_search($val,$curent)){
unset($res[$key]);
$res["<span class='bold_view'> ".$new_key." </span>"] = $val;
}
}
print_r($res);
?>
$arr_pre = array(
"name"=>"asdfg",
"city"=>"anand",
"address" => "anand",
);
$arr_current= array(
"name"=>"ud",
"state"=>"anand",
"address" => "ananda"
);
$result = array_diff_assoc($arr_current,$arr_pre);
$count_curr = array_count_values($arr_current);
$count_old = array_count_values($arr_pre);
foreach ($result as $key => $value){
if(!array_key_exists($key,$arr_pre ))
{
$key_new = "<b>".$key."</b>";
if(!in_array($value,$arr_pre))
{
$val = "<b>".$value."</b>";
}
else if((isset($count_curr[$value]) != isset($count_old[$value])))
{
$val = "<b>".$value."</b>";
}
else
{
$val = $value;
}
unset($arr_current_info[$key]);
}
else {
$key_new = $key;
if(!in_array($value,$arr_pre))
{
$val = "<b>".$value."</b>";
}
else if((isset($count_curr[$value]) != isset($count_old[$value])))
{
$val = "<b>".$value."</b>";
}
else
{
$val = $value;
}
}
$arr_current_info[$key_new]=$val;
}
echo "<pre>";
print_r($arr_current_info);
I have done like this and i got perfect answer
I have a multi-dimension array like:
$fields =
Array (
[1] => Array
(
[field_special_features5_value] => Special Function 5
)
[2] => Array
(
[field_special_features6_value] => Special Function 6
)
[3] => Array
(
[field_opticalzoom_value] => Optical Zoom
)
)
I want to get the value by key, I tried the code below but not work
$tmp = array_search('field_special_features5_value' , $fields);
echo $tmp;
How can I get the value Special Function 5 of the key field_special_features5_value?
Thanks
print $fields[1]['field_special_features5_value'];
or if you don't know at which index your array is, something like this:
function GetKey($key, $search)
{
foreach ($search as $array)
{
if (array_key_exists($key, $array))
{
return $array[$key];
}
}
return false;
}
$tmp = GetKey('field_special_features5_value' , $fields);
echo $tmp;
If you know where it is located in the $fields array, try :
$value = $fields[1]['field_special_features5_value'];
If not, try :
function getSubkey($key,$inArray)
{
for ($fields as $field)
{
$keys = array_keys($field);
if (isset($keys[$key])) return $keys[$key];
}
return NULL;
}
And use it like this :
<?php
$value = getSubkey("field_special_features5_value",$fields);
?>
You need to search recursive:
function array_search_recursive(array $array, $key) {
foreach ($array as $k => $v) {
if (is_array($v)) {
if($found = array_search_recursive($v, $key)){
return $found;
}
} elseif ($k == $key) {
return $v;
} else {
return false;
}
}
}
$result = array_search_recursive($fields, 'field_special_features5_value');
Your problem is that you have a top-level index first before you can search you array. So to access that value you need to do this:
$tmp = $fields[1]['field_special_features5_value'];
You can do it with recursive function like this
<?php
function multi_array_key_exists($needle, $haystack) {
foreach ($haystack as $key=>$value) {
if ($needle===$key) {
return $key;
}
if (is_array($value)) {
if(multi_array_key_exists($needle, $value)) {
return multi_array_key_exists($needle, $value);
}
}
}
return false;
}
?>