Jump to content
alexarpad2003

Multi-Dimensional Arrays in PHP

Recommended Posts

Posted

Avem exemplul asta :


<?php
$produs = array
(
"laptop" => "1200 ron",

"samsung" => "1000 ron",

"lapte" => "5 lei"
);

echo "<table border='1'>";
foreach ($produs as $nume => $pret)
{
echo "<tr>";
echo "<td>";
echo $nume;
echo "</td>";

echo "<td>";
echo $pret;
echo "</td>";
echo "</tr>";
}
echo "</table>";

?>

Cum as putea scrie un array de genul "laptop" => "1200 ron" => "da".

Iar foreach-ul sa fie ($produs as $nume => $pret=>$stoc), bineinteles sa adaug si


echo "<td>";
echo $stoc;
echo "</td>";
echo "</tr>";

Stiu ca nu exista asa ceva, sunt prea obosit si am explicat cum am putut.

Deci ce alternative pot gasi la problema mea ?

Posted (edited)

Uite cateva alternative mai jos care pot sa rezolve multe din problemele tale legate de php :).

<?php
$produse = array
(
array('intrebare' => 'Unde pot invata php?', 'raspuns' => 'http://php.net', 'multumesc'=>'Multumesc!' ),
array('intrebare' => 'Unde pot invata php?', 'raspuns' => 'http://www.lynda.com/PHP-tutorials/php-with-mysql-essential-training/435-2.html', 'multumesc'=>'Multumesc!' ),
array('intrebare' => 'Unde pot invata php?', 'raspuns' => 'http://google.com', 'multumesc'=>'Multumesc!' ),
);

echo "<table border='1'>";
foreach ($produse as $produs)
{
extract($produs);
echo "<tr>
<td>$intrebare</td>
<td>$raspuns</td>
<td>$multumesc</td>
</tr>";
}
echo "</table>";
?>

Edited by devacanta
Posted (edited)


$lista = array(
'laptop' => array(
'pret' => 1200,
'stoc' => 'da',
),
'masina' => array(
'pret' => 40000,
'stoc' => 'nu',
),
);

foreach($lista as $nume=>$val) {
echo $nume;
echo $val['pret'];
echo $val['stoc'];
}

varianta 2


$lista = array(
0 => array(
'nume' => 'laptop',
'pret' => 1200,
'stoc' => 'da',
),
1 => array(
'nume' => 'masina',
'pret' => 40000,
'stoc' => 'da',
),
);
foreach ($lista as $row) {
echo $row['nume'];
echo $row['pret'];
echo $row['stoc'];
}

Edited by Birkoff
  • Upvote 1

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...