简体   繁体   中英

Remove indexes of multidimensional array to convert to a list of many one dimensional arrays PHP

Apologies for vague questioning, I'm fairly new. I've been searching but can't seem to find the solution that fits my scenario.

I am trying to store the output of a looped mysql query as a variable to be used outside the loop, exactly as it would be if I were to print_r the result within the loop. I am trying to save on overhead as I have inherited a function which uses this large dataset frequently and am trying to reduce the calls to the database by saving the output instead of querying database each time.

Currently I have -

$data = array();
$sql = mysqli_query($con, SELECT * FROM my_table);
while($row = mysqli_fetch_assoc($sql)){
  $data[] = $row;
}
print_r($data);

which is resulting in:

Array ( 
[0] => Array ( [id] => 1 [name] => john [age] => 44  ) 
[1] => Array ( [id] => 2 [name] => paul [age] => 30  )
[2] => Array ( [id] => 3 [name] => george [age] => 25  )
)

BUT i am needing an output like this, ie without top indexes...

$output =

Array ( [id] => 1 [name] => john [age] => 44  ) 
Array ( [id] => 2 [name] => paul [age] => 30  )
Array ( [id] => 3 [name] => george [age] => 25  ) ;

It is late and my brain eludes me, many thanks for any pointers.

EDIT: Will take a mulligan on this one, turns out my problem was a formatting one where I did not escape ($) sign when assigning it as a variable whilst saving the output to file. Thanks all for your replies and setting me straight

The result and the expected result is bascially the same thing because as @Nick pointed out you need to have top level keys (0,1,2). (How would you else identify which id belongs to john and to paul and to george?)

It's simply not possible to store an array like this:

Array ( [id] => 1 [name] => john [age] => 44  ) 
Array ( [id] => 2 [name] => paul [age] => 30  )
Array ( [id] => 3 [name] => george [age] => 25  )

It must be stored like this: (with top level keys (in this case:0,1 and 2))

Array ( 
    [0] => Array ( [id] => 1 [name] => john [age] => 44  ) 
    [1] => Array ( [id] => 2 [name] => paul [age] => 30  )
    [2] => Array ( [id] => 3 [name] => george [age] => 25  )
)

The value of $data[0]['name'] would be john, the value of $data[1]['name'] would be paul etc.

If you refer to $data[0] it contains the array: Array ( [id] => 1 [name] => john [age] => 44 )

If you refer to $data[1] it contains the array: Array ( [id] => 2 [name] => paul [age] => 30 ) etc

It's not possible to store the values as you want but it is of course possible to have the output as you wish:

//$data_value is an array in every row 
foreach($data as $data_value) {
    print_r($data_value);
}

which would produce the output:

Array
(
    [id] => 1
    [name] => john
    [age] => 44
)
Array
(
    [id] => 2
    [name] => paul
    [age] => 30
)
Array
(
    [id] => 3
    [name] => george
    [age] => 25
)

Do this:

$data = [];
$i = 0;
while ($row = mysqli_fetch_assoc($sql)) {
    foreach( $row as $field => $value) {
        $data[$i][$field] = $value:
    }
    $i++;
}

print_r($data)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM