Laravel: Access Rules::in variable items - laravel

How to access the items listed in an Illuminate\Validation\Rules\In variable ?
use Illuminate\Validation\Rules\In;
$foo = Rule::in('a', 'b');
$foo->toString() // error
The only way I found to show it is:
>>> dump($foo)
Illuminate\Validation\Rules\In^ {#3512
#rule: "in"
#values: array:2 [
0 => "a"
1 => "b"
]
}

you can convert that Illuminate\Validation\Rules\In to collection then take the result array:
$foo = Rule::in('a', 'b');
$values = collect($foo)->values()[1];
to get the string Representation from it:
$stringRepresentation=$foo->__toString();

Related

How do I output the diffence between values of Array A to Array B in Laravel?

I have an array that plucks values to an array like so:
/* Pluck just the wattage values to an array */
$realtime_data_array = $latestrtfeed->nth(60)->reverse()->pluck('data')->toArray();
which results in:
array:150 [▼
0 => 20277.6
1 => 20281.4
2 => 20285.3
3 => 20289.7
4 => 20293.8
5 => 20298.6
6 => 20303.2
7 => 20307.4
8 => 20311.5
9 => 20315.8
10 => 20319.8
these value get inputted to the chart like so:
$realtime_consumption_chart->dataset('kWh', 'line', $realtime_data_array);
The problem is that the sensor is storing cumulative values and I only want to show the difference between value 0 and value 1 then difference between value 1 and value 2 and so on and so on.
How would I go about something like that?
I am going to take a shot and say that I'm going to have to do a FOREACH on the array and create a new array with the values adjusted, but how do I go about that? maybe:
foreach ($realtime_data_array as $data) {
$realtime_data_array_corrected = ($data[1]-$data[0])->toArray();
}
Nope there is something I'm doing wrong or not considering here.
for ($i = 0; $i < sizeof($realtime_data_array); $i++) {
if($i==0) {
$realtime_data_array_corrected[$i] = 0;
continue;
}
$realtime_data_array_corrected[$i] = $realtime_data_array[$i] - $realtime_data_array[$i-1];
}

PHP array_map() always change my first index value to zero value

I have a controller in Laravel
This is my collection
$milestones = $this->getmilestones();
dump($milestones);
and the value is
array:3 [▼
0 => "["109"
1 => "110"
2 => "111"]"
]
And I tried this code based on the answer here.
So, I have code like this
array_unshift($milestones, $milestones[0]);
unset($milestones[0]);
dump($milestones);
and the value is (index was changed)
array:3 [▼
1 => "["109"
2 => "110"
3 => "111"]"
]
So, after unshifting the collections, I tried to use array_map to convert array of strings to array of integers.
$milestones = array_map('intval', $milestones);
dump($milestones);
But, I still got the same value. The first index returns 0 like this
array:3 [▼
1 => 0
2 => 110
3 => 111
]
What should I do?
Try this one
array_splice($milestone, 0, 1);
dump($milestone);
Ah, finally I got the results that I wanted. I try to remove square brackets and double quote. Because milestones is collection. So my code is
$milestones = str_replace(array('[', ']', '"'),'',$milestones);
Thank you all for your help
Use array_values this should re-index your array the way you need it:
$milestones = array_values($milestones);
If $milestones is a collection:
$milestones = $milestones->values();
The method values() will call array_values on your items defined in your collection instance.
Source: http://php.net/manual/en/function.array-values.php

create method in store function stores array value in db

$array = array($t, $c, $s);
foreach ($array as $a) {
receipt::create($a);
}
i want to pass the array content to database but i got this error
"Type error: Argument 1 passed to Illuminate\Database\Eloquent\Builder::create() must be of the type array, string given,
Actually while insert we should provide the array data even single field or more no of fields.
For example two fields : receipt::create($a). In this $a should contain two values in array format.
New example updated below as per your request. In this your $a variable should be like below format
$a = array(
'user_id' => $userId,
'receipt_description' => $desc
);
$receiptId = receipt::create($a)->id;

JSON is object instead of array, if array_diff returns assoc array on Collection->toArray()

