How to echo out the values of this array?

2019-02-07 02:16发布

问题:

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

回答1:

foreach ($array as $key => $val) {
   echo $val;
}


回答2:

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

for ($i = 0; $i < count($mySimpleArray); $i++)
{
   echo $mySimpleArray[$i] . "\n";
}


回答3:

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

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

this should do the trick :)



回答4:

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.



标签: php arrays key