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";
}
Related
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;
}
This question already has answers here:
Two arrays in foreach loop
(24 answers)
Closed 3 years ago.
I am getting data into two arrays and i want to push that data into single array one by one. Let say i want to get first element from first array and first element from second array and then push that data into single array.
first array
$url = $a->attr['href'];
$lnk[] = ['url'=>$url];
second array
$img = $img->attr['src'];
$img[] = ['img'=>$img];
I want this
$data[] = ['url'=>$url, 'img'=>$img];
I assume the size is same so you can iterate over the length on any of the arrays.
$finalArray=[];
for($i=0;$i<count($lnk);$i++){
$finalArray[] = ['url'=>$lnk[$i]['url'], 'img'=>$img[$i]['img']];
}
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;
}
}
says the orders.txt contains
hello
hello
hello
Code:
<?php
$orders = file("orders.txt");
$row_count = count($orders);
for ($row = 0; $row < $row_count; $row++) {
$col_count = count($orders[$row]);
for ($col = 0; $col < $col_count; $col++) {
echo $orders[$row][$col];
}
}
?>
I found that the cause of error here is that count($orders[$row]) didn't return the total number of element in this array.
the text files in .txt are multidimensional array right?
If i create a multidimensional array I can get each arrays element count in the multidimensional array by using count()
but here im reading from the .txt file and I think it is multidimensional array
My question:
I get the rows count, but not the column count.
I know i can do strlen($orders[$row]) instead, but why count($orders[$row]) is not possible.
What are you trying to achieve. Your example orders.txt is too simple, and you don't specify the result that you expect. file() returns an array of strings where each string is a line from the file (http://php.net/manual/en/function.file.php).
Perhaps if you are trying to read multiple values from each line (i.e. read each line as an array) you should use fgetcsv() instead (http://php.net/manual/en/function.file.php).
When I check print_r($orders), the data from the txt is not stored in a multidimensional array but just a plain array with key/val. So you're assumption is not correct. I'm having a hard time understanding exactly what the problem is (language barriers ...). Your col count is correct as well, since there's only 1 value inside (I get 1 here).
It probably returns 4 (it should be 3) because you have an extra line-feed character in the txt file, remove that last empty line and it will make sense.
This question already has answers here:
Generate json string from multidimensional array data [duplicate]
(5 answers)
Closed 9 months ago.
I need help in how to remove the last comma from this looped var, please.
$image_meta .= "{\"id\":\"".$img_id."\",\"client\":\"".$img_desc."\",\"desc\":\"Test\"},";
FireFox doesn't seem to mind it, but IE does.
If there is any way to even get rid of the .= and loop my data in another way, i would be most thankful.
We would need to see the rest of the script.
But from what I understand of your situation, when you echo $image_meta (after the loop I suppose) you could do one of the two:
echo rtrim($image_meta,',');
or
echo substr($image_meta,0,-1);
You can do like this:
$ar_image_meta = array();
for/foreach() // your loop
{
$ar_image_meta[] = '{"id":"'.$img_id.'","client":"'.$img_desc.'","desc":"Test"}';
}
$image_meta = implode(", ", $ar_image_meta);
If your goal is to "convert" a PHP variable to a Javascript one, have a look at json_encode().
look at implode:
http://php.net/manual/en/function.implode.php
Just to clarify, I'm guessing your doing something similar to this:
$image_meta = '';
foreach($blahs as $blah){
$image_meta .= "{\"id\":\"".$img_id."\",\"client\":\"".$img_desc."\",\"desc\":\"Test\"},";
}
Something like this should work:
$image_meta_arr = array();
foreach($blahs as $blah){
array_push($image_meta, "{\"id\":\"".$img_id."\",\"client\":\"".$img_desc."\",\"desc\":\"Test\"}";
}
$image_meta = implode(',', $image_meta_arr);