S
S
Spiriit2015-03-08 03:05:56
PHP
Spiriit, 2015-03-08 03:05:56

Is it possible to do a similar transformation of an array without a loop?

Good day!
I am making a conclusion from the base to the array, but the presence of the second cycle is sad, tell me please, is it possible to do without it in order to increase performance?

function mysql_query_array($query, $error="error: mysql_query_array"){
    $r = mysql_query($query) or die($error);
    $row = mysql_fetch_assoc($r);

    $row_key = array_keys($row);

    $j=0;
    do{
        for($i=0; $i<count($row_key); $i++){
            $row_array[$row_key[$i]][$j] = $row[$row_key[$i]];
        }

        $j++;
    } while($row = mysql_fetch_assoc($r));

    unset($row);

    return $row_array;
}

And what do you say, is it appropriate to unset here?

Answer the question

In order to leave comments, you need to log in

6 answer(s)
6
65536, 2015-03-08
@Spiriiit

https://php.net/manual/en/function.array-column.php

F
FanatPHP, 2015-03-08
@FanatPHP

And what - productivity is directly strongly affected?
It was a rhetorical question.
The code shows the victim of Popov's video courses. On this, in general, the question can be closed.
Nothing will help you anymore. Regardless of whether unset is needed here or not.

O
olamedia., 2015-03-08
@w999d

In essence - this function should not exist at all in any form.
Why did you need to change the keys at all? To write the same two loops on the output to expand the array back?

V
Vladimir Shiklgruber, 2015-03-08
@mnjghgmjyt

use pdo and don't teach kgo in a priestly way! at least you will learn how to write,
I understand that you need to process each line from the database in turn. if yes then here

$b  = mysql_query("SELECT ip FROM svc ");
  while ($eastb = mysql_fetch_array($b))
  {

echo''.$eastb.'<br>';
  }

S
Sergey Sova, 2015-03-08
@LestaD

My advice is to register on Habré and read a lot.
Also take yii framework or yii2 on hand

O
Oleg, 2015-03-08
@ollisso

If the task is to convert the following result:
id, name, var
1, name1, 10
2, name2, 10
3, name3, 30
into this:

array(
id => array(0 => 1,0 => 2,0 => 3),
name => array(0 => name1,0 => name2,0 => name3),
var => array(0 => 10, 0 => 20, 0 => 30),
)

then the code basically works.
And it will not be particularly possible to increase its productivity, and if it turns out, then by hundredths of a percent, i.e. it's like saving on matches.
Question to you: are you sure that this is the most important part of the code that needs to be optimized in the first place? :)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question