0

I am using an API call that returns an array. It's an array not a sting. I want to wrap the array in an unordered list. I need two separate lists.

1) I want to wrap everything (including sub arrays) in an unordered list.

<ul>
  <li>[name]</li>
  <li>[name]</li>
  <li>[name]</li>
</ul>

2) I just want to wrap [name] in an unordered list.

<ul>
  <li>[1347037874]</li>
    <ul>
      <li>[level_id]</li>
      <li>[name]</li>
      <li>[canceled]</li>
      <li>[cancelDate]</li>
      <li>and all the rest</li>
    </ul>
  <li>[1347037874]</li>
    <ul>
      <li>[level_id]</li>
      <li>[name]</li>
      <li>[canceled]</li>
      <li>[cancelDate]</li>
      <li>and all the rest</li>
    </ul>
</ul>

I assign the API call like this:

$member_id = $logged_in_WP_user_id;
$member_levels = member_levels($member_id);

For explanation only (this line is not in my function...when I output $member_levels like this: htmlspecialchars(print_r(get_member($member_id),true)) I get the following:

Array
( 
    [1347037874] => stdClass Object
        (
            [Level_ID] => 1347037874
            [Name] => HFM-Cardiac Resistance Training Program
            [Cancelled] => 
            [CancelDate] => 
            [Pending] => 
            [UnConfirmed] => 
            [Expired] => 
            [ExpiryDate] => 
            [Active] => 1
            [Status] => Array
                (
                    [0] => Active
                )

            [Timestamp] => 1349804951
            [TxnID] => WL-2-1347037874
        )

    [1347037875] => stdClass Object
        (
            [Level_ID] => 1347037875
            [Name] => HFM-Official Heart Health Guide
            [Cancelled] => 
            [CancelDate] => 
            [Pending] => 
            [UnConfirmed] => 
            [Expired] => 
            [ExpiryDate] => 
            [Active] => 1
            [Status] => Array
                (
                    [0] => Active
                )

            [Timestamp] => 1349804951
            [TxnID] => WL-2-1347037875
        )
)

I have tried several ways to wrap the array in an array, it get close but still can make it work right.

Tks!

p.s. here is what I am now using to make the first list but how do I get the other key values to display as in unordered list under the key value =>Name.

$member_levels = ember_levels($member_id);

  $output  = '';    
  $output .= '<ul>';                            
  foreach($member_levels as $level) {
    $output .= '<li>' . $level_array[]=$level->Name . '</li>';
  }
  $output .= '</ul>';

  print_r($output);
2
  • 1
    You might want to give an example output for a list element. Commented Oct 23, 2012 at 3:49
  • PHP doesn't have unordered lists. It has associative arrays and indexed arrays (which are actually just a special case of associative arrays where the keys are successive integers). Commented Oct 23, 2012 at 3:53

2 Answers 2

1
foreach($member_levels as $level)
{

  $level_array[]=$level->Level_ID;
  $name_array[]=$level->Name;
}
Sign up to request clarification or add additional context in comments.

1 Comment

That works for my first list. I did; however, change the key value to =>Name but how do you get all the other key value to display as a an unordered list under =>Name? I added the code to the bottom of my post above as to how I used your suggestion to output my list.
1

If you want to wrap something in another level of array, do:

$wrapped_thing = array($something);

If this doesn't do what you want, then you haven't described it well.

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.