My issue is in my json I am expecting an array, but am getting an object.
Details:
I have an array of numbers:
$numbers = [1];
I select from relationship, the "drawn numbers":
$drawnNumbers = Ball::whereIn('number', $numbers)->where('game_id', $card->game->id)->get()->map(function($ball) {
return $ball->number;
})->toArray();
I do a ->toArray() here. I want to find the numbers in $numbers that do not occur in $drawnNumbers. I do so like this:
$numbersNotYetDrawn = array_diff($numbers, $drawnNumbers);
My method then return $numbersNotYetDrawn (my headers accept is application/json).
So now the issue. When $drawnNumbers is an empty array, then the printed json is a regular array like this:
[
1
]
However if the relationship returns $drawnNumbers to be an array with numbers, then json is printed as an object:
{
"0" => 1
}
Does anyone know why this is? Anyway to ensure that json is array?
Edit:
Here is my actual data:
$drawnNumbers = Ball::whereIn('number', $numbers)->where('game_id', $card->game->id)->get()->map(function($ball) {
return $ball->number;
})->toArray();
$undrawnNumbers = array_diff($numbers, $drawnNumbers);
// $undrawnNumbers = array_values(array_diff($numbers, $drawnNumbers)); // temp fix
Replace
$numbersNotYetDrawn = array_diff($numbers, $drawnNumbers);
with
$numbersNotYetDrawn = array_values(array_diff($numbers, $drawnNumbers));
to make sure element keys are reset and array is treated as a simple list and serialized to a JSON list - instead of being treated as an associative array and serialized to a JSON object.
I recently had this same problem and wondered the same thing.
I solved it by adding "array_values", but I was wondering how to reproduce it.
I found it that it is reproduced when array_diff removes an element from the array that isn't the last element. So:
>>> $x
=> [
1,
2,
3,
4,
5,
]
>>> array_diff($x, [5]);
=> [
1,
2,
3,
4,
]
>>> array_diff($x, [1]);
=> [
1 => 2,
2 => 3,
3 => 4,
4 => 5,
]

Cakephp 3 How to make session array

I am trying to write session in controller. My structure is
$_SESSION['a'][0] = 1;
$_SESSION['a'][1] = 2;
$_SESSION['a'][2] = 3;
And I am trying this
Configure::write('Session', ['a' =>'1'])
But it is not working. How do this in cakephp 3 way
To write variable in Session in CakePHP 3 you need to write following code :
$this->request->session()->write('Your Key',Your_array);
To know more information you can visit here :
http://book.cakephp.org/3.0/en/development/sessions.html
To make things perfectly clear:
// code writing array to session
$a = [ "abc" => "word", "123" => 42, "?" => $b ];
$a["more"] = "if you need to add";
$a[] = "whatever";
$this->request->session()->write( 'my_array', $a );
// code reading array from session
$recall = $this->request->session()->read( 'my_array' );
debug( sprintf( "What's the word? [%s]", $recall["abc"] ) );
You can simply use
$session->write([
'key1' => 'blue',
'key2' => 'green',
]);
I am refering to
http://book.cakephp.org/3.0/en/development/sessions.html#reading-writing-session-data
The answer is that this cannot be done in CakePHP 3.x
In vanilla PHP, it's possible to do this:
<?php
session_start();
$_SESSION['a'][0] = 1;
$_SESSION['a'][1] = 2;
$_SESSION['a'][2] = 3;
var_dump($_SESSION);
?>
Which will output:
array(1) {
["a"]=> array(3) {
[0]=> int(1)
[1]=> int(2)
[2]=> int(3)
}
}
This is correct, and what should happen.
In CakePHP, you cannot specify arrays in the session key. For example:
$this->request->session()->write('a[]', 1);
$this->request->session()->write('a[]', 2);
$this->request->session()->write('a[]', 3);
Will not work.
If you remove the [] the value will get overwritten. For example:
$this->request->session()->write('a', 1);
$this->request->session()->write('a', 2);
$this->request->session()->write('a', 3);
The value of $this->request->session()->read('a') would be 3. The values 1 and 2 have been overwritten. Again, this is to be expected because you're overwriting the key a each time. The equivalent vanilla PHP for this is:
$_SESSION['a'] = 1;
$_SESSION['a'] = 2;
$_SESSION['a'] = 3;
Due to the lack of an indexed array, $_SESSION['a'] gets overwritten each time. This is normal behaviour. It needs to have the indexes (e.g. ['a'][0], ['a'][1], ...) to work!
The other answers where they have given things like key1 and key2 are not appropriate. Because there are many situations where you want everything contained within an indexed array. Generating separate key names is wrong for this type of scenario.
My edit of the accepted answer was rejected, so I present the - seemingly necessary - explicit code example, for the benefit of #Andy and others.
// code to write to session
$a = [ 0 => "zero", 1 => "one", 2 => "two" ];
$a[] = "three";
$this->request->session()->write( 'my_array', $a );
// code to read from session
$z = $this->request->session()->read( 'my_array' );
debug( $a[3] ); // outputs "three"

Resources