PHP - New numbered var for every object in array [duplicate] - php

This question already has answers here:
PHP variable variables
(6 answers)
Closed 2 years ago.
I have the following scenario:
Array with multiple waypoints
Want a new variable for every waypoint
Number of waypoints is not fixed
The array stores the address data for the waypoint like street, number, zip, town etc.
I can loop and print the output of the array in foreach loop like:
foreach ($waypoints as $waypoint) {
echo $waypoint->street
echo $waypoint->nb
echo $waypoint->zip
echo $waypoint->town
}
What I'm trying to do is, to get a new variable for each waypoint. E.g:
$wp1 = Data from waypoint 1
$wp2 = Data from waypoint 2
What I have tried:
$waypointCount = count($waypoints);
for ($i = 1; $i < $waypointCount; $i++) {
$wp[$i] = $waypoints->street.' '.$waypoints->nb.' '.$waypoints->zip.' '.$waypoints->town.' '.$waypoints->state;
}
My idea was to count the number of waypoints, set a new variable for each waypoint number and store the corresponding waypointdata in the new variable. I'm kinda stuck on how to create the $wp[i] variables and assign the data to it. Does it need to be a combination with a for and a foreach loop?
Looking for some help to get me on the right direction. Thanks!

It looks like you need to access the correct index of your $waypoints array in your loop. Also, make sure you start your loop with $i = 0 or else you'll skip the first element.
$waypointCount = count($waypoints);
for ($i = 0; $i < $waypointCount; $i++) {
$wp[$i] = $waypoints[$i]->street.' '.$waypoints[$i]->nb.' '.$waypoints[$i]->zip.' '.$waypoints[$i]->town.' '.$waypoints[$i]->state;
}

Related

Php count conditions inside loop [duplicate]

This question already has answers here:
PHP - count specific array values
(10 answers)
Closed 9 days ago.
Close my question when it´sdifferent to this not duplicater, the case it´s different in all :
PHP - count specific array values
My question not the same
I have this array for example :
$array_test=array("gren","green","red","red","green","blue");
My idea is know inside loop number of elementos with the condition i want, the array for show it´s more complex and this is only example for understand i need, because try different ways with "count" and don´t show right this number in each case.
I try this :
foreach($array_test as $array_ts) {
if($array_ts=="green") { Count number of elements green /// }
if($array_ts=="red") { Count number of elements red /// }
if($array_ts=="blue") { Count number of elements blue /// }
}
Thank´s for the help, regards.
You can create an array and fill it with the count for each color:
$input = ["gren","green","red","red","green","blue"];
$count = [];
foreach ($input as $color) {
if (array_key_exists($color, $count)) {
$count[$color]++;
} else {
$count[$color] = 1;
}
}
$count will contain:
["green"=>3, "red"=>2, "blue"=>1]

PHP exploding txt file and table-ing them [duplicate]

This question already has answers here:
How to parse a CSV file using PHP [duplicate]
(6 answers)
Closed 2 years ago.
I am currently working on php page creating, and there is one requirement that I need to work on but I am stuck.
There is accounts.txt which has n rows of : date,time,id,status and 4 columns (date,time,id,status).
And basically, I need to split them by "\n" and then "," in order to make each a value for table with row and column equal to the # of adminlog.txt file.
'''
$data = file_get_contents($file_path.'/adminlog.txt');
$data = explode(",",$data);
$adminlog = array();
for ($i = 0; $i < sizeof($data); $i ++) {
array_push($adminlog,$data[$i]);
}
print($adminlog);
'''
Above was the code that I was trying to put in php in order to separate them by comma, but when I tried printing out $adminlog on my page, it would just show "Array". Is there actually way to separate the dataset neatly by "comma" and actually use the separated data and use it to create a table containing all information via loop?
Thanks so much in advance for y'all's enthusiasm and passion!
You can use print_r or var_dump or loop throught the array and print the values
$arr = array('Foo', 'Bar');
foreach($arr as $value) {
echo "$value\n";
}

How to count the elements in array which are more than one (php) [duplicate]

This question already has answers here:
How to detect duplicate values in PHP array?
(13 answers)
Closed 7 months ago.
In php,I have one ArrayList. Lets say
$list = {1000,7000,5000,1000,6000,5000,1000,2000};
So what I want to do is that Make count of each element in list:
For example as above ,
1000 comes three times then count of 1000 = 3
5000 comes two times then count of 5000 = 2,etc.
And I want to access that count of different elements separately.
Edit:
Here I have for loop
for($i=0;$i<count($trip);$i++)
{
// Here list will be new everytime for loop run.
$list = $Users->Matches($trip[$i]);
}
// ----------------------------------------------------------------------
Here what I want to do is that "Take all the element of list for value of
$i : 0 to count($trip)" and "add it into another list lets say $allList
and access" as below :
// -----------------------------------------------------------------------
$cnt_arr = array_count_values($allList);
foreach($cnt_arr as $key => $value) {
echo "\n\n Count of ".$key." = ".$value." ";
}
OUTPUT :
Lets say for loop runs first time :
$list = {1000,7000,5000}
Now for loop second time :
$list = {8000,1000,9000}
Now for loop is completed and at the end I want the $allList value as below :
$allList = {1000,7000,5000,8000,1000,9000}(which has all the elements of above-appended list)
So How can I do this ?
Please Guide me. Any help will be appreciated.
Try with array_count_values like
$cnt_arr = array_count_values($list);
foreach($cnt_arr as $key => $value) {
echo "Count of ".$key." = ".$value."<br>";
}
See this LINK
As per your edit,You need to store them like array like
for($i=0;$i<count($trip);$i++)
{
// Here list will be new everytime for loop run.
$temp_list = $Users->Matches($trip[$i]);
foreach($temp_list as $tmp) {
$list[] = $tmp;
}
}

Save key's and post meta data into array's - Wordpress

Im using wordpress as my base and I need to output the post meta for all my posts, but only for a certain number of keys.
What I intend to do is to save a list of all my keys, that im going to use to query wordpress for metadata for that post.
Below is my code.
//HERE YOU CAN SEE THE KEYS IMM USING ATM
$nyckellista[] = array("ebutik_allm_bas_operativsystem" ,"--foretagsform"
,"ebutik_allm_bas_omsättning");
$i = 0;
//Here im trying to query the get_post_meta with my keys and save the result (it's an array aof values that it return)
foreach($nyckellista as $nyckel)
{
$nyckellista[$i] = get_post_meta($post->ID,$nyckel,false);
echo $i . "Nyckel:" . $nyckel[$i];
$i++;
}
//HERE ME TRYING TO PRINT THE ARRAY CONTENTS
$count = count ($nyckellista);
echo $count;
for($y=1; $y <= $count; $y++)
{
$countmore=count($nyckellista[$y]);
for($x=1; $x <= $countmore; $x++)
{
print ($nyckellista[$y][$x] . "<br> ");
}
echo "<br>";
}
WHAT AM I DOING WRONG?
In the first line the $nyckellista variable is being declared implicitly as an array and then you're assigning to its first position an array of values.
Is this what you need/intend?
Edit:
Another point is, in the counts area of the code, that php arrays get numeric indexes starting at 0, not at 1 (see Example #4 in PHP array reference)

Create PHP array's on the fly

I am having the worst time trying to get this to work. In the following code, I am gathering data from a database query and trying to build a muti-dimensional array object that will keep totals and tally up some information in a specific way. The problem is that instead of getting a value that is incrementing as it should, the value seems to be suffering from the last value it was assigned problem. Here is the code:
$REVIEWS = array();
$USER_REVIEWS = array();
$USER_IMGREVS = array();
pseudo-code: loop here which iterates over the DB results creating
$date - which is into this function as its called for each day of month
$p1user - which is one of the users (there are 3) 'levels' of users
$hr - is the hour which is built from the transaction's timestamp
$hr = date('H', $row['P1TIMESTAMP']);
$p1user = $row['P1USER'];
$REVIEWS[$date] += 1;
$USER_REVIEWS[$date][$p1user][$hr] += 1;
$USER_IMGREVS[$date][$p1user][$hr] += $row['F5'];
print "PASS1<br/>\n";
print "Value of Total Reviews: [".$REVIEWS[$date]."]<br/>\n";
print "Value of User Reviews: [".$USER_REVIEWS[$date][$p1user][$hr]."]<br/>\n";
print "Value of Reviewed Images: [".$USER_IMGREVS[$date][$p1user][$hr]."]<br/>\n";
print "<br/><br/>\n";
So - the 'total reviews' increments by one, as it should, for each time i print this. SO far so good. The next two arrays will only print the last values they were assigned, and will not be added together like they should. Why not? I have attempted to do this another way by literally creating the arrays one by one and assigning them in whole to the array containing them - but that also does not seem to work. Any insights?
i don't know how you initilize your array, maybe this will help:
// replace this 2 lines:
$USER_REVIEWS[$date][$p1user][$hr] += 1;
$USER_IMGREVS[$date][$p1user][$hr] += $row['F5'];
// with this code:
if (!isset($USER_REVIEWS[$date]))
$USER_REVIEWS[$date] = array();
if (!isset($USER_REVIEWS[$date][$p1user]))
$USER_REVIEWS[$date][$p1user] = array();
if (!isset($USER_REVIEWS[$date][$p1user][$hr]))
$USER_REVIEWS[$date][$p1user][$hr] = 0;
$USER_REVIEWS[$date][$p1user][$hr] += 1;
if (!isset($USER_IMGREVS[$date]))
$USER_IMGREVS[$date] = array();
if (!isset($USER_IMGREVS[$date][$p1user]))
$USER_IMGREVS[$date][$p1user] = array();
if (!isset($USER_IMGREVS[$date][$p1user][$hr]))
$USER_IMGREVS[$date][$p1user][$hr] = 0;
$USER_IMGREVS[$date][$p1user][$hr] += $row['F5'];
Sir, I dont understand very well why your coed is not working, but in my first test, I would change these lines:
$count = 1;
$USER_REVIEWS[$count][$p1user][$hr] += 1;
$USER_IMGREVS[$count][$p1user][$hr] += $row['F5'];
$count++;
Please, check if this code helps you anyway.
Your print statements for those values rely on the value of $p1user:
print "Value of User Reviews: [".$USER_REVIEWS[$date][$p1user][$hr]."]<br/>\n";
print "Value of Reviewed Images: [".$USER_IMGREVS[$date][$p1user][$hr]."]<br/>\n";
If you want to print it for all users you should loop over all possible users rather than just using $p1user. Either that or add them up if you want their sum.
Edit: Something that was bugging me was your data structure. It doesn't seem to represent your data very well. In your loop why don't you build up useful information that you store at the base of the review array?

Categories