Viewed   69 times

All,

I have the following JSON Data. I need help writing a function in PHP which takes a categoryid and returns all URLs belonging to it in an array.

Something like this::

<?php
function returnCategoryURLs(catId)
{
    //Parse the JSON data here..
    return URLArray;
}
?>


{
    "jsondata": [
        {
            "categoryid": [
                20 
            ],
            "url": "www.google.com" 
        },
        {
            "categoryid": [
                20 
            ],
            "url": "www.yahoo.com" 
        },
        {
            "categoryid": [
                30 
            ],
            "url": "www.cnn.com" 
        },
        {
            "categoryid": [
                30 
            ],
            "url": "www.time.com" 
        },
        {
            "categoryid": [
                5,
                6,
                30 
            ],
            "url": "www.microsoft.com" 
        },
        {
            "categoryid": [
                30 
            ],
            "url": "www.freshmeat.com" 
        } 
    ]
}

Thanks

 Answers

5

What about something like this :


You first use json_decode, which is php's built-in function to decode JSON data :

$json = '{
    ...
}';
$data = json_decode($json);

Here, you can seen what PHP kind of data (i.e. objects, arrays, ...) the decoding of the JSON string gave you, using, for example :

var_dump($data);


And, then, you loop over the data items, searching in each element's categoryid if the $catId you are searching for is in the list -- in_array helps doing that :

$catId = 30;
$urls = array();
foreach ($data->jsondata as $d) {
    if (in_array($catId, $d->categoryid)) {
        $urls[] = $d->url;
    }
}

And, each time you find a match, add the url to an array...


Which means that, at the end of the loop, you have the list of URLs :

var_dump($urls);

Gives you, in this example :

array
  0 => string 'www.cnn.com' (length=11)
  1 => string 'www.time.com' (length=12)
  2 => string 'www.microsoft.com' (length=17)
  3 => string 'www.freshmeat.com' (length=17)


Up to you to build from this -- there shouldn't be much left to do ;-)

Thursday, November 24, 2022
5

Try something like this:

//initialize array
$myArray = array();

//set up the nested associative arrays using literal array notation
$firstArray = array("id" => 1, "data" => 45);
$secondArray = array("id" => 3, "data" => 54);

//push items onto main array with bracket notation (this will result in numbered indexes)
$myArray[] = $firstArray;
$myArray[] = $secondArray;

//convert to json
$json = json_encode($myArray);
Friday, December 23, 2022
4

I've completely reworked my answer based on your examples / current methods. No need to go the DataTables route if you are not familiar with JS.

    <!DOCTYPE html>
<html>

<body>

<h1>Customers</h1>
<div id="id01"></div>

<script>
var xmlhttp = new XMLHttpRequest();
var url = "https://httpapi.com/api/products/reseller-price.json?5&api-key=";

xmlhttp.onreadystatechange=function() {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        myFunction(xmlhttp.responseText);
    }
}
xmlhttp.open("GET", url, true);
xmlhttp.send();

function myFunction(response) {
    var arr = JSON.parse(response);
    var key;

    //add table header
    var out = "<table style="border: 1px solid black;">";
    out += "<thead><Domain><th>Add/Transfer Domain</th><th>Restore Domain</th><th>Add New Domain</th><th>Renew Domain</th></thead><tbody>";

    //loop through json keys and grab data
    for(key in arr) {
      if(arr.hasOwnProperty(key)) {

        out += "<tr><td>" +
        key +
        "</td><td>" +
        arr[key][0].pricing.addtransferdomain[1] +
        "</td><td>" +
        arr[key][0].pricing.restoredomain[1] +
        "</td><td>" +
        arr[key][0].pricing.addnewdomain[1] +
        "</td><td>" +
        arr[key][0].pricing.renewdomain[1] +
        "</td></tr>";

      }
    }

    out += "</tbody></table>";

    //put out string in defined div
    document.getElementById("id01").innerHTML = out;
}
</script>

</body>
</html>
Sunday, October 16, 2022
 
teneff
 
4

you can use it like this, in JSON format when you evaluate false value it will give you blank, and when you evaluate true it will give you 1.

$str = '[{"clientId":"17295c59-4373-655a-1141-994aec1779dc","channel":"/meta/connect","connectionType":"long-polling","ext":{"fm.ack":false,"fm.sessionId":"22b0bdcf-4a35-62fc-3764-db4caeece44b"},"id":"5"}]';

$arr = json_decode($str,true);

if($arr[0]['ext']['fm.ack'])    // suggested by **mario**
{
    echo "true";    
}
else {
    echo "false";   
}
Thursday, October 6, 2022
3

Since that URL returns a JSON response:

<?php

$content=file_get_contents("https://btc-e.com/api/2/btc_usd/ticker");
$data=json_decode($content);
//do whatever with $data now
?>
Friday, August 5, 2022
 
jamie_s
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :