Having a problem with getting an api response into an array

Hi Guys
Wonder if anyone can help.
I have called data from the the semrush api.
Its a url that gets called.
see http://www.semrush.com/api-analytics/

The response from the api give me phrases.

Example

Keyword
rent to own homes
rent to own furniture
furniture rental

I am trying to get all the "phrases into an array". The"Keyword" is a header in example and can be can be Ignored or removed from the array.
I the sample attached my code breaks all the words into single words.
I want the phrase and not the single word.

How do I keep the phrases as they are provided?

rent to own homes
rent to own furniture
furniture rental

and not a list of single words

    to
    own
    homes
    buy
    part
    homebuy
matthewdacruzAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
Sample?
0
Julian HansenCommented:
If your text is just a string that you want to get into lines then
<?php
$input = "Keyword
rent to own homes
rent to own furniture
furniture rental";

$lines = explode("\n",$input);
array_shift($lines); // Loose the keyword line
var_dump($lines);

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
matthewdacruzAuthor Commented:
Hi sorry I thought the sample got uploaded
Here it is.
semrush-sample.txt
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

Ray PaseurCommented:
Please call the API and get the response back into a string variable.  Then print that variable with var_dump() and post the output here in the code snippet.

Also, if there is a man page associated with the API response - something that tells us what we expect to get back - please post a link to that, too.  Thanks!
0
Ray PaseurCommented:
Ahh, now that I see that we can be a bit more specific.  Please run var_dump($response_us); and post the output in the code snippet.
0
Slick812Commented:
as julianH, suggests , using the PHP explode( ) function , is one of the most useful things to deal with delimited strings; ;

// if your API return is somethin like
$return = 'Keyword;Search Volume;CPC;Competition;Number of Results search engine optimization;22200;17.79;0.97;31300000';

//get phrases  in to an array
$phrases = explode(";", $return);

Open in new window

I am uncertain, what to show you as how, or if you should use the phrases in this array for output?
0
matthewdacruzAuthor Commented:
Hi Ray
See attached snippet as requested
output.txt
0
matthewdacruzAuthor Commented:
Hi Julian
I tweaked my code based on your sample.
Got the phrases I wanted. However duplicates are in output.

example
------------
how to lose weight fast
how to lose weight
lose
weight watchers
how to lose weight fast

In my code I use
do the following

//Put all arrays into singe unique array
$data_merged_semrush_related_keywords_english = array_unique(array_merge($data_unique_arr_us, $data_unique_arr_uk));

should array_unique not get rid of the duplicate phrases?
0
Ray PaseurCommented:
This gave me a sensible result.  You might want to eliminate array position zero.  I did not see any duplicates; the response appears to be one-line-at-a-time.

<?php // demo/temp_matthewdacruz.php
/**
 * http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28680538.html#a40798652
 */
error_reporting(E_ALL);
echo '<pre>';

// REEAD THE RESOURCE
$url = 'http://api.semrush.com?type=phrase_related&key=obsfucated&display_limit=3&export_columns=Ph&phrase=weight+loss&database=us&filter=%2B%7CNq%7CGt%7C5000%7C%2B%7CCp%7CGt%7C1.50';
$htm = file_get_contents($url);

// SEPARATE THE LINES
$arr = explode(PHP_EOL, $htm);

// SHOW THE WORK PRODUCT
print_r($arr);

Open in new window

0
matthewdacruzAuthor Commented:
Hi Ray
What I have done is i have called data from the US and then From The UK.
Gives me the common phrases in english.

I then merged the arrays using array_merge and then tried to remove duplicate phrases using array_unique.

Once I have my group of arrays and I merge them into one list whay wont the duplicate phrases be removed?
0
Slick812Commented:
You seem to get a different response string that is shown on their API instrucions -
   http://www.semrush.com/api-analytics/

for the keyword search ? As the response they show is    ;   delimited ?

it says that if you add this -
   &export_escape=1

it will at least segregate the phrases with  double quotes  "how to lose weight fast"

but you can not have the space delimited string you get, an ever hope to use a programming to sort and get any phrases
0
Slick812Commented:
??? could the use of your -
          type=phrase_related

or the -
    &display_limit=3

be eliminating the   ;   delimiter?  an API can not send back Unorganized and non-separated segments (phrases) and ever be useful to the requestors?
0
matthewdacruzAuthor Commented:
I was looking for a particular dataset that has filtered out what I dont need. Its calling on a particular dataset using the api parameters.

I have solved the phrase issue. Its just the duplicates I need removed at this point.
0
Ray PaseurCommented:
Array_unique() will remove the duplicates.
http://php.net/manual/en/function.array-unique.php

This works and produces a sensible response:
<?php // demo/temp_matthewdacruz.php

/**
 * http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28680538.html#a40798652
 */
error_reporting(E_ALL);
echo '<pre>';

// READ THE US RESOURCE
$url = 'http://api.semrush.com?type=phrase_related&key=obfuscated&display_limit=6&export_columns=Ph&phrase=weight+loss&database=us&filter=%2B%7CNq%7CGt%7C5000%7C%2B%7CCp%7CGt%7C1.50';
$htm = file_get_contents($url);

// APPEND THE UK RESOURCE
$url = 'http://api.semrush.com?type=phrase_related&key=obfuscated&display_limit=6&export_columns=Ph&phrase=weight+loss&database=uk&filter=%2B%7CNq%7CGt%7C5000%7C%2B%7CCp%7CGt%7C1.50';
$htm .= file_get_contents($url);

// SEPARATE THE LINES
$arr = explode(PHP_EOL, $htm);

// REMOVE ANY DUPLICATES
$arr = array_unique($arr);

// SHOW THE WORK PRODUCT
print_r($arr);

Open in new window

0
Julian HansenCommented:
should array_unique not get rid of the duplicate phrases?

What duplicates?

Each of the following is a different element in the array - and each element is unique
how to lose weight fast
how to lose weight
lose
weight watchers
how to lose weight fast

Open in new window

Uniqueness is a relative term depending on context - not sure what you are asking ?
0
matthewdacruzAuthor Commented:
What I would like to achieve is a list where each phrase is listed once.
You will see 'how to lose weight fast' appears in line 1 and line 5
0
matthewdacruzAuthor Commented:
Thanks guys got it working.
Thanks for the help all
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.