-1

I fetch $pmpro_levels from postmeta as:

when i try print_r($pmpro_levels); I have array like this:



$pmpro_levels=Array
(
    [2] => stdClass Object
        (
            [id] => 2
            [name] => PREMIUM
            [description] => <h4 style="text-align: center"><strong>3 Mois</strong></h4>
<p style="text-align: center">7€ /mois</p>
<p style="text-align: center"><span style="color: #000000">soit 0,70€ par jour</span></p>
             
        )

    [3] => stdClass Object
        (
            [id] => 3
            [name] => GOLD
            [description] => <h4 style="text-align: center"><strong>6 Mois</strong></h4>
<p style="text-align: center">6€ /mois</p>
<p style="text-align: center"><span style="color: #000000">soit 0,20€ par jour</span></p>
            
        )

    [5] => stdClass Object
        (
            [id] => 5
            [name] => MINI
            [description] => <h4 style="text-align: center"><strong>1 Mois</strong></h4>
<p style="text-align: center">10€ /mois</p>
<p style="text-align: center"><span style="color: #000000">soit 0,33€ par jour</span></p>
            ));


and i dont know how to traverse it, and how could I get array like this




Array
(
    [2]  => string data not object


    [3] => string data  not object


    [5] => string data  not object

)


Any idea how can I do this? I would like to have an array without object. Do we have to use jason? I a newbie in PHP.

4
  • Cast object to array like (array)$obj Commented Nov 6, 2020 at 18:21
  • What string data do you want? There is id, name and description. Commented Nov 6, 2020 at 18:27
  • Did you give up or what? Commented Nov 6, 2020 at 18:52
  • yes , i found the issue by another way. Many thanks Commented Nov 7, 2020 at 19:10

1 Answer 1

0

You can use something like:

$newArray = [];
foreach($pmpro_levels as $level) {
    $newArray[] = get_object_vars($level);
}

or using array_map:

$newArray = array_map(function ($level) {return get_object_vars($level);}, $pmpro_levels);

If you want to convert an array to stdClass object you can typecast it to object like so:

$myArray = [ 'title' => 'My title', 'description' => 'Some description' ];
$myObject = (object)$myArray;
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.