Adding to array from multiple foreach statements - php

I'm trying to add the dynamically generated variables $title and $price to the array $list.
This works to a certain degree. The code creates an array with the keys where they have a title and price value.
The price value is correct and different for each key. However, it seems that only the first title result is added, creating the following array (same title untill key 30)
[0]=> array(2) { ["title"]=> string(57) "Gibson Les Paul ** Nr.1 Gibson dealer ** 18 gitaarwinkels" ["price"]=> string(25) " € 300,00 " } [1]=> array(2) { ["title"]=> string(57) "Gibson Les Paul ** Nr.1 Gibson dealer ** 18 gitaarwinkels" ["price"]=> string(25) " € 100,00 " }
Looking at the code I think it is because the first foreach loop is only executing the second one.
I know the correct values for $title are there, because when I isolate the title foreach loop like this:
foreach ($titlehit as $titles) {
$title = $titles->nodeValue;
echo "$title";
}
30 different $title result are displayed
$url = "https://url.com";
$html = new DOMDocument();
#$html->loadHtmlFile($url);
$xpath = new DOMXPath($html);
$titlehit = $xpath->query("//span[#class='mp-listing-title']");
$pricehit = $xpath->query("//span[#class='price-new']");
$list = array();
$i = 0;
foreach ($titlehit as $titles) {
foreach ($pricehit as $prices) {
if ($i >=5 && $i <=35) {
$title = $titles->nodeValue;
$price = $prices->nodeValue;
$list[] = array(
'title' => $title,
'price' => $price
);
}
$i++;
}
}
How can I get the array $list to hold both the correct title and price values? Thanks for your help.

Assuming that for each title, there is one price i.e there is a 1:1 title-to-price ratio in the corresponding arrays, you will only need 1 loop.
$list = array();
$i = 0; // initialize
// Assuming $titlehit AND $pricehit have the same number of elements
foreach ($titlehit as $titles) {
// Assuming $pricehit is a simple array. If it is an associative array, we need to use the corresponding key as the index instead of $i. We'll get to this once you can confirm the input.
$prices = $pricehit[$i];
// Not entirely sure why you need this condition.
if ($i >=5 && $i <=35) {
$title = $titles->nodeValue;
$price = $prices->nodeValue;
$list[] = array(
'title' => $title,
'price' => $price
);
}
$i++;
}

Not exactly enough code here to know for sure but it looks like you need to reset your iterator $i with each iteration of your foreach loop:
$list = array();
foreach ($titlehit as $titles) {
$i = 0;
foreach ($pricehit as $prices) {
if ($i >=5 && $i <=35) {
$title = $titles->nodeValue;
$price = $prices->nodeValue;
$list[] = array(
'title' => $title,
'price' => $price
);
}
$i++;
}
}

Related

Counting rows by array_push

I'm a bit stuck here
I have an array that I'm exporting with laravel excel and I'm basically creating a category row and then all subsequent item rows that belong to that category.
How can I properly add a counter for every array_push to the groupItem array so that I can count every row between the groupItem push and set the row with the category info to bold?
Basically I only want to bold the row that has the data for category_code, category_name and category_desc so I would need to iterate based on the array_push for the category info I believe
I think I would need to set a count, increase count for the categoryItem array_push, store that count in an array and then set those array stored rows to bold?
$allgroupResult= array();
foreach($prices->groups as $group){
$groupItem = array();
$groupItem["category_code"] = $group->category_code;
$groupItem["category_name"] = $group->category_name;
$groupItem["category_desc"] = $group->category_desc;
array_push($allgroupResult, $groupItem);
foreach($group->skus as $sku){
$skuItem = array();
$skuItem["item_code"] = $sku->sku_info->item->item_code;
$skuItem["identifier"] = $sku->sku_info->identifier;
foreach($sku->prices as $price => $amount){
$skuItem[] = $amount;
}
$skuItem[] = strip_tags(html_entity_decode($sku->sku_info->item->desc));
foreach ($sku->sku_info->details as $details) {
$skuItem[] = $details->details1;
$skuItem[] = $details->details2;
$skuItem[] = $details->details3;
}
array_push($allgroupResult, $skuItem);
}
}
$name = 'File Export';
$build = Excel::create($name, function ($excel) use ($allgroupResult) {
$excel->setTitle('File Export');
$excel->sheet('File Export', function ($sheet) use ($allgroupResult) {
$sheet->fromArray($allgroupResult);
// bold the column headers
$sheet->getStyle('A1:'.$sheet->getHighestColumn().'1')->getFont()->setBold(true);
// $count = 2;
// foreach($excelRows as $one){
// $sheet->fromArray($one, null, 'A2');
// $sheet->row($count, function($row) {
// $row->setFontWeight('bold');
// });
// $count += count( $one ) + 1;
// }
// set the width for the columns that are used
$sheet->setWidth('A', 10);
$sheet->setWidth('B', 24);
$sheet->setWidth('C', 20);
$sheet->setWidth('D', 12);
$sheet->setWidth('E', 10);
$sheet->setWidth('F', 16);
$sheet->setWidth('G', 16);
$sheet->setWidth('H', 16);
$sheet->setWidth('I', 16);
$sheet->setWidth('J', 16);
$sheet->setWidth('K', 16);
});
})->download('xlsx');
Why not creating another folded array inside $allgroupResult for each category, so having structure like this:
array(1) {
[0] =>
array(4) {
'category_code' =>
string(13) "category_code"
'category_name' =>
string(13) "category_name"
'category_desc' =>
string(13) "category_desc"
'skus' =>
array(3) {
[0] =>
string(4) "sku1"
[1] =>
string(4) "sku2"
[2] =>
string(4) "sku3"
}
}
}
and then you can just do count($item['skus']) whenever you need to get the number of products in every category. In order to do this , try the following modification to your foreach loop:
foreach($prices->groups as $group){
$groupItem = array();
$groupItem["category_code"] = $group->category_code;
$groupItem["category_name"] = $group->category_name;
$groupItem["category_desc"] = $group->category_desc;
$groupItem["skus"] = array();
foreach($group->skus as $sku){
$skuItem = array();
$skuItem["item_code"] = $sku->sku_info->item->item_code;
$skuItem["identifier"] = $sku->sku_info->identifier;
foreach($sku->prices as $price => $amount){
$skuItem[] = $amount;
}
$skuItem[] = strip_tags(html_entity_decode($sku->sku_info->item->desc));
foreach ($sku->sku_info->details as $details) {
$skuItem[] = $details->details1;
$skuItem[] = $details->details2;
$skuItem[] = $details->details3;
}
$groupItem["skus"][] = $skuItem;
}
$allgroupResult[] = $groupItem;
}

PHP create array from another array

I have an array which I have received from an API.
object(stdClass)#19498 (4) {
["success"]=>
bool(true)
["quoteId"]=>
int(0011)
["abcValue"]=>
float(00)
["priceResponse"]=>
array(4) {
[0]=>
object(stdClass)#19502 (9) {
["priceId"]=>
int(1263)
["fPrice"]=>
float(37.14)
["grossPrice"]=>
float(44.7)
["priceType"]=>
string(2) "ABC"
}
[1]=>
object(stdClass)#19501 (10) {
["priceId"]=>
int(1263)
["fPrice"]=>
float(37.14)
["grossPrice"]=>
float(44.7)
["priceType"]=>
string(2) "ABC"
}
[2]=>
object(stdClass)#19500 (8) {
["priceId"]=>
int(1266)
["fPrice"]=>
float(550.14)
["grossPrice"]=>
float(544.7)
["priceType"]=>
string(2) "DEF"
}
}
}
I want to loop through the array to add another object in PriceResponse i.e customPrice and also delete some objects from the array like fPrice, priceType etc. The best way I figured to do this was to create another array. However I can't seem to get it working:
PHP:
$output_array = json_decode($output);
$modified_array = array();
$priceResultArray = array();
foreach($output_array as $j => $item) {
foreach($output_array->priceResponse as $i => $field) {
$percent = $field->grossPrice * 10 / 100;
$customPrice = $field->grossPrice + $percent;
$priceResultArray['priceId'] = $field->priceId;
$priceResultArray['customPrice'] = $customPrice;
}
$modified_array['success'] = $output_array->success;
$modified_array['quoteId'] = $output_array->quoteId;
$modified_array['priceResponse'] = $priceResultArray;
}
var_dump($modified_array);
This is the output of the modified array - it only shows the last result of the priceResultArray:
array(3) {
["success"]=>
bool(true)
["quoteId"]=>
int(0011)
["priceResult"]=>
array(5) {
["priceId"]=>
int(1266)
["customPrice"]=>
float(599.17)
}
}
Any pointers would be appreciated.
You don't need the outer loop. $output_array is a single object, not an array. You're looping over the properties, but never doing anything with $j or $item.
And instead of creating a new array, you can simply modify the objects in the original priceResponse array.
$output_array = json_decode($output);
foreach ($output_array->priceResponse as $field) {
$percent = $field->grossPrice * 10 / 100;
$customPrice = $field->grossPrice + $percent;
$field->customPrice = $customPrice;
unset($field->fPrice);
unset($field->priceType);
unset($field->grossPrice);
}
You have such output because you put values inside the same keys within your loop.
You should create new object on each loop interation.
Checkout this code:
$output_array = json_decode($output);
$modified_array = array();
$priceResultArray = array();
foreach($output_array as $j => $item) {
foreach($output_array->priceResponse as $i => $field) {
$percent = $field->grossPrice * 10 / 100;
$customPrice = $field->grossPrice + $percent;
$singlePriceResult = array();
$singlePriceResult['priceId'] = $field->priceId;
$singlePriceResult['customPrice'] = $customPrice;
$priceResultArray[] = $singlePriceResult;
}
$modified_array['success'] = $output_array->success;
$modified_array['quoteId'] = $output_array->quoteId;
$modified_array['priceResponse'] = $priceResultArray;
}
var_dump($modified_array);

writing to excel in php

foreach ($ids as $key => $value) {
$settlement_data = $this->settlement_model->getSettlementData($value);
foreach ($settlement_data as $key_date => $price) {
$temp[] = array($key_date, (float)$price['settlement_price']);
}
$this->load->library('xlsxwriter');
$file = new XLSXWriter();
$first_row = array();
if ($temp) {
$first_row = array_fill(0, count($temp[0]), "");
}
//add one empty row at the beginning of array
array_unshift($temp, $first_row);
//add title row at the beginning of array (above the empty one)
$title = 'test title';
$file->writeSheet(array_values($temp), 'Mytest', $headers, $title);
$file->downloadAsFile('test' . ".xlsx");
die();
}
The sample data for $settlement data is as follows:
array(7571) {
["2017-05-20"]=>
array(4) {
["settlement_price"]=>
string(15) "10.001"
}
["2017-05-21"]=>
array(4) {
["settlement_price"]=>
string(14) "15.726"
}
["2017-05-22"]=>
array(4) {
["settlement_price"]=>
string(15) "15.729352220997"
}
write now only the dates and price of one id is written to the excel file.How can i write the prices of other ids as well corresponding to the date?
example:
Date Price of id1 price of id2
2017-05-20 10.001 20.001
2017-05-21 15.726 70.001

PHP foreach loops and data retrieval

Using PHP and MySQL I have generated two arrays. I would like to loop through these arrays, retrieve data from both and display together in one sentence.
foreach ($items as $item) {
if(isset($item->item_title)) {
$itemTitle = $item->item_title;
}
// var_dump($itemTitle);
// string(7) "Halfway" string(5) "Story" string(6) "Listen"
}
foreach ($aData["Items"]["Item"] as $a) {
if (isset($a['description'])) {
$aDescription = $a['description'];
}
// var_dump($aDescription );
// string(4) "Good" string(6) "Strong" string(2) "OK"
}
?>
Desired result;
The title is Halfway and the description is Good.
The title is Story and the description is Strong.
The title is Listen and the description is OK.
// etc
// etc
Is it possible to nest the foreach loops, or is there a better more efficient way?
Please try this way. Hope this help!!
foreach ($items as $index => $item) {
if(isset($item->item_title)) {
$itemTitle = $item->item_title;
echo 'The title is '.$itemTitle;
}
if(isset($aData["Items"]["Item"][$index]['description']) {
$itemDescription = $aData["Items"]["Item"][$index]['description'];
echo ' and the description is '.$itemDescription;
}
echo '<br>';
// The title is Halfway and the description is Good.
}
You can merge those two foreach loops using a simple for loop, like this:
$count = count($items) >= count($aData["Items"]["Item"]) ? count($aData["Items"]["Item"]) : count($items);
for($i = 0; $i < $count; ++$i){
if(isset($item[$i]->item_title)) {
$itemTitle = $item[$i]->item_title;
}
if (isset($aData["Items"]["Item"][$i]['description'])) {
$aDescription = $aData["Items"]["Item"][$i]['description'];
}
// your code
}
Sidenote: The above code assumes that two arrays $items and $aData["Items"]["Item"] have unequal number of elements, though this will work for equal number of elements as well. If you're sure that these two arrays will always have equal number of elements, then refactor the $count = ... ; statement in the following way,
$count = count($items);
or
$count = count($aData["Items"]["Item"]);
and use this $count variable in for loop.
Try this hope this will help you out.
Note: Here i am assuming both array's have same indexes.
$items
$aData["Items"]["Item"].
If not you can do array_values($items) and array_values($aData["Items"]["Item"])
foreach ($items as $key => $item)
{
if (isset($item->item_title) && isset($aData["Items"]["Item"][$key]['description']))
{
$itemTitle = $item->item_title;
echo sprinf("The title is %s and the description is %s",$itemTitle,$aData["Items"]["Item"][$key]['description']);
echo PHP_EOL;
}
}

Trouble with accessing information in a multi-dimensional array in PHP

I'm trying to parse the most recent 3 news articles in an RSS feed. After that, it should create a "preview" of the description, and then display the title and the preview. I got it to display the first article 3 times...
<?php
$doc = new DOMDocument();
$doc->load('http://info.arkmediainc.com/CMS/UI/Modules/BizBlogger/rss.aspx?tabid=575593&moduleid=1167800&maxcount=25&t=16c4c7582db87da06664437e98a74210');
$arrFeeds = array();
foreach ($doc->getElementsByTagName('item') as $node) {
$itemRSS = array (
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
'description' => $node->getElementsByTagName('description')->item(0)->nodeValue,
'pubDate' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue
);
array_push($arrFeeds, $itemRSS);
}
$itemRSS = array_slice($itemRSS, 0, 3); // This cuts it down to 3 articles.
for ($i = 0; $i < 3; $i++)
{
$title = $itemRSS['title'];
$description = substr($itemRSS['description'],0,100);
echo("<h2>".$title."</h2>");
echo("<br />".$description."<br />");
}
?>
I also got it to "work" (show the first 3) by using a foreach loop...
/*
foreach($itemRSS as $ira)
{
$title = $itemRSS['title'];
$description = substr($itemRSS['description'],0,100);
echo("<h2>".$title."</h2>");
echo("<br />".$description."<br />");
}
*/
It's commented out because it makes less sense to me.
Please help! Thanks!
You have pushed the rss items into the $arrFeeds and now you can access these by the index, e.g. $arrFeeds[0] would be the first rss item.
for ($i = 0; $i < 3; $i++)
{
$title = $arrFeeds[$i]['title'];
$description = substr($arrFeeds[$i]['description'],0,100);
echo("<h2>".$title."</h2>");
echo("<br />".$description."<br />");
}
But the following is better: with foreach:
$theFirstThreeArticles = array_slice($arrFeeds, 0, 3); // This cuts it down to 3 articles.
foreach($theFirstThreeArticles as $ira)
{
$title = $ira['title'];
$description = substr($ira['description'],0,100);
echo("<h2>".$title."</h2>");
echo("<br />".$description."<br />");
}
When you use foreach, there are there are two (in your case, but there can be 3) possible "parameters" that it accepts. An array to traverse and a variable to store each element. This example:
$numbers = array('one', 'two');
foreach($numbers as $number) {
echo "\nNew Iteration\n";
var_dump($numbers);
var_dump($number);
}
Will output:
New Iteration
array(2) {
[0] => string(3) "one"
[1] => string(3) "two"
}
string(3) "one"
New Iteration
array(2) {
[0] => string(3) "one"
[1] => string(3) "two"
}
string(3) "two"
The iterator never modifies the array. It just sets $number to be the value of each element in the array until there are no more elements. The same principle applies to a for loop. The code below will produce the same as above.
$numbers = array('one', 'two');
for($i = 0; $i < count($numbers); $i++) {
echo "\nNew Iteration\n";
var_dump($numbers);
var_dump($numbers[$i]);
}
The same applies when you use multidimensional arrays. Whether you choose for or foreach, you have to access each element in the array. So your code will look something like this:
foreach($rssItems as $rssItem)
{
$title = $rssItem['title'];
$description = substr($rssItem['description'],0,100);
echo("<h2>".$title."</h2>");
echo("<br />".$description."<br />");
}
I chose to use foreach because I find it's much easier to let PHP handle the iteration. Plus, the foreach line reads properly in English. For each item in $rssItems alias it as $rssItem. Notice the difference between the plural and the singular. The foreach runs its block for each item in $rssItems and for each iteration the variable $rssItem will contain the current value in $rssItems that the loop is on.
Want more of a reason to use foreach? If you need to work with the key too, foreach will give you both the key and value of every element of the hash (array).
$words = array('hola' => 'hello', 'adios' => 'goodbye', 'gracias' => 'thank you');
foreach($words as $spanishWord => $englishTranslation) {
echo $spanishWord . " in English is " . $englishTranslation . "\n";
}
Foreach also allows you with SPL to write classes that extend or implement an iterator. Classes that contain collections, can function as a class but can be iterated over. For example:
class PhotoAlbum implements IteratorAggregate {
private $pictures = array();
public function __construct($pictures) {
$this->pictures = $pictures;
}
public function addPicture($picture) {
$this->pictures[] = $picture;
}
public function share($friend) {
// etc..
}
public function getIterator() {
return new ArrayIterator($this->pictures);
}
}
$album = new PhotoAlbum($picturesArrayFromDB);
foreach($album as $picture) {
echo $picture;
}

Categories