12

如何单独回显这个数组的值?

Array ( [0] => 20120514 [1] => My Event 3 )

所以

 echo $value[0]; etc

到目前为止我有这个:

foreach (json_decode($json_data_string, true) as $item) {

$eventDate = trim($item['date']);

    // positive limit
    
   $myarray = (explode(',', $eventDate, 2));

foreach ($myarray as $value) {
   echo $value;
}

此回显将整个字符串 no 作为数组输出。如果我这样做?

  echo $value[0};

然后我只得到它的2个字符??

print_r :

Array ( [0] => 20120430 [1] => My Event 1 )
4

5 回答 5

29
foreach ($array as $key => $val) {
   echo $val;
}
于 2012-05-14T13:59:11.817 回答
3

这是原始元素数组的简单例程:

for ($i = 0; $i < count($mySimpleArray); $i++)
{
   echo $mySimpleArray[$i] . "\n";
}
于 2014-03-06T14:36:10.410 回答
1

为此,您需要在 foreach 循环中设置键和值:

foreach($item AS $key -> $value) {
echo $value;
}

这应该可以解决问题:)

于 2012-05-14T14:00:05.950 回答
1

这里的问题在于您的爆炸声明

//$item['date'] presumably = 20120514.  Do a print of this
$eventDate = trim($item['date']);

//This explodes on , but there is no , in $eventDate
//You also have a limit of 2 set in the below explode statement
$myarray = (explode(',', $eventDate, 2));

 //$myarray is currently = to '20'

 foreach ($myarray as $value) {
    //Now you are iterating through a string
    echo $value;
 }

如果您要这样做,请尝试将您的初始 $item['date'] 更改为 2012,04,30 。否则,我不完全确定您要打印什么。

于 2012-05-14T14:08:03.607 回答
0
var_dump($value)

它解决了我的问题,希望你也是。

于 2019-04-07T05:28:44.787 回答