cannot put msSQL column names into array - php

I'm trying to put the column names of a msSQL table into an array. Using
for ($i = 0; $i < mssql_num_fields($result); ++$i) {
echo mssql_field_name($result) . "<br><br>";
}
the column names print to the screen just fine. Also get_type() shows that they are strings. However, when I try to put them into an array like this:
$column_names = array();
for ($i = 0; $i < mssql_num_fields($result); ++$i) {
$current_column = mssql_field_name($result);
array_push($column_names, $current_column);
}
var_dump($column_names); gives me an array (albeit the expected length) of boolean values. All false. I would expect to see an array containing the names of all my columns. What am I doing wrong here? Thank you

Looks like you are missing the $i argument on the mssql_field_name call. Try this maybe:
$column_names = array();
for ($i = 0; $i < mssql_num_fields($result); ++$i) {
$column_names[] = mssql_field_name($result, $i);
}
http://php.net/manual/en/function.mssql-field-name.php

Related

Php Xml loop through addAttribute

How can I loop through addAttribute to write multiple values.
So far it looks like this:
for($i = 0; $i < 10; $i++)
{
$this->title->addAttribute('names', "'".$this->Data[$i]->names.';');
}
I get this error:
SimpleXMLElement::addAttribute(): Attribute already exists
current the xml looks like this(without the loop, with a static value name):
<button names=Tim;"/>
but I want it to look like this after the loop:
<button names=Tim;Tom;Ted"/>
how do I achieve this?
you can achieve this by first create an array with all your name, and then implode your array in your attribute
This should work
PHP
$names = [];
foreach($this->Data as $key => $value) {
$names[] = $value->names;
}
$this->title->addAttribute('names', implode(';', $names));
You do not need to use addAttribute() to add value to exist attribute. Only add new value to attribute like bottom code
for($i = 0; $i < 10; $i++)
{
$this->title['names'] .= $this->Data[$i]->names.';'
}
Check result in demo
In your code sample, you'r trying to add same attribute. Instead of it, you can create an array with names. Then you can join array elements with a glue string with implode.
$names = [];
for($i = 0; $i < 10; $i++)
{
$names[] = $this->Data[$i]->names;
}
$this->title->addAttribute('names', implode(';',$names));

Sum each row of a multidimensional array

I want to sum each row of a multidimensional array :
$number = array
(
array(0.3,0.67, 0.3),
array(0.3,0.5,1),
array(0.67,0.67,0.3),
array(1,0.3,0.5)
);
The result what i want is like this :
row1 = 1.27
row2 = 1.8
row3 = 1.64
row4 = 1.8
I already tried this code :
for($i = 0; $i < 4; $i++) {
for($j = 0; $j < 5; $j++) {
$sumresult[] = array_sum($number[$i][$j]);
}
}
But it appear an error like this :
Warning: array_sum() expects parameter 1 to be array, double given in xxxx
array_sum needs array not values. Do like this:
for($i = 0; $i < 4; $i++) {
$sumresult[] = array_sum($number[$i]);
}
its because you are passing an value instead of the array containing it.
One correct solution would be:
$sumResult = array();
foreach($number as $values){
$sumResult []= array_sum($values);
}
print_r($sumResult);
Should do the trick ;)
It's easier to just map the array_sum() function to the array to sum the inner arrays:
$sumresult = array_map('array_sum', $number);

Why does accessing an array element throw an array-as-string error in PHP?

I've done some searching and I didn't find any posts that quite answered my question.
I have a PHP array generated, for the sake of argument, with this code:
$i = 5;
for($i = 0; $i < $j; $i++) {
$multiArray[0][$i] = $i;
$multiArray[1][$i] = $i;
}
When I try to access it with:
for($i = 0; $i < $j; $i++) {
echo "$multiArray[0][$i]";
echo "$multiArray[1][$i]";
}
I get:
Notice: Array to string conversion on line 3
Notice: Array to string conversion on line 4
...x4
When I replace echo with printf("%d", $multiArray[0][$i]) then it prints fine. Why do I have to explicitly tell PHP that I'm asking for an int when the element I'm accessing is clearly an int (and PHP knows it, via var_dump())? I'm not accessing the array, but an element within the array.
Thanks
Simple double quoted variable interpolation supports up to one nested element. In other words, "foo[0][1]" is interpreted as "{$foo[0]}[1]". That means it tries to interpret the array $foo[0] as a string at that point to interpolate it into the string.
But using quotes here at all is entirely nonsensical. You don't want string interpolation, you just want to output a variable value:
echo $multiArray[0][$i];
just try this:
for($i = 0; $i < $j; $i++) {
echo $multiArray[0][$i];
echo $multiArray[1][$i];
}
Your code is parsing the string not the array, try to remove the quotes.The [] brackets after the array are considered as a string not the as a paremeter.Use the code below
<?php
$j = 5;
for($i = 0; $i < $j; $i++) {
$multiArray[0][$i] = $i;
$multiArray[1][$i] = $i;
}
for($i = 0; $i < $j; $i++) {
echo $multiArray[1][$i];
}
Hope this helps you

Making associative array

My goal is to make ant assoc array from the values of for loop.
//$from_time value is 6 and $to_time value is 23
for ($i = $from_time; $i <= $to_time; $i++) {
$working_time_array[] = $i;
}
echo json_encode($working_time_array);
The output I get on AJAX success, and when I console.log it, I get result as such :
["6",7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23]
Preferred result is
["6","7","8","9","10"]... etc
The only difference between the two results is one result set contains integers and the other contains strings. If you want those values to be strings just cast them when assigning them to the array:
for ($i = $from_time; $i <= $to_time; $i++) {
$working_time_array[] = (string) $i;
}
This really shouldn't be necessary unless your client side is expecting strings only.
You would need to cast $i to a string before pushing it to the array.
for ($i = $from_time; $i <= $to_time; $i++) {
$working_time_array[] = (string)$i;
}
why would you convert int to string?
for your goal this should work
for ($i = $from_time; $i <= $to_time; $i++) {
$working_time_array[] = "$i";
}
echo json_encode($working_time_array);

Undefined offset with an array

Why can't I use variable which contains a number to specify an array value:
$info(array)
$mySQLHeadings(array)
$infoString(empty String)
$mySQLHeadingString(empty string)
for ($i=0; $i<=count($info) ; $i++){
if($info[$i] != ""){
$mySQLHeadingString .= $mySQLHeadings[$i] . ",";
$infoString .= "'". $info[$i] ."',";
}
}
PHP says it's an undefined offset $i in the arrays. How can I correct it or do something similar. Thank you so much.
You should write for ($i = 0; $i < count($info); $i++). Array indexes start from 0 while count() starts from 1.
Also don't use count() inside for loop - move it before:
$count_info = count($info);
for ($i = 0; $i < $count_info; $i++)
If $info is numerically indexed, you can access elements with $i, but not further than max index !
count() gives you the array length, but max numeric index is (length - 1)
so :
for ($i=0; $i < count($info); $i++) {
//....
}

Categories