How I can get 0 index value in codeigniter? - codeigniter

Array ( [0] => 18 [1] => 1 )
how i can get only 0 index value?. I am using this code in codeigniter. Can any one help?. Is this possible with for each loop so it access all indexes but show only zero index?
foreach($m as $m)
{
echo $m->['0'];
}

If you want only first index then you don't need to foreach loop
Just write:
echo $m[0];
And if you want all index of array then:
foreach ($m as $key => $value) {
echo $key;
}

Try this:
print_r($m[0]);
or
foreach($m as $m)
{
echo $m['0'];
}

Following is the code to get value of index 0
//$array variable declared containing multiple values
$array=array('0' =>"first value",'1' =>"second value",'2' =>"third value" );
echo "<pre>";
print_r($array); // helps in printing the value key and value
//iterating through each values in the array
foreach ($array as $key => $value) {
if($key==0) //checks if key is 0
{
echo $value; //prints the value in key 0
}
}

Related

check data that is not in array with 2 value matches

I have 2 arrays and I want to check those arrays with 2 value matches,
if those 2 value not matches I want to insert into database.
Code I have tried :
$data1 = {['reg_no'=>123,'name'=>'John'],['reg_no'=>1234,'name'=>'Lisa']};
$data2 = {['reg_no'=>123,'name'=>'John'],['reg_no'=>1234,'name'=>'Lisa'],['reg_no'=>12345,'name'=>'Roger']};
foreach($data1 as $d1)
foreach($data2 as $d2)
if($d1['reg_no'] != $d2['reg_no'] && $d1['name'] != $d2['name'])
//insert into database
// this not work because it will enter all data that not match
endif
endforeach
endforeach
you can use FirstOrNew Or FirstOrCreate in laravel.
like insert if not exist
$students = User::firstOrNew(['student_id' => $request->student_id, 'course_code' => $request->course_code]);
$students->foo = $request->foo;
$students->save();
$data1 = {['reg_no'=>123,'name'=>'John'],['reg_no'=>1234,'name'=>'Lisa']};
$data2 = {['reg_no'=>123,'name'=>'John'],['reg_no'=>1234,'name'=>'Lisa'],['reg_no'=>12345,'name'=>'Roger']};
$data = array_uintersect($data1, $data2, "strcasecmp");
if($data['reg_no'] != $data1['reg_no'] && $data2['reg_no']) {
// insert
}

Sum of multiple table row

How can i get sum of all these counts?
{{ DB::table('A')->count()}}
{{ DB::table('B')->count()}}
{{ DB::table('C')->count()}}
{{ DB::table('D')->count()}}
// Your Tables Which You Know Them Counts
$tables = ['A', 'B'];
// Our Count Start From Zero
$count = 0;
// Count Each Table And Add Our Count
foreach ($tables as $table) {
$tableCount = DB::table($table)->count();
$count += $tableCount;
}
// Display Count With dd()
dd( $count );
// Or Display In View
return view('index', compact('count'));
Like this?:
{{ $a = DB::table('A')->count()}}
{{ $b = DB::table('B')->count()}}
{{ $c = DB::table('C')->count()}}
{{ $d = DB::table('D')->count()}}
{{ $sum = $a + $b + $c + $d }}
But you may want to send all these data from a controller method instead of doing it in a view.

How to find difference of same column in foreach loop

I am take all field in for-each loop and take the difference of each two column
enter image description here
The above image 'tech_strt_km' is field name and take the difference of 122-22 and 200-122 how find these difference and store there values in another variyable i am using foreach loop for print these numbers
<?php
foreach($pexpn as $row) {
echo $row->tech_strt_km; ?><br>
} ?>
Any way to find the difference of same column
please help me !!
This should work fine. We add one to the current key to get the next value so we can subtract it from the current value, then we save these differences in an array diff for later usage.
$arr = array('22', '122', '200');
$diff = array();
foreach ($arr as $k => $v) {
if (!isset($arr[$k + 1])) {
// if we don't have a next item we are done
// break from foreach
break;
}
// abs only necessary if we expect negative differences and
// if we don't want that --- store differences in array
// for later usage
$diff[] = abs($arr[$k + 1] - $v);
}
print_r($diff);

how to print 2d array in laravel 5.4.12 by dd();?

please help me to solve this problem that how to die and dump 2D array in laravel and count array in an array.. i tried this but not working
$arr=collect($request->input('title['+$i+']'));
dd($arr->count());
also tried this code
$arr=collect($request->input('title[0]'));
dd($arr->count());
You do not say that you need to count them in your questions but here you go:
The aray:
$arrayname= array(
array('a', 'b', 'c'),
array('r', 't','y', 'u'),
);
The function
for ($i=0; $i < count($arrayname); $i++){
$singlearray[$i]= count($arrayname[$i]);
}
Result
array:2 [▼
0 => 3
1 => 4
]
dd($request->all()) will display contents of the array.
If you want to see the title, do this:
dd($request->title);
try to use:
echo count($arr);
dd($arr);
or
echo count($arr);
print_r($arr);
die;
$a = $request->all();
$flatArray = [];
$it = new RecursiveIteratorIterator(new RecursiveArrayIterator($a));
foreach($it as $v) {
$array[] = $v;
}
dd(count($flatArray));
Resource:
How to Flatten a Multidimensional Array?
i get it from the other site... Answer here
dd(count(request('title')[0]));
dd(count(request('title')['age']));

Check if a value is present in a comma separated varchar string

I have a record "id_zone" in database that store cites id like this $abilitato->id_zone = '1, 2, 3, 9, 50'
From laravel i need to check if a value passed from url is present in this field. The following work only if i have a single value
$abilitato = \App\Models\Servizi::where('id', $servizio)->('id_zone', $verifica->id_citta)->first();
if(!empty($abilitato) && empty($verifica->multicap)) {
$json = ['CAP' => '1' , 'DEB' => $cap ];
}else{
$json = ['CAP' => '0' , 'DEB' => $cap];
}
i need to check if
If you want to know if a value is in your array you can use this functión to know it.
here is a example:
// First convert your string to array
$myString = $abilitato->id_zone; // Here your string separate with comas
$array = explode(',', $myString); // Here convert it to array
$value = 7; // Here you put the value that you want to check if is in array
// Check if exist a value in array
if (in_array($value, $array)){
echo "Exist in array";
}else{
echo "No exist in array";
}
This is the documentation of these functions: explode , in_array
Regards!

Resources