0

I need to get the value of [text] from all these object - class.

I can get values from arrays But from these class object thing in array ,I am unable to get the values of [text].

Any help would be greatly appreciated.

Array
(
 [0] => stdClass Object
 (
    [text] => time
    [tr] => Array
    (
        [0] => stdClass Object
        (
            [text] => while
            [syn] => Array
            (
                [0] => stdClass Object
                (
                    [text] => when
                )
            )
        )
        [1] => stdClass Object
        (
            [text] => occasion  
            [syn] => Array
            (
                [0] => stdClass Object
                (
                    [text] => moment
                )
                [1] => stdClass Object
                    (
                        [text] => day   
                    )
                    [2] => stdClass Object
                        (
                            [text] => date  
                        )
                )
            )
        )
    )
)
Nayan Dave
  • 1,078
  • 1
  • 8
  • 30
newcoder
  • 1
  • 1
  • 4
  • please post expected outcome+code you have tried so for........ – Alive to die - Anant May 28 '20 at 06:29
  • `array[0]->text` is what you need – Marian May 28 '20 at 06:35
  • This is actually a api based result so the code is big. I used json decode to output this results var_dump( json_decode($myArray)); then i used print_r($myArray->response; but then it gives my above result. While I wanted the value of "text" like in first array [text] => time , but i want "text" values from all. – newcoder May 28 '20 at 06:38
  • @hlfrmn And also `$array[0]->tr[0]->text`, `$array[0]->tr[1]->text, `$array[0]->tr[0]->syn[0]->text`, and so on. – Barmar May 28 '20 at 06:40
  • 1
    To get them all you will probably need to write a recursive function. – Barmar May 28 '20 at 06:40

1 Answers1

0

It's not the most elegant solution but something like this should do the trick:

<?php
$data = [
    [
        'text' => 'time',
        'tr' => [
            'text' => 'while',
            'syn' => [
                'text' => 'when'
            ],
            [
                'text' => 'occasion',
                'syn' => [
                    [
                        'text' => 'moment'
                    ],
                    [
                        'text' => 'day'
                    ],
                    [
                        'text' => 'date'
                    ]
                ]
            ]
        ]
    ]
];

$data = json_decode(json_encode($data));

function recurseProperties($object) {
    $text = [];
    foreach ($object as $property => $value) {
        if ($property === 'text') {
            echo $property . '  =>  ' . $value . "\n";
            $text[] = $value;
        } else {
            $text = array_merge($text, recurseProperties($value));
        }
    }
    return $text;
}

function extractText($data) {
    $text = [];
    foreach ($data as $object) {
        $text = array_merge($text, recurseProperties($object));
    }

    return $text;
}

var_dump(extractText($data));

Alternatively if you are comfortable working with arrays and it is a json source, you can just do:

  json_decode($jsonString, true)

Which will convert it to an associative array see: https://www.php.net/manual/en/function.json-decode.php

Adam Hall
  • 11
  • 2