Get specific value from json response not working - php

enter image description here
I have the following json response as in the image. I would want to access specific value, like: "ROUMANIE ROVA AROMANIA" but I can't seem to get to it. I tried the following:
$response = json_decode($r->getBody(),true);
foreach($response['ParsedResults'] as $key)
{
foreach($key['TextOverlay']['Lines'] as $bla)
{
echo $bla['LineText'];
echo $bla[0]['LineText'];
}
}
If I only echo one depth, it's working. I searched for a solution but none did the trick. Thanks.

0 is the current index of the first item, $bla contains already the data you're looking for, so doing this directly should work:
echo $bla['LineText'];
Here is how the full code should look like:
$response = [
'ParsedResults' => [
[
'TextOverlay' => [
'Lines' => [
[
'LineText' => 'ROUMANIE ROVA AROMANIA',
'Words' => [
[
'WordText' => 'ROUMANIE',
'OtherData' => 'whatever'
],
[
'WordText' => 'ROVA',
'OtherData' => 'whatever'
],
[
'WordText' => 'AROMANIA',
'OtherData' => 'whatever'
],
]
]
]
]
]
]
];
foreach($response['ParsedResults'] as $key)
{
foreach($key['TextOverlay']['Lines'] as $bla)
{
echo $bla['LineText'];
}
}
Tested here: http://sandbox.onlinephpfunctions.com/code/0577e854eed73dfb33193c391acc37dd81baf982

Related

How to add "runtime_mappings" to the query for FOS/Elastica in PHP?

Need to add the next json query to php code with using FOSElasticaBundle:
"runtime_mappings": {
"Agreement": {
"type": "keyword",
"script": {
"source": "if(doc['winningBidder.edrpou'].size()>0 && doc['seller'].size()>0)\r\n{\r\nemit(\r\n doc['seller'].value+\":\"+\r\n doc['trading.id'].value+\":\"+\r\n doc['winningBidder.edrpou'].value+\":\"\r\n )\r\n}"
}
}
}
If I'm set this in simple method (\Elastica\Query)->addParam():
->addParam('runtime_mappings', [
'Agreement' => [
'type' => 'keyword',
'script' => [
'source' => "if(doc['winningBidder.edrpou'].size()>0 && doc['seller'].size()>0)\r\n{\r\nemit(\r\n doc['seller'].value+\":\"+\r\n doc['trading.id'].value+\":\"+\r\n doc['winningBidder.edrpou'].value+\":\"\r\n )\r\n}"
]
]
])
Then I get an error when I try to collect the query:
Unknown key for a START_ARRAY in [runtime_mappings].
Alternatively, you can create a query by starting with the "runtime_mappings" array and adding all other parameters after:
$query = \Elastica\Query::create(['runtime_mappings' => [
'Agreement' => [
'type' => 'keyword',
'script' => [
'source' => "if(doc['winningBidder.edrpou'].size()>0 && doc['seller'].size()>0)\r\n{\r\nemit(\r\n doc['seller'].value+\":\"+\r\n doc['trading.id'].value+\":\"+\r\n doc['winningBidder.edrpou'].value+\":\"\r\n )\r\n}"
]
],
]]);
// add other params to query...
$query->addParam();

How do I correct this one line PHP syntax?

newbie here. I have the following PHP-code for a script with Betfair-API.
if ($event->marketName == "Match Odds") {
// print_r($event);
$params = [
"marketIds" => [$event->marketId],
'priceProjection' => ['priceData' => ['EX_BEST_OFFERS']]
];
}
I need to add virtual bets. API-documentation says:
You can return virtual bets in the response when using API-NG by including the virtualise":"true" in the listMarketBook request e.g.
[
{
"jsonrpc":"2.0",
"method":"SportsAPING/v1.0/listMarketBook",
"params":{
"marketIds":[
"1.114101556"
],
"priceProjection":{
"priceData":[
"EX_BEST_OFFERS"
],
"virtualise":"true"
}
},
"id":1
}
]
How can I change my code to work? I've tried a few dozen combinations, but no luck. Sorry for being newbie.
David
$params = [
"marketIds" => [$event->marketId],
'priceProjection' => ['priceData' => ['EX_BEST_OFFERS']],
'virtualise' => true,
];

