My arrays
a:3:{s:6:"Choice";a:2:{i:0;s:5:"First";i:1;s:6:"Second";}s:5:"fcode";s:26:"form_rajas_exmsw2rpc81anlj";s:9:"useremail";s:26:"rajasekarang.cud#gmail.com";}
array (
'Choice' =>
array (
0 => 'First',
1 => 'Second',
),
'fcode' => 'form_rajas_exmsw2rpc81anlj',
'useremail' => 'rajasekarang.cud#gmail.com',
)
my php code
$arrays = 'a:3:{s:6:"Choice";a:2:{i:0;s:5:"First";i:1;s:6:"Second";}s:5:"fcode";s:26:"form_rajas_exmsw2rpc81anlj";s:9:"useremail";s:26:"rajasekarang.cud#gmail.com";}';
$decode = unserialize($arrays);
foreach($decode as $key => $value) {
echo '<td width="100">' . $value . '</td>';
}
My Error is :
Notice: Array to string conversion in....
The first Values in Nested Array.
How to convert nested array as a Value?
I need to show like this,
<tr><td>First,Second</td><td>form_rajas_exmsw2rpc81anlj</td><td>rajasekarang.cud#gmail.com</td></tr>
If $value is an array, you need a nested loop.
foreach ($decode as $key => $value) {
if (!is_array($value)) {
$value = array($valule);
}
foreach ($value as $subvalue) {
echo "<td width='100'>$subvalue</td>";
}
}
If you can have multiple levels of nesting, you should write a recursive function that handles each level.
If you want a sub-array to be shown as a comma-separated list, you can use implode.
foreach ($decode as $key => $value) {
if (is_array($value)) {
$value = implode(', ', $value);
}
echo "<td width='100'>$subvalue</td>";
}
you have nested array populated with elements of diferent size so you will always get an error. so in your foreach loop, you can check if the value is an array or not, like for instance
if (count($value)>1){
//code here like for instance echo $value[0]...
}//not recomended becuse an array can have only one value, but you can if you know you will put at least 2
or
if(is_array($value)..
Me I will do the following:
foreach($thing as $thingy =>$that){
//check if array or not
if(is_array($that)){
foreach($that as $t)
//do whatever
}else
//do whatever
}
Related
This is probably very simple ... but I can't figure it out. I need to use array keys as variables. I have three tabs that need to use unique variables to access data.
$array = array(
'items' => 'latest',
'itemsFollow' => 'follow',
'itemsExp' => 'expired'
);
while ( ($stuff = current($array)) !== FALSE ) {
echo '<div id="'.key($array).'" class="tab-content grid flex">';
foreach(/*array_key*/ as $item) { //need foreach($items, foreach($itemsFollow and foreach($itemsExp
// do stuff
}
echo '</div>';
next($array);
}
Loop through your array with foreach. There is no need to loop twice (nested loops) in a one dimensional array. Use if, elseif or case statements to do custom stuff depending on the value.
foreach ($array as $key => $value){
echo '<div id="'.$key.'" class="tab-content grid flex">';
if($value=="latest"){
//do stuff
}
elseif($value=="follow"){
//do stuff
}
elseif($value=="expired"){
//do stuff
}
}
I have an array that looks like this:
$array = array(
array(
"http://google.com",
"Google"
),
array(
"http://yahoo.com",
"Yahoo"
)
);
What is the simplest way to loop through it. Something like:
foreach ($array as $arr) {
// help
}
EDIT: How do I target keys, for example, I want to do:
foreach ($array as $arr) {
echo '<a href" $key1 ">';
echo ' $key2 </a>';
}
In order to echo out the bits you have to select their index in each array -
foreach($array as $arr){
echo ''.$arr[1].'';
}
Here is an example.
Use nested foreach() because it is 2D array. Example here
foreach($array as $key=>$val){
// Here $val is also array like ["Hello World 1 A","Hello World 1 B"], and so on
// And $key is index of $array array (ie,. 0, 1, ....)
foreach($val as $k=>$v){
// $v is string. "Hello World 1 A", "Hello World 1 B", ......
// And $k is $val array index (0, 1, ....)
echo $v . '<br />';
}
}
In first foreach() $val is also an array. So a nested foreach() is used. In second foreach() $v is string.
Updated according to your demand
foreach($array as $val){
echo ''.$val[1].'';
}
The easiest way to loop through it is:
foreach ($array as $arr) {
foreach ($arr as $index=>$value) {
echo $value;
}
}
EDIT:
If you know that your array will have always only two indexes then you can try this:
foreach ($array as $arr) {
echo "<a href='$arr[0]'>$arr[1]</a>";
}
First modify your variable like this:
$array = array(
array("url"=>"http://google.com",
"name"=>"Google"
),
array("url"=>"http://yahoo.com",
"name"=>"Yahoo"
));
then you can loop like this:
foreach ($array as $value)
{
echo '<a href='.$value["url"].'>'.$value["name"].'</a>'
}
The way to loop through is,
foreach($array as $arr)
foreach($arr as $string) {
//perform any action using $string
}
Use the first foreach loop without { } for the simplest use.
That can be the most simple method to use a nested array as per your request.
For your edited question.
Wrong declaration of array for using key.
$array = array(
"http://google.com" => "Google",
"http://yahoo.com" => "Yahoo" );
And then, use the following.
foreach ($array as $key => $value)
echo "<a href='{$key}'>{$value}</a>";
This doesn't slow down your server's performance.
In modern versions of php, you can assign (destructure) subarray values to variables of your choosing.
A good tutorial: https://stitcher.io/blog/array-destructuring-with-list-in-php
Code: (Demo)
$array = [
["http://example1.com", "Example 1"],
["http://example2.com", "Example 2"]
];
foreach ($array as [$key1, $key2]) {
echo "$key2\n";
}
Output:
Example 1
Example 2
To be honest, I'd probably name the variables $url and $text respectively.
I have two array output (using preg_match_all), for example: $name[1] and $family[1].
i need to put these arrays together, and i use foreach as this:
foreach( $name[1] as $name) {
foreach( $family[1] as $family) {
echo $name.$family.'<br />';
}
}
but it don't work.
(each foreach loop works separately)
Assuming they have matching keys for the loop:
foreach( $name as $key => $value) {
echo $value[$key] . $family[$key] . '<br />';
}
This will go through each match for $name and print it out, and then print out the corresponding $family with it. I don't think you want to hardcode [1].
If you do, I'm a little confused and would like to see a var_dump of both $name and $family for clarification.
$together= array();
foreach( $name as $key => $n) {
$tuple= array('name'=>$name[$key],'family'=>$family[$key]);
$together[$key]= $tuple;
}
foreach( $together as $key => $tuple) {
echo "{$tuple['name']} {$tuple['family']}<br />";
}
Use array_combine():
Creates an array by using the values from the keys array as keys and
the values from the values array as the corresponding values.
PHP CODE:
$nameFamilly=array_combine($name[1] , $family[1]);
foreach( $nameFamilly as $name=>$familly) {
echo $name.$family.'<br />';
}
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??
The print_r :
Array ( [0] => 20120430 [1] => My Event 1 )
foreach ($array as $key => $val) {
echo $val;
}
Here is a simple routine for an array of primitive elements:
for ($i = 0; $i < count($mySimpleArray); $i++)
{
echo $mySimpleArray[$i] . "\n";
}
you need the set key and value in foreach loop for that:
foreach($item AS $key -> $value) {
echo $value;
}
this should do the trick :)
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.
var_dump($value)
it solved my problem, hope yours too.
Is there a way to traverse an array such as $_POST to see the field names and not just the values. To see the values I do something like this.
foreach ($_POST as $value){
echo $value;
}
This will show me the values - but I would like to also display the names in that array. If my $_POST value was something like $_POST['something'] and it stored 55; I want to output "something".
I have a few select fields that I need this for.
You mean like this?
foreach ( $_POST as $key => $value )
{
echo "$key : $value <br>";
}
you can also use array_keys if you just want an array of the keys to iterate over.
You can also use array_walk if you want to use a callback to iterate:
function test_walk( &$value, $key )
{
...do stuff...
}
array_walk( $arr, 'test_walk' );
foreach ($_POST as $key => $value) {
echo $key; // Field name
}
Or use array_keys to fetch all the keys from an array.
foreach ($_POST as $key => $value){
echo $key.': '.$value.'<br />';
}
If you just want the keys:
foreach (array_keys($_POST) as $key)
{
echo $key;
}
Or...
foreach ($_POST as $key => $value)
{
echo $key;
}
If you want both keys and values:
foreach ($_POST as $key => $value)
{
echo $key, ': ', $value;
}
For just the keys:
$array = array_keys($_POST);
Output them with:
var_dump($array);
-or-
print_r($array);