Reputation: 784
First of all, sorry if this sounds like a rather commonly asked question, but I've looked all over without finding something satisfactory.
This is my code:
$query = mysql_query("SELECT * FROM Rezept");
$allRecipes = array();
for($i = 0; $i < mysql_num_rows($query); $i++){
$average = 0;
$row = mysql_fetch_row($query);
$review = mysql_query("Select Stern FROM Bewertung WHERE Rezept_NR LIKE ". $row[0]);
for($n = 0; $n < mysql_num_rows($review); $n++){
$stern = mysql_fetch_row($review);
$average += $stern[3];
}
$average /= mysql_num_rows($review);
$recipe = array(
"Name" => $row[2],
"Kurzb" => $row[3],
"Bild" => $row[4],
"Bewertung" => $average
);
$allRecipes = array_merge_recursive($allRecipes, $recipe);
}
print_r($allRecipes);
}
Anyway. What I get:
Array ( [Name] => Array ( [0] => Test [1] => Teeeest [2] => Test [3] => Test [4] => Test [5] => Test [6] => Test [7] => Test [8] => Test [9] => Test [10] => Test [11] => Test [12] => Test [13] => Test [14] => Tests [15] => Tests [16] => Tests ) [Kurzb] => Array ( [0] => Tes [1] => Testtest [2] => Test [3] => Test [4] => Test [5] => Test [6] => Test [7] => Test [8] => Test [9] => Test [10] => Test [11] => Test [12] => LOLTest [13] => Test [14] => Testersersts [15] => Testersersts [16] => Testersersts ) [Bild] => Array ( [0] => [1] => [2] => [3] => [4] => [5] => [6] => [7] => [8] => [9] => [10] => [11] => [12] => [13] => [14] => [15] => [16] => ) [Bewertung] => Array ( [0] => [1] => [2] => 0 [3] => [4] => [5] => [6] => [7] => [8] => [9] => [10] => [11] => [12] => [13] => [14] => [15] => [16] => ) )
It's pretty close, but not what I'm looking for.
What I want:
Array([0] => array([Name] => Test [Kurzb] => Test [Bild] => something) [1] => array([Name] => Test [Kurzb] => Test [Bild] => something))
Is it a problem that I'm trying to use multiple arrays with the same keys? Am I aproaching this with the wrong idea? I'm a complete newbie when it comes to programming...
Upvotes: 1
Views: 69
Reputation: 734
$query = mysql_query("SELECT * FROM Rezept");
$i =0;
while(($result = mysql_fetch_row($query)) !== FALSE)
{
$newArray[$i][] = $result;
$i++;
}
return $newArray;
1 simply just query db 2 fetch it with while.
Upvotes: 0
Reputation: 167172
Use array_combine
for this...
$a = array('green', 'red', 'yellow');
$b = array('avocado', 'apple', 'banana');
$c = array_combine($a, $b);
This would return:
Array
(
[green] => avocado
[red] => apple
[yellow] => banana
)
So in your code, change this:
$allRecipes = array_combine($allRecipes, $recipe);
Upvotes: 2