Asked  1 Year ago    Answers:  5   Viewed   16 times

I have a small problem with my mysqli related function. Here is the code:

$query = "SELECT * FROM uploads";
 if ($result = $mysqli->query($query)) {
   while ($row = $result->fetch_assoc()) {
     return $row;
  }

The problem is that when I use this function $row appears to be an array, but with only the first value from the query result in it. But if I try to return var_dump($row) instead, function displays array as expected, with all the values from the query result. Can you please explain why is this happening and how to return an array with complete query result correctly. Thanks!

 Answers

2
$data  = array();
$query = "SELECT * FROM uploads";
if ($result = $mysqli->query($query)) {
  while ($row = $result->fetch_assoc()) {
     $data[] = $row;
  }
}
return $data;
Thursday, April 1, 2021
 
Jubair
 
5

In PHP 5.4, you can do this: $var = myFunction(param1, param2)['specificIndex'];.

Another option is to know the order of the array, and use list(). Note that list only works with numeric arrays.

For example:

function myFunction($a, $b){
    // CODE
    return array(12, 16);
}

list(,$b) = myFunction(1,2); // $b is now 16
Thursday, April 1, 2021
 
Gilko
 
2

return statement must be after the for loop if you want whole list in your case during the first loop only the value is returned. just remove the return from for loop it will work fine.

def portfolioReturns (securities, quintilesNo, perReturns):
    '''
    this function receives 
    1)securities: array with the security names and values ** for the purpose of our work the names
    should already be sorted
    2)quintilesNo: the number of portfolios we want to create 
    3)perReturns: an array with the returns that will be used for performance measuremnt

    It returns an array with the returns for each portfolio

    '''

    # we calculate the number of securities per portfolio 
    stdFolioSize = np.divmod(securities.size, quintilesNo)[0] # we take the floor division
    folioReturn = [] # pd.DataFrame()
    # we create portfolios with equal number of securities except of the last one where we use all the remaining securities
    for k in range(0, quintilesNo, 1): # in folio list we store the name of the securities we must include in each portfolio
        if k < (quintilesNo - 1):           
            folioList = securities.index.get_level_values(1)[k * stdFolioSize : (k + 1) * stdFolioSize]

        else: # the last portfolio will also include the remainder securities 
            folioList = securities.index.get_level_values(1)[k * stdFolioSize : securities.size]

        # now that we have the list of the securities to be included in the folio, we use the table
        # with the periodical returns to check the performance. The portfolio we construct is equally weighted

        # first we drop one index(the first index of the country) and then we store all the periodical returns in one-array 
        perRetFinalTable = pd.DataFrame(perReturns.reset_index(level = 0, drop = True)).T  

        # using the list of the bonds we want to include in our portfolio we pick the bond returns and
        # we store them in one array. Then we calculate the folio return
        folio = perRetFinalTable[folioList]
        folioReturn = np.append(folioReturn, folio.sum(axis = 1) * (1 / folio.size))
        folioReturn = pd.DataFrame(folioReturn).T
        # folioReturn = pd.Series(folioReturn).T

    return (folioReturn)
Sunday, August 29, 2021
3

You are defining a function f = @(x,y) [0, 1]; which has the input parameters x,y and the output [0,1]. What else do you expect to happen?

Update:

This should match your description:

g=@(x,y)[zeros(size(x)),ones(size(y))]
g(x',y')
Friday, September 3, 2021
5
echo ${!${false}=getArray()}[0];

This is how it works, step by step

${false}=getArray()

assigns the result of getArray to a variable with an empty name ('' or null would work instead of false)

!${false}=getArray()

negates the above value, turning it to boolean false

 ${!${false}=getArray()}

converts the previous (false) value to an (empty) string and uses this string as a variable name. That is, this is the variable from the step 1, equal to the result of getArray.

${!${false}=getArray()}[0];

takes index of that "empty" variable and returns an array element.

Some more variations of the same idea

echo ${1|${1}=getArray()}[1];
echo ${''.$Array=getArray()}[1];

function p(&$a, $b) { $a = $b; return '_'; }
echo ${p($_, getArray())}[1];

As to why getArray()[0] doesn't work, this is because php team has no clue how to get it to work.

Monday, November 1, 2021
 
mzereba
 
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :