Need help parsing JSON decoded grandchildren with PHP - php

foreach($response_2['items'] as $item) {
$item['track']['name'] = (array)$item['track']['name'];
foreach($item['track']['name'] as $key => $value) {
echo $value . "<br />";
}
$item['track']['artists']['name'] = (array)$item['track']['artists']['name'];
foreach($item['track']['artists']['name'] as $key => $value) {
echo $value . "<br />";
}
$item['track']['duration_ms'] = (array)$item['track']['duration_ms'];
foreach($item['track']['duration_ms'] as $key => $value) {
echo $value . "<br />";
}
}
...to parse this response: http://pastebin.com/uxBF3Kxe# (after I've decoded it).
All the values for ['track']['name'] and ['track']['duration_ms'] are echoed correctly, but nothing is echoed for ['track']['artists']['name'].
What am I doing wrong here?
Thanks!

['track']['artists'] is array :)
Try this ;)
foreach($item['track']['artists'] as $key => $value) {
echo $value['name'] . "<br />";
}
Btw...
This is better solution ;)
$response_2 = json_decode($json, true);
foreach($response_2['items'] as $item) {
echo $item['track']['name'] . "<br />";
foreach($item['track']['artists'] as $key => $value) {
echo $value['name'] . "<br />";
}
echo $item['track']['duration_ms'] . "<br />";
}
/** Added separator */
$response_2 = json_decode($json, true);
foreach($response_2['items'] as $item) {
echo $item['track']['name'] . "<br />";
echo implode(',', array_map(
function ($value) {
return $value['name'];
},
$item['track']['artists']
)
);
echo "<br />";
echo $item['track']['duration_ms'] . "<br />";
}

Related

Iterate through Nested array in PHP

I have a nested array on this link Array Sample
I am using code below to parse this, but second and beyond depth it's returning nothing. However tried with recursive function.
printAllValues($ArrXML);
function printAllValues($arr) {
$keys = array_keys($arr);
for($i = 0; $i < count($arr); $i++) {
echo $keys[$i] . "$i.{<br>";
foreach($arr[$keys[$i]] as $key => $value) {
if(is_array($value))
{
printAllValues($value);
}
else
{
echo $key . " : " . $value . "<br>";
}
}
echo "}<br>";
}
}
What I am doing Wrong? Please help.
Version of J. Litvak's answer that works with SimpleXMLElement objects.
function show($array) {
foreach ($array as $key => $value) {
if (!empty($value->children())) {
show($value);
} else {
echo 'key=' . $key . ' value=' . $value. "<br>";
}
}
}
show($ArrXML);
You can use recurcive function to print all values:
function show($array) {
foreach( $array as $key => $value) {
if (is_array($value)) {
show($value);
} else{
echo 'key=' . $key . ' value=' . $value. "<br>";
}
}
}
show($ArrXML);

How to detect URL redirections and separate each header?

I have a problem: i want to detect all redirections of a 3xx response code and separate each header of URL. I investigated that get_headers function follows redirections and put it in an array of each component of redirection. I know that "fb.com" redirects 3 times as you can see on this page:
I want to do the same thing of that page, and am trying to do so:
But my PHP code seems to be wrong. Here it is:
<?php
$header = array();
$header[] = array();
$header_url = #get_headers('http://fb.com',1);
/*
foreach ($header_url as $key => $value){
echo $key . " = " . $value . "<br />";
}
*/
$a = 0;
foreach($header_url as $key => $value){
if(is_int($key)){
$header[$a][] = $value;
$a++;
} else if(!is_int($key) && !is_object($key)){
$header[0][$key] = $value;
} else if(!is_int($key) && is_object($key)){
foreach ($key as $key2 => $value2){
$header[$key2][$key] = $value2;
}
}
}
echo "<h1>ONE</h1>";
foreach ($header[0] as $key => $value) {
echo $key . " = " . $value . "<br />";
}
echo "<h1>TWO</h1>";
foreach ($header[1] as $key => $value) {
echo $key . " = " . $value . "<br />";
}
echo "<h1>THREE</h1>";
foreach ($header[2] as $key => $value) {
echo $key . " = " . $value . "<br />";
}
?>
I would appreciate help on what i am doing wrong.
THANKS SO MUCH AND HAVE A NICE DAY DEV!

How to pass all the values in an array which is processed by for loop?

<?php
$xml = simplexml_load_file("sample.xml");
echo " ". $xml->getName() . "<br />";
foreach($xml->children()->children() as $child)
{
$a=$child->getName() . "<br />";
array_push($a);
echo $a;
}
?>
Try this
<?php
$arr = array();
$xml = simplexml_load_file("sample.xml");
echo " ". $xml->getName() . "<br />";
foreach($xml->children()->children() as $child)
{
$a=$child->getName() . "<br />";
$arr[] = $a;
echo $a;
}
?>

Magento Get Products By Manufacturer

I have the following code which gets me a list of manufacturers:
$attribute = Mage::getModel('eav/entity_attribute')
->loadByCode('catalog_product', 'manufacturer');
$valuesCollection = Mage::getResourceModel('eav/entity_attribute_option_collection')
->setAttributeFilter($attribute->getData('attribute_id'))
->setStoreFilter(0, false);
$preparedManufacturers = array();
foreach($valuesCollection as $value) {
$preparedManufacturers[$value->getOptionId()] = $value->getValue();
}
if (count($preparedManufacturers)) {
echo "<h2>Manufacturers</h2><ul>";
foreach($preparedManufacturers as $optionId => $value) {
echo "<li>" . $value . " - (ID:" . $optionId . ")</li>";
}
echo "</ul>";
}
How do I get the first product of each manufacturer?
Thanks
try this
if (count($preparedManufacturers)) {
echo "<h2>Manufacturers</h2><ul>";
foreach($preparedManufacturers as $optionId => $value) {
<!-- add the this code for get first item id -->
$firstProductId=Mage::getModel('catalog/product')->getCollection()
->addStoreFilter(0)
->addAttributeToFilter('manufacturer',$optionId)->getFirstItem()->getId();
echo "<li>" . $value . " - (ID:" . $optionId . ")</li>";
}
echo "</ul>";
}

Multidimensional array loop not displaying the result for objects

I am getting the result from the SOAP client as an response. I just have to analyze the structure of the parameter and display the results accordingly. I have the following srucutre for SerialEquipment and i am getting the results peoperly for all the parameters except the Esaco parameter. The Esaco parameter is an array object and it resides inside the SerialEquipment array. I am trying to fetch the response from Esaco array object but getting an error as Invalid arguments supplied for foreach. I am not understanding how to get the results for Esaco Parameter by looping properly.Just a small mistake i am doing in looping the array.
Code:
foreach($Serial as $key => $obj)
{
echo "<b>"."Serial Equipment=>" . $key . "</b>"."<br>";
echo "Code=>". $obj->Code . "<br>";
echo "Desc Short=>". $obj->Desc_Short . "<br>";
echo "Desc Long=>". $obj->Desc_Long . "<br>";
foreach($obj->Esaco as $key2 => $obj2)
{
if($obj2 === null){
// doesn't contain Esaco
break;
}
else{
echo "<b>"."Esaco=>" . $key2 . "</b>"."<br>";
echo "EsacoMainGroupCode=>". $obj2->EsacoMainGroupCode . "<br>";
echo "EsacoMainGroupDesc=>". $obj2->EsacoMainGroupDesc . "<br>";
echo "EsacoSubGroupCode=>". $obj2->EsacoSubGroupCode . "<br>";
echo "EsacoSubGroupCode=>". $obj2->EsacoSubGroupDesc . "<br>";
echo "EsacoSubGroupCode=>". $obj2->EsacoGroupCode . "<br>";
echo "EsacoSubGroupCode=>". $obj2->EsacoGroupDesc . "<br>";
}
}
}
if($parameter['aktion'] == 'getVehicle')
{
$vehicle = getVehicleValuation();
$Serial=$vehicle['SerialEquipment'];
$VehicleFuel=$vehicle['VehicleFuel'];
foreach($VehicleFuel as $key => $obj2)
{
echo "Fuel Type=>". $obj2->Fuel_Type . "<br>";
echo "Fuel Type Code=>". $obj2->Fuel_Type_Code . "<br>";
echo "ECE_Unit=>". $obj2->ECE_Unit . "<br>";
echo "ECE_In=>". $obj2->ECE_In . "<br>";
echo "ECE_Out=>". $obj2->ECE_Out . "<br>";
echo "ECE_All=>". $obj2->ECE_All . "<br>";
echo "ECE_CO2=>". $obj2->ECE_CO2 . "<br>";
}
}
This is my structure for SerialEquipment:
if($parameter['aktion'] == 'getVehicle')
{
$vehicle = getVehicleValuation();
if(($serials = $vehicle['SerialEquipment']) === null){
// doesn't contain SerialEquipment
break;
}
foreach($serials as $serial){
print "Code =>" . $serial->Code . "<br>";
print "Desc Short =>" . $serial->Desc_Short . "<br>";
//...
foreach($serial->Esaco as $esaco){
print "EsacoMainGroupCode =>" . $esaco->EsacoMainGroupCode. "<br>";
print "EsacoMainGroupDesc =>" . $esaco->EsacoMainGroupDesc. "<br>";
//...
}
}
}
And for the VehicleFuel:
if($parameter['aktion'] == 'getVehicle')
{
$vehicle = getVehicleValuation();
$Serial=$vehicle['SerialEquipment'];
$VehicleFuel=$vehicle['VehicleFuel'];
$fuelType = $VehicleFuel->Fuel_Type;
// if there is only going to be one VehicleFuel object Vehicle, then just do..
echo "Fuel Type =>". fuelType->Fuel_Type . "<br>";
echo "Fuel Type Code =>". $fuelType->Fuel_Type_Code . "<br>";
// if there will be more than one, you will want to use a loop...
foreach($fuelType as $obj)
{
echo "Fuel Type=>". $obj->Fuel_Type . "<br>";
echo "Fuel Type Code=>". $obj->Fuel_Type_Code . "<br>";
echo "ECE_Unit=>". $obj->ECE_Unit . "<br>";
echo "ECE_In=>". $obj->ECE_In . "<br>";
echo "ECE_Out=>". $obj->ECE_Out . "<br>";
echo "ECE_All=>". $obj->ECE_All . "<br>";
echo "ECE_CO2=>". $obj->ECE_CO2 . "<br>";
}
}
The key Esaco is an object not an array. You should change your second foreach.
foreach($Serial as $key => $obj) {
echo "Serial Equipment=>" . $key . "<br>";
echo "Code=>". $obj->Code . "<br>";
echo "Desc Short=>". $obj->Desc_Short . "<br>";
echo "Desc Long=>". $obj->Desc_Long . "<br>";
foreach($obj->Esaco as $key2 => $obj2) {
echo $obj2;
//...
}
}

Categories