Hi i have a function which is:
public function getpopularAction()
{
$businessReviewMapper = new Application_Model_Mapper_BusinessReviewsMapper();
$result = $businessReviewMapper->getTotalPopular();
for ($i = 0; $i < count($result); $i++)
{
$rotd[$i] = $businessReviewMapper->getROTD($result[$i]['review_id']);
for ($j = 0; $j < count($rotd); $j++)
{
$rotd[$j]['u_img'] = $this->view->getLoginUserImage(
$rotd[$j]['social_id'], $rotd[$j]['login_type'], null, null, large
);
}
}
print_r($rotd);
exit;
}
The result i get is:
Array
(
[0] => Array
(
[0] => Array
(
[review_id] => 161
[review_desc] => tgi goooood....................
[user_id] => 2
[rating] => 3
[review_date] => 20121022203529
[name] => zlippr
[social_id] => 12345678
[login_type] => facebook
[user_unique_name] => zlippr
[city] => Kuala Lumpur
[business_name] => TGI Friday Kuala Lumpur
)
[u_img] => /public/images/image_not_found.png
)
)
I do not know where problem is but the u_img is not fetched properly, not sure whether the array loop is execute properly.
Not sure what the inner loop is for. If you are trying to retrieve the loginuserimage corresponding to each rotd, you need to do like :
for ($i = 0; $i < count($result); $i++)
{
$rotd[$i] = $businessReviewMapper->getROTD($result[$i]['review_id']);
$rotd[$i]['u_img'] = $this->view->getLoginUserImage(
$rotd[$i]['social_id'],$rotd[$i]['login_type'],null,null,large);
}
By looking at your ouput, your result are inside 2nd layer of array which means you should replace your variable inside getLoginUserImage from
$rotd[$i]['social_id'],$rotd[$i]['login_type']
to
$rotd[$i][0]['social_id'],$rotd[$i][0]['login_type']
Related
I would like to make a function that decreases values of an array by specific order.
the values should be decreased by order noodle -> bread -> rice.
I tried to make the function below.
But it does not work well.
the loop break before achiving array_sum($stapleArray) ===$sizeDayArray.
$stapleArray
should be deducted until the total size equals $sizeDayArray
E.g.
input
targetWeekDayArrayArray
(
[0] => tue
[1] => wed
[2] => fri
[3] => sat
)
*size = 4
$sizeDayArray = size_of($targetWeekDayArrayArray)
$stapleArray=
Array
(
[rice] => 5
[bread] => 0
[noodle] => 1
)
expected result
$stapleArray=
Array
(
[rice] => 4
[bread] => 0
[noodle] => 0
)
caller
$differenceSettingAndDay = array_sum($stapleArray)- sizeof($targetWeekDayArray);
$size = sizeof($targetWeekDayArray);
$stapleArray= $this->substractStapleFood($differenceSettingAndDay,$stapleArray, $size);
private function substractStapleFood($substractionnumber,$stapleArray,$sizeDayArray)
{
$array_key = ['noodle','bread','rice'];
for($i = 0; array_sum($stapleArray) ===$sizeDayArray ; $i++ )
{
$i_mod = $i % count($stapleArray); //$i % 3
$key = $array_key[$i_mod];
if (isset($stapleArray[$key]) && $stapleArray[$key] > 0) {
$stapleArray[$key]--;
}
}
return $stapleArray;
}
Actual result
Array
(
[rice] => 5
[bread] => 0
[noodle] => 1
)
It seems that id did not go through the loop.
Wrong in line :
for($i = 0; array_sum($stapleArray) ===$sizeDayArray ; $i++ )
condition array_sum($stapleArray) ===$sizeDayArray start is false => loop is not running
Change same as :
for($i = 0; !(array_sum($stapleArray) ===$sizeDayArray) ; $i++ )
It works fine creating level-1, but when level-2 is being created, it only updates the first letter of index [1].
Code:
// Position [Level-1]
$taxonomy_id = [
"id_no_1",
"id_no_2",
"id_no_3",
];
// Position [Level-2]
$titles = [
"title_1",
"title_2",
"title_3",
];
$array = [];
for ($i = 0; $i < count($taxonomy_id); $i++) {
//Construct level-1
$array[] = $taxonomy_id["{$i}"];
//Construct level-2
$array["{$i}"]["{$i}"] = $titles["{$i}"];
}
print_r($array);
Result:
(
[0] => td_no_1
[1] => it_no_2
[2] => id_no_3
)
Wanted result:
Array
(
[id_no_1] => Array
(
[0] => title_1
)
[id_no_2] => Array
(
[0] => title_2
)
)
You would be better off creating the sub arrays in one go, you can also simplify "{$i}" with $i...
for ($i = 0; $i < count($taxonomy_id); $i++) {
$array[$taxonomy_id[$i]] = [$titles[$i]];
}
I'm trying to fill an array with a for loop. This is done to get the amount of pages a certain book has. but when executing the code, it skips the first object in the array. Can anyone tell me why? (I thought it was because $i starts at 1 instead of 0 but that doesn't seem to change anything)
if(!empty($article['finishing'])){
$numPages = $article['copies'];
$arrayIndexNumber = [];
for($i=1; $i <= $numPages; $i++){
$arrayIndexNumber[] = $i;
}
if(count($arrayIndexNumber) >= 1 ){
if(count($arrayIndexNumber) == 1){
$output['attributes']['EFPageRange'] = 1;
$print_jobs[$article['id']][] = $output;
}
if(count($arrayIndexNumber) > 1){
$comma_separated1 = implode(", ", ['1', $article['copies']]);
$output['attributes']['EFPageRange'] = $comma_separated1;
$print_jobs[$article['id']][] = $output;
}
array_shift($arrayIndexNumber);
array_pop($arrayIndexNumber);
$comma_separated2 = implode(", ", $arrayIndexNumber);
$output['attributes']['EFPageRange'] = $comma_separated2;
if(count($arrayIndexNumber) >= 2){
$print_jobs[$article['id']][] = $output;
}
}
$article['file_url'] = 'i has finishing';
$output['attributes']['username'] = $article['file_url'];
}
above code outputs:
[0] => Array
(
[attributes] => Array
(
[title] => 277569
[EFPrintSize] => a4
[num copies] => 1
[num pages] => 119
[EFPCName] => 80
[EFDuplex] => TopTop
[EFPageRange] => 1, 119
)
)
instead of:
[0] => Array
(
[attributes] => Array
(
[title] => 277564
[EFPrintSize] => a4
[num copies] => 1
[num pages] => 45
[EFPCName] => 80
[EFDuplex] => false
[EFPageRange] => 1, 45
[username] => i has finishing
[EFColorMode] => Grayscale
)
)
Your first array element is deleted because of array_shift:
array_shift($arrayIndexNumber);
array_shift
array_shift — Shift an element off
the beginning of array
Debug your code:
for($i=1; $i <= $numPages; $i++){
$arrayIndexNumber[] = $i;
}
echo '<pre>';
print_r($arrayIndexNumber); // Check what the array returns
php array indexes starts to count from zero
for($i=1; $i <= $numPages; $i++)
^^^
change it to $i=0
Below is a part of my produced array. How can I keep only every second element usign PHP?
I have tried this until now without luck, maybe it depends on the structure of the array, I didn't understand.
$size = count($array);
$result = array();
for ($i = 0; $i < $size; $i += 2) {
$result[] = $array[$i];
}
var_dump($result);
this is my array below:
Array
(
[0] => Array
(
[0] =>
11.490
[1] =>
11.490
[2] =>
13.490
[3] =>
13.490
[4] =>
17.490
[5] =>
17.490
[6] =>
20.990
[7] =>
20.990
[8] =>
14.290
[9] =>
14.290
[10] =>
14.490
[11] =>
14.490
[12] =>
19.990
[13] =>
19.990
Use array_filter (php.net)
function odd($var)
{
return($var & 1);
}
$array[0] = array_filter($array[0], "odd", ARRAY_FILTER_USE_KEY);
EDIT:
Of course you can also use an "even" method
EDIT 2:
Change to match the code from the op.
From $array to $array[0]
EDIT 3:
Add a missing semicolon
How about using unset on every other element?
<?php
$array = array('1','1','2','2','4','4','5','5');
for($i = count($array) - 1; $i > 0; $i -= 2){
unset($array[$i]);
}
Let's say I have following array:
Array
(
[0] => Array
(
[0] => a
[1] => 1
)
[1] => Array
(
[0] => b
[1] => 8
)
[2] => Array
(
[0] => c
[1] => 16
)
[3] => Array
(
[0] => d
[1] => 21
)
....
)
Numbers in inner array are generated randomly from range (0, 100) and they don't repeat.
I would like to create a loop, which will iterate from 0 to 100 and check if loop iteration is equal to inner number of above array. Excepted result is array with 100 elements:
Array
(
[0] => const
[1] => a
[2] => const
[3] => const
[4] => const
[5] => const
[6] => const
[7] => const
[8] => b
[9] => const
[10] => const
.
.
[16] => c
[17] => const
.
.
[21] => d
[22] => const
[23] => const
.
.
)
What I need is something like:
for ($i=0; $i < 100; $i++) {
if($i === $name[$i][1]) {
$new_array[] = $name[$i][0];
} else {
$new_array[] = 'const';
}
}
But I can't get it working, thus I need some help.
I am not an English native speaker, so hopefully you understand what I would like to achieve. Thanks for any help.
you need a nested loop like:
for ($i=0; $i < 100; $i++):
$found = false;
foreach($name as $array):
if($array[1] === $i):
$found = true;
$new_array[] = $array[0];
endif;
endforeach;
if(!$found):
$new_array[] = 'const';
endif;
endfor;
The reason it doesn't work is because each time $i is incremented you're trying to make a match in $name[$i], and not checking all of the arrays in $name, the simplest solution I can think of (and to perform the least number of iterations) would be to do something like:
$new_array = array();
foreach ($name as $n) {
$new_array[$n[1]] = $n[0];
}
for ($i=0; $i<100; $i++) {
if (!isset($new_array[$i])) {
$new_array[$i] = 'const';
}
}
ksort($new_array);
So first of all, loop through your $name array, and set up your $new_array with the the key => value pair (eg. [1] => 'a', [8] => 'b'), then in the for loop just check if the key ($i) has already been set, and if not, set it with the value 'const'. Finally, sort the $new_array by its keys.
The number of iterations in this example is count($name) + 100, whereas a nested loop for example would be 100 * count($name).
use
for ($i=0; $i < 100; $i++) {
if($i === $name[$i][1]) {
$new_array[$i] = $name[$i][0];
}
else{
$new_array[$i] = 'const';
}
}
for ($i = 0; $i < count($name); ++$i) {
if ($name[$i][1] === $i) {
$name[$i] = $name[$i][0];
} else {
$name[$i] = 'const';
}
}
Why do u use Identical operator instead of Equal
for ($i=0; $i < 100; $i++) {
if($i == $name[$i][1]) {
$new_array[] = $name[$i][0];
} else {
$new_array[] = 'const';
}
}