0

I'm at a 24 hour hackathon trying to solve this so, excuse if it's a little rushed.

1st for each loop works fine, I'm getting a list of categories from this url

https://dev.xola.com/api/categories

I grab the list with this

$fullurl = "https://dev.xola.com/api/categories"; 
$string .= file_get_contents($fullurl); // get json content
$json_a = json_decode($string, true); //json decoder

then loop it with this

<?
foreach($json_a as $v)
{?>
echo $v ?}>

Now with the second for each look, I want to grab the items from this url

https://dev.xola.com/api/experiences

that match the category from the last url

so samething 
$fullurl = "https://dev.xola.com/api/categories"; 
$string .= file_get_contents($fullurl); // get json content
$json_b = json_decode($string, true); //json decoder

here's the complete loop I tried

 <?
 $i=0;
foreach($json_a as $v)

$i++ {?> echo $v ?

 foreach($json_b as $x){?>
 if($v==$x):   
 echo $v
 endif;
 ?>

}?>
4
  • There are some typo mistakes please correct them first Commented Sep 22, 2013 at 13:38
  • $1=0;? PHP variables can't start with a number. Commented Sep 22, 2013 at 13:39
  • 1
    Why do you contatenate $string .= file_get_contents($fullurl)? If you concatenate 2 strings with json-stringified data - you won't be able to parse result as json. Commented Sep 22, 2013 at 13:45
  • The diffrent urls does not have the same format. The latter one has Data and then Category in it. Therefore the comparision seem faulty. Then you should not concatenate the $string because then you're comparing the string with itself. (first elements of $json_b is the same elements as $json_a) Commented Sep 22, 2013 at 13:48

2 Answers 2

2

This will create a $result array with only the data that had the categories early acquired:

<?php
$categories_url = "https://dev.xola.com/api/categories";
$data = file_get_contents($categories_url);
$categories = json_decode($data, true);

$experiences_url = "https://dev.xola.com/api/experiences";
$data = file_get_contents($experiences_url);
$experiences = json_decode($data, true);
$result = array();
foreach ($experiences['data'] as $experience)
{
    if (in_array($experience['category'], $categories))
    {
        $result[] = $experience;
    }
}
print_r($result);

And you can easily read the result with:

foreach ($result as $item)
{
    echo $item['category'], "\n";
    echo $item['desc'], "\n";
    //... other data available ...
}
Sign up to request clarification or add additional context in comments.

Comments

0

The data structure of the experiences JSON is not the same as the categories JSON, therefore if($v==$x) will never match. If you want to find all results in experiences with a category from the categories url you can do the following:

<?

    $BASE_URL = 'https://dev.xola.com/api/';

    $categories = json_decode(file_get_contents($BASE_URL . 'categories'));
    $experiences = json_decode(file_get_contents($BASE_URL . 'experiences'));
    $matches = array();

    foreach( $categories as $category ) {

        foreach( $experiences->data as $experience ) {

            if( $experience->category === $category ) {

                $matches[] = $experience;
            }

        }

    }

?>
<? foreach( $matches as $match ) : ?>
    <? echo $match->category; ?><br>
<? endforeach; ?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.