How to extract only value from the below array
Array ( [COUNT(department_name)] => 3 )
this is a response from a db query the actual value came like this
Array ( [0] => Array ( [COUNT(department_name)] => 3 ) )
if we select the 0th index $department[0] it is giving this
Array ( [COUNT(department_name)] => 3 )
but now i need only the value from this i.e 3 how can i extract the value from this.
and i tried like this $department[0]['department_name'] and $department[0]['COUNT(department_name)'] but no result.
You can use this simply and it's working -
$department = var_export($department,true);
print_R($department[0]['COUNT(department_name)'])
var_export helps you get the structured information of a variable.
For more see this -
var_export
NOTE - I will suggest you to to get a more readable key from database.
I have this array from an AJAX request:
array (
[0] => 'lat,long#id_1'
[1] => 'lat,long#id_2'
[2] => 'lat,long#id_3'
)
The problem here is I'm not gonna have always a 'correct-ordered' array like that, so it may look like this one:
array (
[0] => 'lat,long#id_1'
[1] => 'lat,long#id_2'
[2] => 'lat,long#id_3'
[3] => 'lat,long#id_1'
[4] => 'lat,long#id_3'
[5] => 'lat,long#id_2'
)
I need to get the last array value of every id_X (currently only 3 ids):
array (
[3] => 'lat,long#id_1'
[4] => 'lat,long#id_3'
[5] => 'lat,long#id_2'
)
How can I find each last value of that array based on partial string (id_X)?
First do a reverse sort to ensure the latest values are parsed first. Then run them through a loop, matching the partial string to get the ID, adding the data to an array if the ID index doesn't exist yet. Last values will be added to your array, others will be neglected. Something like this:
rsort($ajaxy);
$lasts = [];
foreach($ajaxy as $str) {
$id = substr($str, strrpos($str, '#') + 1);
if (!isset($lasts[$id])) {
$lasts[$id] = $str;
}
}
var_dump($lasts);
If you have a huge array, and you know the amount of IDs you will be getting, you can add in a check to terminate the loop when all required IDs have been added in to avoid redundant processing.
Otherwise, don't bother with the reverse sort, and simply keep overwriting previous values 'til the end, but I find this a cleaner approach. ^_^
I have array came from my ajax
$contestant_name_arr = $_GET['contestant_name_arr'];
print_r($contestant_name_arr);
Whenever i try to get the value of each in loop i got error because instead of this
Array ( [0] => value1,value2 )
It should be look like this:
Array (
[0] => value1
[1] => value2
)
How do I separate like that in the example above.
Either devise your url query string to be:
http://yourhost.com?contestant_name_arr[0]=value&contestant_name_arr[1]=value2
Or just simply explode;
$contestant_name_arr = explode(',', $contestant_name_arr[0]);
Need to find if two arrays match, and then where they match pull data from the mysql row in which they match. Should I use
$sql = "SELECT * FROM around";
$resultsd = $conn->query($sql);
foreach($resultsd as $rowd) {}
if (array_intersect($ar1, $ar2)) {
$sword[] = $rowd['TIM'];
}
or should I use
if (in_array($ar1, $ar2)) {
$sword[] = $rowd['TIM'];
}
Getting the arrays like:
$ar1[] = $rowd['nim'];
$ar2[] = $rowd['nim'];
Then how does one go about pulling the specific row that they match at?
I am seeing that they match, and printing out the array's okay:
Array ( [0] => dcbabcbded ) Array ( [0] => fafeafaebee [1] => afabfdefcbb [2] => dcbabcbded
But when I trying a echo the mysql data where they match I fail:
Array ( )
i would go with sql IN clause.
You have an array of customer names: $a = array('john','rob','paul');
implode array $nms = join(',',$a);
Make sql: 'SELECT * FROM tabl WHERE name IN ('.$nms.')';
do the array intersection first (or what you just have to..) to have an array of needed names.
use $new = array_intersect(Array ( [0] => dcbabcbded ), Array ( [0] => fafeafaebee [1] => afabfdefcbb [2] => dcbabcbded)) : then make valid sql as stated in my answer above.
If u are sure you've allways have array intersenct with one or none values, then make sql just with first array element: $ar[0] using sql WHERE clause.
I am trying to insert multiple rows in a MySQL table from PHP arrays. I managed with with help of other members to get set of values in a pair of brackets but when i try to insert this i get "Error: Column count doesn't match value count at row 1" I donot know where am i going wrong. my codes are as below: (The number of values i get vary according to user input)
$docno1=array();
$serialno = array();
$acc_name = array();
$debit = array();
$credit = array();
for ($i=1;$i<=$rowcount;$i++)
{
//echo 'Accountname'.$i.' :'.($_GET['accname'.$i]).'<br>';
$docno1 [] = ($_GET['docno']);
array_unshift($docno1,"");
unset($docno1[0]);
$serialno [] = $i;
array_unshift($serialno,"");
unset($serialno[0]);
$acc_name[] = ($_GET['accname'.$i]);
array_unshift($acc_name,"");
unset($acc_name[0]);
$debit[] = ($_GET['DrAmount'.$i]);
array_unshift($debit,"");
unset($debit[0]);
$credit[] = ($_GET['CrAmount'.$i]);
array_unshift($credit,"");
unset($credit[0]);
}
$sum_dr = array_sum ($debit);
$sum_cr = array_sum ($credit);
echo ' values of $multi<br>';
$multi = array(
($docno1),
($serialno), //Array for a row of fields
($acc_name),
($debit),
($credit),
($docno1)
);
print_r($multi);
$new = array();
foreach($multi as $key=>$value) {
$new[] = "'".implode("','", $value)."'";
}
echo '<br>Values of $new <br>';
print_r($new);
$query = "(".implode("), (",$new).")";
echo $query.'<br>';
mysql_query("INSERT INTO docitems (`docno`,`itemno`,`accountname`,`debit`,`credit`, `picrefno`) VALUES ".$query.";") or die('Error: ' . mysql_error());
echo "Inserted successfully";
die;
The results i get are :
values of $multi
Array
(
[0] => Array
(
[1] => 3434
[2] => 3434
)
[1] => Array
(
[1] => 1
[2] => 2
)
[2] => Array
(
[1] => Lemon
[2] => Kidney Beans
)
[3] => Array
(
[1] => 20
[2] => 10
)
[4] => Array
(
[1] => 0
[2] => 0
)
[5] => Array
(
[1] => 3434
[2] => 3434
)
)
Values of $new
Array
(
[0] => '3434','3434'
[1] => '1','2'
[2] => 'Lemon','Kidney Beans'
[3] => '20','10'
[4] => '0','0'
[5] => '3434','3434'
)
('3434','3434'), ('1','2'), ('Lemon','Kidney Beans'), ('20','10'), ('0','0'), ('3434','3434')
Error: Column count doesn't match value count at row 1
mysql_query("INSERT INTO docitems (`docno`,`itemno`,`accountname`,`debit`,`credit`, `picrefno`) VALUES ".$query.";") or die('Error: ' . mysql_error());
You are trying to insert something into 6 fields, so that $query string must have 6 values in it, or you get this error.
You have a lot of $query's that are 2 values. And that's not 6
It looks to me as if you are mapping your array the wrong way round. You're trying to add two records with six fields each, but what you're actually putting into the SQL statement are six records with two fields each.
This is why MySQL is complaining -- because you've told it you want to update six fields, but in each of the records you've given it, you've only specified two fields.
You need to build your array differently.
I assume that $docno1, $serialno, $acc_name, $debit and $credit will always all have the same number of array elements (it appears from your code that you are assuming this, so I'll follow you in your assumption).
In that case, you need to build your array something like this:
$multi = array();
foreach($docno1 as $key=>value) {
$multi[] = array(
$docno1[$key],
$serialno[$key], //Array for a row of fields
$acc_name[$key],
$debit[$key],
$credit[$key],
$docno1[$key])
}
Replace the block in your code where you set $multi with this, and your program should work.
Look at what print_r($multi) looks like now, and you'll see the difference.
(note, there are more efficient ways of writing your whole program than this, but I've focused on giving you a drop-in replacement for this specific bit, to help show you where you were going wrong, rather than simply rewriting the whole program for you)
Hope this helps.
If the error is occurring when trying to insert a row to your table, try specifying the list of fields, in the insert query -- this way, the number of data in the values clause will match the number of expected columns.
Else, MySQL expects six columns : it expects the specific inserts -- for which you didn't specify a value.