Get a single value from count() - php

I'm trying to get the number of ids in an array but my code outputs the count like an array instead of giving me a single value.
<?php
foreach ($studentlistbysection as $stlist) {
$numstudent[] = $stlist->id;
echo count($numstudent);
}
This outputs the number as 12345678910 instead of a single value like 10
How do I count the number of ids in my array?

You should count it after this loop
foreach ($studentlistbysection as $stlist) {
$numstudent[] = $stlist->id;
}
echo count($numstudent);

Related

Search multi-dimesional array and return specific value

Hard to phrase my question, but here goes. I've got a string like so: "13,4,3|65,1,1|27,3,2". The first value of each sub group (ex. 13,4,3) is an id from a row in a database table, and the other numbers are values I use to do other things.
Thanks to "Always Sunny" on here, I'm able to convert it to a multi-dimensional array using this code:
$data = '13,4,3|65,1,1|27,3,2';
$return_2d_array = array_map (
function ($_) {return explode (',', $_);},
explode ('|', $data)
);
I'm able to return any value using
echo $return_2d_array[1][0];
But what I need to be able to do now is search all the first values of the array and find a specific one and return one of the other value in i'ts group. For example, I need to find "27" as a first value, then output it's 2nd value in a variable (3).
You can loop through the dataset building an array that you can use to search:
$data = '13,4,3|65,1,1|27,3,2';
$data_explode = explode("|",$data); // make array with comma values
foreach($data_explode as $data_set){
$data_set_explode = explode(",",$data_set); // make an array for the comma values
$new_key = $data_set_explode[0]; // assign the key
unset($data_set_explode[0]); // now unset the key so it's not a value..
$remaining_vals = array_values($data_set_explode); // use array_values to reset the keys
$my_data[$new_key] = $remaining_vals; // the array!
}
if(isset($my_data[13])){ // if the array key exists
echo $my_data[13][0];
// echo $my_data[13][1];
// woohoo!
}
Here it is in action: http://sandbox.onlinephpfunctions.com/code/404ba5adfd63c39daae094f0b92e32ea0efbe85d
Run one more foreach loop like this:
$value_to_search = 27;
foreach($return_2d_array as $array){
if($array[0] == $value_to_search){
echo $array[1]; // will give 3
break;
}
}
Here's the live demo.

array isn't combining with loop in php

i've a array problem couldn't just solve it:
here's the code that is already in a foreach loop. i'm getting the $row->class_id value from this loop. it works fine and get me the total students row within an array and i preserved it in $total_student_of_this_class variable. i used another foreach loop for storing the result of first loop and then second loop and so on. but this loop gives only first loop result.
i need to combine the all array of total iteration of the loop.
$total_student_of_this_class =
$this->db->select('student_id')->where('class_id',
$row->class_id)->get('student')->result_array();
echo count($total_student_of_this_class);
// prints 2 in the first row of the output table and 5 in the second
row for me.
$total_student = array();
foreach ($total_student_of_this_class as $tt)
{
$total_student[] = $tt;
}
echo '<pre>';
print_r($total_student_of_this_class);
echo '</pre>'
echo count($total_student);
// prints only 2 outside the loop (not 7)
pls someone help me.
The problem here is seems to be with $total_student = array();: initializing array inside an other loop it always starts it from zero values array, so you get only results from one iteration inside it and doesn't collect all datas.
You can also look at array_merge php function.

how can i get the number of items in implode?

I got a variable of ingredient name and ingredient value
I wanted to know if they have equal number of items. I used implode.
for ex.ingredient_name['chicken'^'meat'^'water'] = 3 .
$ingredient_name =implode("^",$_POST["mytext"]);
$ingredient_value = implode("^", $_POST["mytext2"]);
implode takes array as a parameter so your code should work fine with
implode("^",$_POST["mytext"]);
implode("^",$_POST["mytext2"]);
so
if(count($_POST["mytext"])===($_POST["mytext2"])) echo "equal";
should do the job

Indexing certain elements within a PHP array

I have a PHP array created by the mysqli_fetch_array() function. The array holds several rows, and without looping through them, can I grab each row one by one?
I have tried:
$links= mysqli_fetch_array($links_result);
echo $links['link'][0];
But I can't seem to get it to work. What could I be doing wrong? Is this possible?
when you do
$links= mysqli_fetch_array($links_result);
you get the next row in result set in $links just access them by the column names.
you can loop over the results like
if(mysqli_num_rows($links_result)) {
while($links= mysqli_fetch_array($links_result)) {
//each row in here
var_dump($links);
}
}
If the field name is 'link'
Without looping:
echo $links[0]['link'];
where '0' is your row number
so, for row #505 it will be:
echo $links[505]['link'];
Just make sure it exists ;)
Without loop you can grab each row of array using recursive function
like this:
function printarray_withoutloop($userarray,$elementindex)
{
if(count($userarray) > $elementindex + 1)
{
echo $userarray[$elementindex]['link'];
printarray_withoutloop($userarray,$elementindex + 1)
}
elseif(count($userarray) == $elementindex + 1)
{
echo $userarray[$elementindex]['link'];
}
}
$links= mysqli_fetch_array($links_result);
$elementindex=0;
printarray_withoutloop($links,$elementindex);

How to get the values inside an array?

$playerId= array();
$playerId[] = intval($row['Id']);
$allrounders[] = array(
'Weight'=>$fullTotal,
'Id'=>$playerId
);
rsort($allrounders);
$sliceAr = array_slice($allrounders,0,5);
foreach($sliceAr as $allroundK){
echo $allrounders[]['Id']."<br/>";
}
Question:
In the above array how to get the values of the Id Key accordingly? It takes all the player scores and organizes that with his ID and sorts it descending order. It takes the 1st 5 results. I need the Ids of those.
Inside the foreach loop, $allroundK is the item of your array. In this case, it's the array with weight and id. So:
foreach($sliceAr as $allroundK) {
echo $allroundK['Id']."<br />";
}
Do
echo $allrounders[0]['Id'][0];
Since you have set the array in this manner
$allrounders[] = array(
'Weight'=>$fullTotal,
'Id'=>$playerId
);
Here $allrounders[] also means that an array so the elements Weight and Id will be added in the [0th] element of the array $allrounders
If you want to get rid of [0] just set the array like this
$allrounders = array(
'Weight'=>$fullTotal,
'Id'=>$playerId
);
And now you can access the Id like
echo $allrounders['Id'][0];
EDIT:
In your case it will work as
foreach($sliceAr as $allroundK){
echo $allroundK['Id'][0]."<br/>";
}
or
foreach($sliceAr as $allroundK){
foreach($allroundK['Id'][0] as $allroundJ){
echo $allroundJ."<br/>";
}
}

Categories