Jump to content

Pegar item de uma 'array'


Toninho
Go to solution Solved by Ivo Vicente,

Recommended Posts

Viva, 

Partindo do principio que essa string é json, é necessário que seja convertida para array com recurso ao método json_decode

<?php

// JSON para Array associativo
$toArray = json_decode('{"result":{"title":"ALMOXARIFE I","company":"","rating":"4","reviews_count":"1242"}}', true);
var_dump($toArray["result"]["title"]);

// De Array associativo para Objecto/stdClass
$toObject = (object) $toArray;
var_dump($toObject->result["title"]);

 

Edited by Ivo Vicente

Feito é melhor que perfeito

Link to comment
Share on other sites

Ivo, obrigado

veja completo:

<?php
$api_url = '';
    $load = [
      'api_key' => '',
      'url' => '',
      'parse' => [
        'title' => 'h1 >> text',
        'company' => '.jobsearch-InlineCompanyRating a >> text',
        'rating' => 'meta[itemprop=ratingValue] >> content',
        'reviews_count' => 'meta[itemprop=ratingCount] >> content',
        'description' => '#jobDescriptionText >> text'
      ]
    ];


    $postdata = json_encode($load);
    $ch = curl_init($api_url);
    curl_setopt($ch,CURLOPT_POST, true);
    curl_setopt($ch,CURLOPT_POSTFIELDS, $postdata);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
    curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
    $result = curl_exec($ch);
    curl_close($ch);

    //echo $result;
	echo $result->result->title; 
	
	?>

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.