I stuck with my code and I am new to Php. How to store values of different arrays with same key in PHP ?.
for array 1: [0]=>31,[1]=>42, [2]=>21, .....
for array 2: [0]=>21,[1]=>21,[2]=>24, .....
for array 3: [0]=>45,[1]=>34,[2]=>45, ....
I tried to get a Output as :
[0]=>S5001:31,21,45|[1]=>S5002:42,21,34|[2]=>S5003:21,24,45.......[N]S50..N-1:21,23,45.
where S5001,S5002, are the keys of array1,array2,array3 ....
$student_marks_entered1=$this->input->post('mark1');
$student_marks_entered2=$this->input->post('mark2');
$student_marks_entered3=$this->input->post('mark3');
foreach($student_marks_entered1 as $key=>$value1)
{
$marks_arr1[]=$value1;
}
foreach($student_marks_entered2 as $key=>$value2)
{
$marks_arr2[]=$value2;
}
foreach($student_marks_entered3 as $key=>$value3)
{
$marks_arr3[]=$value3;
}
Desired Output is:
S5001:31,21,45|S5002:42,21,34|S5003:21,24,45.......S50..N-1:21,23,45
Using for loop get all value by there key..
See below code.
$student_marks_entered1=array(31,42,21);
$student_marks_entered2=array(21,21,24);
$student_marks_entered3=array(45,34,45);
$name=5000;
for($i=0; $i<3;$i++){
$var=$name + $i + 1;
$pipe=$i < 2 ? '|' : '';
echo "S".$var.":". $student_marks_entered1[$i].','.$student_marks_entered2[$i].",".$student_marks_entered3[$i].$pipe;
}
demo and example
Use below code
<?php
$names = array( 0=>"S5001",1=>"S5002",2=>"S5003" );
$student_marks_entered1=array(31,42,21);
$student_marks_entered2=array(21,21,24);
$student_marks_entered3=array(45,34,45);
foreach($names as $key=>$name)
{
$newArr[$key] = $name.":".$student_marks_entered1[$key].",".$student_marks_entered2[$key].",".$student_marks_entered3[$key];
}
$finalStr = implode("|", $newArr);
echo $finalStr;
Output:
S5001:31,32,33|S5002:42,44,46|S5003:21,23,25
Demo : Click Here
Related
I want to show dash(-) if my array is empty after imploding it. Here below is my try so far.
Result with Data in Array -> https://repl.it/HIUy/0
<?php
$array = array(1,2);
$result = array();
foreach ($array as $curr_arr) {
$result[] = $curr_arr;
}
echo 'Array With Data - ' . implode(',', $result);
//Result : Array With Data : 1,2
?>
Result without Data in Array -> https://repl.it/HIVE/0
<?php
$array = array();
$result = array();
foreach ($array as $curr_arr) {
$result[] = $curr_arr;
}
echo 'Array Without Data - ' . implode(',', $result);
//Result : Array With Data - :
?>
As you can see in the second result, I am unable to print anything as my array was blank hence I was unable to print anything.
However, I want to print Dash(-) using implode only by using something like array_filter which I already tried but I am unable to do so. Here I have tried this https://repl.it/HIVP/0
<?php
$array = array();
$result = array();
foreach ($array as $curr_arr) {
$result[] = $curr_arr;
}
echo 'Array With Data : ' . implode(',', array_filter($result));
//Result : Array With Data :
?>
Can someone guide me how to achieve this ?
Thanks
You can check if your array is empty and then return/ echo a Dash:
if(!empty($array)){
// Array contains values, everything ok
echo 'Array with data - ' . implode('yourGlueHere', $array);
} else {
// Array is empty
echo 'Array without data -';
}
If you want to have it in one line, you could do something like the following:
echo 'Array with' . empty($array) == false ? '' : 'out' . 'data - ' . empty($array) == false ? implode('glue', $array) : '';
Answers posted by Tobias F. and Gopi Chand is correct.
Approach 1:
I'd suggest you going this way would help you (Basically using ternary operator).
As here is no other way of doing this using just implode function.
echo empty($result) ? '-' : implode(',',$result);
Approach 2
Using a helper function like this.
function myImpllode($glue = "", $array = [])
{
if(!empty($array)){
// Array contains values, everything ok
return implode($glue, $array);
} else {
// Array is empty
return '-';
}
}
I am working on a small php script, currently i have an array like this
[0] yassine#m, [1] yassine#f, [2] Dolmi#m , [3] yassine#l
I want PHP to check if there is a duplicated element (yassine in this case) and return something like this.
[0] yassine , [1] Dolmi#m
array_unique won't work. And i really don't have any clue how to solve this. If looked for a solution on the internet but doesnt seem to find it. Anyone can help Please ?
I think this may work for you.
First sort array by value, then use combination of substr(), strpos() and array_push() to create new array according to your need
then remove duplicate value using array_unique()
<?php
$oldarray = array("suman#1","suman#2","suman#3","sujan#1","suresh#2","");
// first sort array by value so matching value comes together
asort($oldarray);
$newarray = array();
$count = count($oldarray);
for($i=0; $i < $count-1; $i++){
$a = $oldarray[$i];
$b = $oldarray[$i+1];
if($i == 0)
$c = "";
else
$c = $oldarray[$i-1];
if(substr($a,0,strpos($a,"#")) == substr($b,0,strpos($b,"#")) || substr($a,0,strpos($a,"#")) == substr($c,0,strpos($c,"#")) ){
array_push($newarray,substr($a,0,strpos($a,"#")));
}
else
array_push($newarray,$a);
}
print_r($oldarray);
// now remove duplicate value from new array
$newarray = array_unique($newarray);
print_r($newarray);
?>
Check following solution
http://ideone.com/fork/kJlLbs
<?php
function generateUniqueList ($arr){
$ret = array();
foreach ($arr as $value) {
$key = explode("#", $value)[0];
if (array_key_exists($key, $ret)) {
$ret[$key] = $key;
}
else {
$ret[$key] = $value;
}
}
return array_values($ret);
}
$arr = array("yassine#m","yassine#f","Dolmi#m", "yassine#l");
$list = generateUniqueList ($arr);
print_r($list);
I want to add two element in an array. The first one is the key and the second is the value. But I want to add it dynamically. I want to do it like the following code:
$arr="";
for( $i=0;$i<20;$i++ ) {
$arr[$i]=arr($i=>$i+1);
array_push($arr[$i]);
}
print_r($arr);
But of course it don't work. Could anyone tell me how to do it ?
Maybe you are trying to do this:
$arr = array(); // use array() instead of empty string
for( $i=0; $i<20; $i++ ) {
$arr[$i]= $i + 1;
}
print_r($arr);
$arr must be an array not string try this
$arr= array();
instead of
$arr="";
Not sure what you mean by all this, but you didn't really define the arrays correctly.
$arr = array();
for($i=0;$i<20;$i++) {
$arr[$i] = $i + 1;
array_push($arr[$i]);
}
print_r($arr);
Like answered above, you must use the array() function.
Try this way
$arr = array();
for($i=0;$i<20;$i++) {
$arr[$i] = $i+1;
}
print_r($arr);
This is tested and working
<?php
$stack = array("");
for($i=0;$i<20;$i++) {
array_push($stack, $i);
}
print_r($stack);
?>
this code will allow you to do what you request, unless I understood your requirement wrong?
Let me know if this is any help :)
If you are trying to create a numbered list, then use this instead:
<?php
$stack = array("0");
for($i=1;$i<20;$i++) {
array_push($stack, $i);
}
print_r($stack);
?>
Checkout the php manual: http://uk3.php.net/array_push
Josh.
Recently I made a program to create 4 random numbers I want to put these numbers in an array but I did not echo the array's numbers :
my code is:
<?php
$numbers = array();
function rand_num_generator() {
return rand(1000,9999);
}
for($i=0;$i<4;$i++) {
$number[i] = rand_num_generator();
}
echo $number[2];
?>
Here i am not able to access array using their index values.
You missed the $ sign in front of the i inside $number[i] which must be used before a variable
$numbers = array();
function rand_num_generator() {
return rand(1000,9999);
}
for($i=0;$i<4;$i++) {
$number[$i] = rand_num_generator();
echo $number[$i].'<br>';
}
//print_r($number);to see the whole array
You only echo once: at echo $number[i];, $i is 4, hence you only display the last random number.
You could loop on your array to echo each.
Put your echo into loop. And you have some mistakes. Use that:
$numbers = array();
function rand_num_generator() {
return rand(1000,9999);
}
for($i=0;$i<4;$i++) {
$number[$i] = rand_num_generator();
echo $number[$i].'<br>';
}
To put something in an array I recommend to use array_push
<?php
$numbers = array();
function rand_num_generator() {
return rand(1000,9999);
}
for($i=0;$i<4;$i++) {
array_push($numbers, rand_num_generator());
}
print_r($numbers); //Or use 'echo $numbers[0] . " " . $numbers[1]' etc etc
?>
I have a script that creates an array in the following format
$named_array["vehicles"][0]['vehicle'] = "i100-1 " ;
$named_array["vehicles"][1]['vehicle'] = "i100-2 " ;
$named_array["vehicles"][2]['vehicle'] = "i100-46 " ;
What I want to do later in the script is get the index value[0-1-2 etc] from $named_array
but I only have the value ( i100-1 etc) as a query option , This is so I can alter it later. What I want to achieve is something like , what is the index value of $named_array where value is i100-2
this is output to json at the end .
I hope this makes sense ! any help please ?
function complex_index_of($named_array, $val){
for($i=0, $n=count($named_array['vehicles']); $i<$n; $i++){
if ($named_array['vehicles'][$i]['vehicle'] == $val)
return $i;
}
return -1;
}
echo complex_index_of($named_array, 'i100-2 ');
// output: 1
Try something like this (maybe create a function, if you need to do it more than once)
$needle = 'i100-1';
$vIndex = -1;
foreach ($named_array["vehicles"] as $index => $data) {
if($data['vehicle'] == $needle) {
$vIndex = $index;
break;
}
}