Search code examples
phpyalinqo

YALINQO: Query deep array


Array
(
[0] => Array
    (
        [EndpointArn] => arn:aws:sns
        [Attributes] => Array
            (
                [Enabled] => true
                [Token] => APA91
                [CustomUserData] => Alcatel
            )

    )

[1] => Array
    (
        [EndpointArn] => arn:aws:sns:ap
        [Attributes] => Array
            (
                [Enabled] => true
                [Token] => APA91bGC
                [CustomUserData] => Oppo
            )

    )

)

I get this array from source. I'm new to yalinqo, so after try and error, i happen to get to print CustomUserData with this code:

$result= from($Model2['Endpoints'][0]['Attributes'])->select(function($np) { return $np; });

print $result->toArrayDeep()['CustomUserData']; 

Now I want to print EndpointArn based on CustomUserData which I selected, but my code not work.

This is the code:

print 
from($Model2['Endpoints'])
->select(function($np){ return $np['EndpointArn']; })
->where(function($np){return $np['Attributes']['CustomUserData'] == 'Oppo';})
->toArrayDeep()
;

Error message:

Warning: Illegal string offset 'Attributes' in /home/u
Warning: Illegal string offset 'CustomUserData' in /home/u  
Warning: Illegal string offset 'Attributes' in /home/u    
Warning: Illegal string offset 'CustomUserData' in /home/u

This code:

print 
from($Model2['Endpoints'])
->select(function($np){ return $np['EndpointArn']; })
->where(function($np){return $np['Attributes']['CustomUserData'] == 'Oppo';})
;

Error message:

Catchable fatal error: Object of class YaLinqo\Enumerable could not be converted to string in

Can someone shade some light here?

Thanks.


Solution

  • After try and error, here is the answer:

    print 
    from($Model2['Endpoints'])
    ->where(function($np){return $np['Attributes']['CustomUserData'] == 'Oppo';})
    ->select(function($np){ return $np['EndpointArn']; })
    ->toString()
    ;
    

    Result

    arn:aws:sns:ap