M.Ferguson
M.Ferguson

Reputation: 3

Putting values from an array into a two dimensional array with keys

new to php, I have read dozens of 'insert data into arrays' posts on the net, but none seem to describe my exact problem, (or I am too ignorant to recognize it) So here we go.

I get some data from a SQL database. I want to present this in a table. I have a working script that does this, but it has a slight malfunction, it doesn't show any empty results (rows) in the table. To get the desired result I my attempt 2.0 starts now with an empty table, with all the rows, and fill them in with data later.

$names = array('Alice', 'Bob', 'Charles', 'Dick', 'Emy');
for ($i=0; $i< count($names); $i++) 
 {
    $empty_table[] = array("Name" => $names[$i], "Total" => 0, "Wins" => 0, "Losses" => 0) ;
 }

This gives me an array that I can display as a table, with 5 data rows, each containing a value for the field with a Name key (Alice to Emy) and otherwise filled with values set to 0 for all other keys. It looks like:

Name Total Wins Losses

Alice 0 0 0

Bob 0 0 0

Charles 0 0 0

Dick 0 0 0

Emy 0 0 0

From the database I want to fill in the values that are 0, IF and only IF there is something in the database. I get the data with

for ($i=0; $i< count($names); $i++) {
$sql = "SELECT
        SEVERAL THINGS    
        WHERE Something = '".$names[$i]."'";
    $result = mysqli_query($conn,$sql);
// Not the real query obviously, but that part works and gives the intended result.
    while ( $row = mysqli_fetch_assoc($result) ) {
    Do something with the result in $row;
    }   // end while
}       // end for

$row is an array with the same structure as one of the values of $empty_table. Example:$row could be: Array ( [Name] => Bob [Total] => 10 [Wins] => 7 [Losses] => 3 )

Now the question. How do I get the one dimension array $row into the two dimension array $empty_table (replacing the '0' values) , given that $empty_table always has a fixed 5 elements but there can be any number between 0 and 5 '$row's in the while loop depending on what is in the database?

If I try:

$empty_table[] = $row;

or

$empty_table[$row['Name']] = $row;

I only get rows added to the existing array with '0' values, not rows replaced. I do understand that the solution is between the '[]' but I can't figure out put something there that relates to the proper $row[].

Upvotes: 0

Views: 86

Answers (2)

Rashi
Rashi

Reputation: 1186

I am just pretending that the $row is from db.

<?php

$names = array('Alice', 'Bob', 'Charles', 'Dick', 'Emy');

for ($i=0; $i< count($names); $i++)
 {
    $empty_table[] = array("Name" => $names[$i], "Total" => 0, "Wins" => 0, "Losses" => 0) ;
 }

$row = array("Name" => 'Alice', "Total" => 5, "Wins" => 6, "Losses" => 4);
$found = false;
foreach($empty_table as $k=>$a) {
    if($a['Name'] == $row['Name']) {
        $found = true;
        break;
    }
}
if($found) {
    echo 'index found'.$k.'\n';
    $empty_table[$k] = array_replace($empty_table[$k], $row);
    var_dump($empty_table);
}

Upvotes: 0

Marcos P&#233;rez Gude
Marcos P&#233;rez Gude

Reputation: 22158

I think that you can change the array construction to this:

for ($i=0; $i< count($names); $i++) {
     $empty_table[$names[$i]] = array("Name" => $names[$i], "Total" => 0, "Wins" => 0, "Losses" => 0) ;
}

So your array keys will be the name of the person. With this you can identify what row is each moment. The array should be constructed like this:

$empty_table = Array(
  "John"=> Array("name"=>"John", "total"=>10, "wins"=>3, "loses"=>7),
  "Mary"=> Array("name"=>"Mary", "total"=>10, "wins"=>5, "loses"=>5),
);

So you can access to the values like this:

$john_row = $empty_table["John"];

Upvotes: 2

Related Questions