2017-02-18 57 views
1
<?php 

$playerBox = json_decode($_POST['player_data'], true); 

echo print_r($playerBox); 

echo $playerBox['name']; 

?> 

我发送的数组由ajax到PHP,这是我的PHP代码,我的意图是获取每个值并为其创建一个html p。简单得到json_decode数组值,错误不知道

但我无法获得每个值,我不知道为什么每个人都可以使用它来获得它,我不能。

这里是print_r的

Array 
(
    [0] => Array 
     (
      [id] => 1 
      [name] => Jonny 
      [number] => 27 
     ) 

    [1] => Array 
     (
      [id] => 2 
      [name] => dx 
      [number] => 28 
     ) 

) 

的$ _ POST [ 'player_data')的错误我得到

<br /> 
<font size='1'><table class='xdebug-error xe-notice' dir='ltr' border='1' cellspacing='0' cellpadding='1'> 
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>(!)</span> Notice: Undefined index: name in C:\wamp\www\objecttest\directory\class-mail.php on line <i>7</i></th></tr> 
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr> 
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr> 
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0004</td><td bgcolor='#eeeeec' align='right'>135312</td><td bgcolor='#eeeeec'>{main}()</td><td title='C:\wamp\www\objecttest\directory\class-mail.php' bgcolor='#eeeeec'>..\class-mail.php<b>:</b>0</td></tr> 
</table></font> 

如何获得每个值,创建这样?

id name number 

1 Johnny  27 

2 dx   28 
+1

回声$ playerBox [0] [ '名称'];使用会得到正确的值 – JYoThI

回答

0

你必须使用这样

$playerBox[0]['id']; 
$playerBox[0]['name']; 
$playerBox[0]['number']; 
+0

为什么我必须把0? –

+0

用于循环以在记录中迭代。 0是索引号。 –

0

指数只要使用foreach叠代阵列访问阵列,并得到各指标值

<table border="1px" > 
     <thead> 
     <tr> 
     <th>ID</th> 
     <th>Name</th> 
     <th>Number</th> 
     </tr> 
    </thead> 
    <tbody> 
<?php 

    foreach($playerBox as $play) 
    { 
    ?> 
    <tr> 
     <td><?php echo $play['id']; ?> </td> 
     <td><?php echo $play['name']; ?></td> 
     <td><?php echo $play['number']; ?></td> 
    </tr> 
    <?php 
    } 
?> 
</tbody> 
</table>