How can I iterate through JSON output in PHP and pick just the second key => value?

php loop through json object
how to get data from json array in php
php loop through json array
php json foreach key => $value
json_decode
iterate through json string php
php for loop
php iterate object properties

how can I iterate trough this JSON output and put just "hersteller" values into an array and print them out? Do I need just one for-loop-block or a nested for-loop with $i and $j?

[
{
"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 :)

$arr = json_decode($str, true);  //converts JSON string into array
$arr_hersteller = array_column($arr, 'hersteller'); //returns an array containing "hersteller" values

[Solved] Looping through JSON decoded object - PHP, Having a small issue trying to figure out the basics to loop through an object and display some data. //The $ouput variable holds the JSON below $output = json_decode($outputDetails); Notice : Trying to get property of non-object in… To return an object, set the second parameter in the function to true . If you’re working with JSON (JavaScript Object Notation) and either need to convert a JSON string to array or object and loop through it or vice-versa, take an array or object and convert it to a JSON string to return, both can be done in PHP or JavaScript.

<?php
$json = '[
{
"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
}
]';
//convert json to array json_decode(array, true) 
//this true mean convert it to array instead of object
$array = json_decode($json, true);

//create new empty array
$hersteller = array();

//iterate
foreach($array as $key => $val){
  if(isset($val['hersteller'])){
    $hersteller[] = $val['hersteller'];  
  }
}

echo "<pre>";
//print the array
print_r($hersteller);

Object Iteration - Manual, As the output shows, the foreach iterated through all of the visible properties that could be accessed. To take it a step further, the Iterator interface may be� Output: By following the above steps, you can easily convert any type of PHP array into JSON data format using json_encode() method. If you want to send or exchange small amount of data from one location another location then you can convert the data into JSON format and export it easily.

With the help of "codeit" I did following which solved my problem:

$output = array();

$urlContents = file_get_contents(#Here is a URL to get the JSON value#);

$bestandspflegeArray = json_decode($urlContents, true);
$hersteller = array_column($bestandspflegeArray, 'hersteller');

for ($i=0; $i < count($hersteller); $i++) { 
            $output[] = $hersteller[$i];  
        }

echo json_encode($output);

Thank you :)

PHP and JSON, The json_encode() function is used to encode a value to JSON format. This example shows how to loop through the values of a PHP object: <?php $jsonobj � I’m now trying to write an AJAX call to that PHP function, iterate through the JSON array, and display it in the browser in non-JSON formatting. My AJAX code looks like the following:

php loop through array Code Example, Get code examples like "php loop through array" instantly right from your google search results echo $item;. 4 foreach (array_expression as $key => $value). The JSON string has a wrong format. It has been quoted and quotes inside it have been escaped, like would have been the effect of addslashes.. Your code does not reveal where this tampering happened, but it would be best you would remove the cause of this.

The List of the 10 Most Common Mistakes That PHP Developers , Buggy PHP Code: The 10 Most Common Mistakes PHP Developers Make As a result, when we go through the second foreach loop, “weird stuff” appears To still get the benefit of using references in foreach loops without running the risk of []; public function setValue($key, $value) { $this->values[$key] = $value; } public� Note To loop over a object array in JSON formatted string, you need to converts it to JavaScript object (with JSON.parse() or $.parseJSON()) before parse it with jQuery $.each(). See this example – JQuery Loop Over JSON String […]

Foreach loops in PHP: 6 facts you should know, Almost every programming language supports foreach loops, and PHP is no As you can see in the output, while the second array key is actually a string Foreach is usually the easiest way to iterate through arrays, and it turns out to If you want to look at the test code just click down here to show it. Get my free PDF:. What I would like to do is to loop through this response, probably with forreach(), resulting in a list containing the rating and excerpt for each review in the response. Any guidance / direction is greatly appreciated.. Below is the JSON I'm working with. (it is the response from the Yelp API).

Comments
  • Does this answer your question? php: loop through json array