2012-10-25 60 views
0

我有一個數組分配關聯數組到PHP可變

陣列([0] =>數組([用戶名] => phizy [ID] => 1)[1] =>數組([用戶名] => rapik [ID] => 4)[2] =>數組([用戶名] => ASAS [ID] => 5))

我如何提取,並把它們在PHP變量?

<li><?php $username." with id of ".$userid."</li>"; 

謝謝

回答

1

嘗試這樣的:

foreach($array as $arobj) 
{ 
    ?> 
    <li><?php $arobj['username']." with id of ".$arobj['id']; ?></li> 
    <?php 
} 

,或者,如果你想賦給變量,然後,

foreach($array as $arobj) 
{ 
    $username = $arobj['username']; 
    $userid = $arobj['id']; 
    ?> 
    <li><?php $username." with id of ".$userid; ?></li> 
    <?php 
} 
+0

'

  • <?php echo $ username。「with id of」。$ userid; ?>
  • '(很好的回答只是錯過了回聲) – chim

    +0

    如果你把它寫成<?='而不是'<?php',你可以省略'echo'。 – Barmar

    +0

    非常感謝你!有用! – Phizy

    0

    只是爲有趣的,如果你必須在數組鍵變量賦值的基地,你可以試試這個:

    foreach($array as $subArr) 
    { 
        foreach($subArr as $key=>$value) 
        { 
        $$key=$value; 
        } 
        echo "<li>$username with id of $id.</li>"; 
    } 
    

    Document

    1

    如果你真的想要把一個數組變量的所有按鍵,你可以使用功能extract()