volkanb
volkanb

Reputation: 269

How can I get the second json block of a JSON output?

This method

$bestandspflegeArray = json_decode($urlContents, true);

turns json into an array.

With this method

$hersteller = array_column($bestandspflegeArray, 'hersteller');

I get all values of "hersteller".

So how can I say if the html input matches with the value of hersteller, print all his other values too. Like id, hersteller and baujahr...and so on? In the json code down below for "bmw" for example.

[
{
"id": 2,
"hersteller": "bmw",
"modell": "{modell}",
"baujahr": "2015",
"artikelname": "nockenwelle",
"ekpreis": 149,
"verkpreis": 349,
"mengeverfuegbar": 8
},
{
"id": 3,
"hersteller": "audi",
"modell": "{modell}",
"baujahr": "2018",
"artikelname": "kotfluegel",
"ekpreis": 89,
"verkpreis": 249,
"mengeverfuegbar": 4
},
{
"id": 4,
"hersteller": "mercedes",
"modell": "{modell}",
"baujahr": "2019",
"artikelname": "getriebe",
"ekpreis": 299,
"verkpreis": 859,
"mengeverfuegbar": 3
}
]

Thank you :)

Upvotes: 0

Views: 93

Answers (1)

ADyson
ADyson

Reputation: 61859

I don't see what value array_column would give you here, to be honest.

If you want to look for all items where the "hersteller" value matches your input, and then print all the other values from that item, the simplest way is just to use a loop. (There may be a "nicer" way which someone will show you, but this is simple, understandable and will work):

$inputValue = "bmw"; //dummy input value, for demo
$bestandspflegeArray = json_decode($urlContents, true);

foreach ($bestandspflegeArray as $item)
{
  if ($item["hersteller"] == $inputValue) { print_r($item); }
}

(Obviously you could replace the print_r with something more sophisticated to create a user-friendly output, but this just shows you the general concept.)

Upvotes: 1

Related Questions