dhad18 dhad18 - 5 months ago 23
PHP Question

Unset all data except the data associated with a given value in url

I have a file that sends a request through curl, when I hit the url which is formatted as http://www.example.com/api/something?id=24 I get a series of arrays back in JSON form. It looks like:

24: {
pe: {
id: "24",
name: "Blah",
engine_src: "blah",
each_lender: "1",
soap_request: "0",
lenders: {
0: "3",
1: "1",
2: "6",
3: "12"
}
},
lenders: {
0: {
id: "1",
platform_id: "2",
lender_id: "3",
engine_id: "24",
status: "1",
engine_lender_id: "3",
engine_lender_name: "choice #1"
},
}


There are several other numbers and arrays in the list that look similar. I need to return the array that is associated with the id in the url and only that array.

I have set a new variable which looks like

$selected = (int)$_REQUEST['pe'];


How do I unset all other values except what is in my $selected variable?

Answer

Ok, then, assuming your response is in the variable $jsonresponse...

$selected = json_decode($jsonresponse,$assoc=TRUE);
$selected = $selected[$id];