Craft Element API data output as a hash instead of an array

I'm building and API feed using Element API plugin from Craft, and I'd like the data output to be serialized as a hash, but currently it's returning an array, as example bellow:
<?php
namespace Craft;
return [
'endpoints' => [
'event-name/feed.json' => [
'elementType' => ElementType::Entry,
'criteria' => ['section' => 'event1'],
'transformer' => function(EntryModel $entry) {
$speakersList = [];
foreach ($entry->speakersList as $speaker) {
$speakersList[] = [
'name' => $speaker->speakerFullName,
'jobTitle' => $speaker->speakerJobTitle,
'company' => $speaker->speakerCompany,
];
}
return [
'speakers' => $speakersList,
];
},
],
];
And the output:
{
"data": [
{
"speakers": [
{
"name": "John Doe",
"jobTitle": "Marketing",
"company": "Company 1",
},
...
I've tried the serialize options in the documentation, but none seemed to solve the issue.
Currently if I'd like to access the speakers within data I'd have to first access index[0] to be able to get to the speakers key.
Is there a way to get rid of this array level?

Yii2 how to add multiple link in params.php

I want to add multiple link in params.php .
Is it possible to do so ?
Current view of my params in yii2 .
Params.php
<?php
return [
"api_link" => "http://example.com" ,
];
I want to add multiple link in it
Ex:
<?php
return[
"api_link" => "http://example.com" & "http://www.example.com"
];
Is it possible to do so ? I tried but unable to succeed on this attempt .
Any lead over this will really be helpful.
You can set array for each params :
<?php
return [
"api_link" => [
"http://example.com",
"http://example.com",
]
];
You can store in an array
<?php
return [
"api_link" => [
"http://example.com",
"http://example.com",
]
];
....
and for accessing you must iterate over it
$myaArray = [
"api_link" => [
"http://example.com",
"http://www.example.com",
]
];
foreach( $myArray['api_link'] as $key =>$value){
echo $value;
}
or
echo $myArray['api_link'][0];
i don't understand what you want. But you can try by multidimensional array.
$arrayname = array(
'url1' => 'nokibrokes.com',
'url2' => 'example.com'
);
return $arrayname;

Filter off some elements away from php object

Not sure if this is a php or cakephp question. I am using cakephp ver3.1.3. I have a cakephp query object $query that look like this when I call debug($query->toArray());
[
(int) 0 => object(App\Model\Entity\Customer) {
'id' => (int) 1,
'username' => 'asd',
'password' => '123',
'fullname' => 'asd',
'email_addr' => 'asd#gmail.com',
'[new]' => false,
'[accessible]' => [
'*' => true
],
'[dirty]' => [],
'[original]' => [],
'[virtual]' => [],
'[errors]' => [],
'[repository]' => 'Customers'
}
]
When I call json_encode($query), it looks like this;
[
{
"id": 1,
"username": "asd",
"password": "123",
"fullname": "asd",
"email_addr": "asd#gmail.com"
}
]
How do I process $query such that when I call json_encode($query), the output will look like this?
[
{
"email_addr": "asd#gmail.com"
}
]
To only receive the field 'email_addr' from the database, modify your query using the method select():
$query->select(['email_addr']);
If you want to remove all other fields after the query already ran, you can just loop over the array and modify the elements:
$simplified = array();
foreach($query as $row) {
$simplified[] = array(
'email_addr' => $row->get('email_addr')
);
}
echo json_encode($simplified);
On a side note, an important warning: Do not, under no circumstance, store passwords in clear text. Read this answer, specifically the section about storing passwords!

Categories