How to echo out the values of this array?

2019-02-07 01:53发布

How to echo out the values individually of this array?

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

so

 echo $value[0]; etc

I have this so far:

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;
}

This echo's out the whole string no as an array. and if i do this?

  echo $value[0};

Then i only get 2 characters of it??

EDIT:

The print_r :

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

Thanks

Chris

标签: php arrays key
4条回答
Rolldiameter
2楼-- · 2019-02-07 02:28

you need the set key and value in foreach loop for that:

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

this should do the trick :)

查看更多
太酷不给撩
3楼-- · 2019-02-07 02:33
foreach ($array as $key => $val) {
   echo $val;
}
查看更多
等我变得足够好
4楼-- · 2019-02-07 02:35

The problem here is in your explode statement

//$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;
 }

Try changing your initial $item['date'] to be 2012,04,30 if that's what you're trying to do. Otherwise I'm not entirely sure what you're trying to print.

查看更多
男人必须洒脱
5楼-- · 2019-02-07 02:49

Here is a simple routine for an array of primitive elements:

for ($i = 0; $i < count($mySimpleArray); $i++)
{
   echo $mySimpleArray[$i] . "\n";
}
查看更多
登录 后发表回答