0

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:"[email protected]";}

array (
  'Choice' => 
  array (
    0 => 'First',
    1 => 'Second',
  ),
  'fcode' => 'form_rajas_exmsw2rpc81anlj',
  'useremail' => '[email protected]',
)

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:"[email protected]";}';

$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>[email protected]</td></tr>

5
  • The first Values in Nested Array there is 2 value which are "first" and "second" which you wanna show? Commented Jul 26, 2015 at 7:56
  • There are many ways to convert nested array as a Value. What is the expected output ? So we can suggest accordingly Commented Jul 26, 2015 at 8:03
  • I Need To Show All values.. Commented Jul 26, 2015 at 8:04
  • I Need To Show All values dynamically. Commented Jul 26, 2015 at 8:05
  • There is an edit button. Most easy answear is to use echo '<pre>'; print_r( $decode ); echo '</pre>';. You can also make a recursion function that checks if the input is an array: ifso recall itself with that array, else print the string. Commented Jul 26, 2015 at 8:18

2 Answers 2

1

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>";
}
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks, The Results is <td width='100'>First</td><td> Second</td> But I Need <td width='100'>First, Second</td> It is Possible?
Use implode for that.
It Works $decode = unserialize($arrays); echo'<table><tr>'; foreach ($decode as $key => $value) { if (!is_array($value)) { $value = array($value); } if (is_array($value)) { $value = implode(', ', $value); } echo "<td width='100'>$value</td>"; } echo'</tr></table>';
You don't need the first $value = array($value) any more.
I change That, $decode = unserialize($arrays); echo'<table><tr>'; foreach ($decode as $key => $value) { if (is_array($value)) { $value = implode(', ', $value); } echo "<td width='100'>$value</td>"; } echo'</tr></table>';
0

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
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.