I need to convert the laravel form validation error array to a string only the values.
I do not want use json_encode.
in my case I am trying to do the save edit function with ajax.
so I decided to get the form validation errors as a string from the controller.
writing a call back function to convert the array of errors to a string, so the error key may change form to form. how do I do this.
Is there a better way to handle this situation..
This is what I tried
if ($validator->fails()) {
$error = $validator->getMessageBag()->toArray();
echo '<pre>'; print_r($error); echo '</pre>';
$error_str = '';
foreach ($error as $row){
$error_str .= $row[0].'</br>';
}
echo $error_str;
}
You should not use $row[0]because it will only fetch first error from a list of errors of specific field.
Here is a solution to get all errors in one string with its field name.
$str = implode("\n",
array_map( function( $e, $key ){
return $key.": ". implode(", ",$e);
}, $error, array_keys($error) )
);
print_r($str);
I am trying to access a specific value from an array, but have difficulties fetching it. More specifically it is the value DealerCarExtended-->ImageIds-->ImageId-->Id, but my problem is that ImageIds is an array with multiple ImageId's. I use son_decode, but the code below obviously doesn't work.
$response = file_get_contents( 'http://api.autoit.dk/car/GetCarsExtended/391B093F-BB4A-45AA-BEFF-7B33842401EA' );
$myArray = json_decode($response,true);
$myArray = $myArray[0];
echo $myArray['ImageIds']['ImageId']['Id'];
I'm sure that this is trivial for most of you guys but i'm a newbie in this :-)
A working example :
$response = file_get_contents( 'http://api.autoit.dk/car/GetCarsExtended/391B093F-BB4A-45AA-BEFF-7B33842401EA' );
$myArray = json_decode($response,true);
$myArray = $myArray[0];
# fetching second array item :
echo $myArray['ImageIds'][1]['Id']
i have a form data and the data's array like this:
$datas=array("x-1","y-2","y-2","y-3","t-1");
my foreach loop:
foreach($datas as $x => $data){
$data=explode("-",$data);
if($data[0]==$data[0]+1){$n=1;}else{$n=0;}
$keys[$x]=$data[0].$n++;
$vals[$x]=$data[1];
}
i couldn't write the true code, my 3rd line is wrong i think (if($data[0]=$data[0]+1){$n="1";}else{$n="";})
so, i wanna rename the duplicate keys by giving number. my output should be like this:
x=1 y1=1 y2=2 y3=2 t1=1
Try
$datas=array("x-1","y-2","y-2","y-3","t-1");
$i=0;
$n=1;
foreach($datas as $x => $data){
$data=explode("-",$data);
$data2=explode("-",$datas[$i+1]);
if($data[0]==$data2[0])
{
$keys[$x]=$data[0].$n;
$n=$n+1;
}
else
{
$keys[$x]=$data[0].$n;
$n=0;
}
$vals[$x]=$data[1];
$i++;
}
This code has error you use = Which will assign value , not compare it.
also n should be integer not string
To fix that
foreach($datas as $x => $data){
$data=explode("-",$data);
if($keys[$x]==$keys[$x+1]){$n=1;}else{$n=0;}
$keys[$x]=$data[0].$n++;
$vals[$x]=$data[1];
}
if($data[0]=$data[0]+1){$n="1";}else{$n="";}
Use == instead of = for the if statements
Really need help on this :(I 'll try to be as simple as possible.
I got one big file looking like this:
ID,Info1,Info2,info3,...
On each line, i got one ID and a lot of stuff, comma separated. There can be > 3000 lines.
Now i got a second file like this :
ID,Info4,Info5,Info6,...
The first file contains ALL the elements whereas the second file contains only some of them.
For example, first one:
BLA1,some stuff...
BLA2,some stuff...
BLA3,some stuff...
ALO1,some stuff...
ALO2,some stuff...
And the second one :ยจ
BLA3,some stuff2...
ALO1,some stuff2...
BLA1,some stuff2...
What i want is simple, I want to append all the 'some stuff2...' of the second file to the first one like a join type=left with sql
I want the first file to have now :
BLA1,some stuff...,some stuff2...
BLA2,some stuff...
BLA3,some stuff...,some stuff2...
ALO1,some stuff...,some stuff2...
ALO2,some stuff...
I tried something like this :
ForEach ($line in $file1) {
$colA = $line.Split(',')
ForEach ($line in $file2) {
$colB = $line.Split(',')
if($colA[0]-eq $colB[0]) { #Item found in file2
$out += $date + $colA[1]+","+ ... +","+ $colB[1]+","+ ... +"`n"
}else {
$out += $date + $colA[1]+","+ ... +"`n"
}
}
}
But it takes so much time it dosnt success (and maybe there were other problems i didnt see). What's the best way? a 2D Array? I could try to sort the IDs and then script a little, but as its not numerical only i don't know how to process.
Thks a lot guys for your help,
Use a hashtable where the key is the ID.
$ht = [ordered]#{}
foreach ($line in $file1) {
$id,$rest = $line -split ',',2
$ht[$id] = $line
}
foreach ($line in $file2) {
$id,$rest = $line -split ',',2
if ($ht.ContainsKey($id)) {
$ht[$id] += ",$rest"
}
else {
$ht[$id] = $line
}
}
$ht.Values > newfile.txt
I went with the assumption that you either have known header lines or can add them...
f1.csv
Name,Item_1
BLA1,thing_bla1_1
ALB1,thing_alb1_1
BLA2,thing_bla2_1
ALB2,thing_alb2_1
BLA3,thing_bla3_1
ALB3,thing_alb3_1
f2.csv
Name,Item_2
BLA3,thing_bla3_2
ALB3,thing_alb3_2
BLA1,thing_bla1_2
ALB1,thing_alb1_2
BLA2,thing_bla2_2
ALB2,thing_alb2_2
Code:
$grouped = Import-Csv .\f1.csv, .\f2.csv | group -property Name -ashashtable
$($grouped.Keys | foreach {$obj = $grouped.Item("$_")[0].Name + "," + $grouped.Item("$_")[0].Item_1 + "," + $grouped.Item("$_")[1].Item_2; $obj}) | Out-File .\test.csv
What we are doing here is importing the two CSVs into one element, then grouping the items of the same name in the hash table. Then we pipe the keys (the non-duplicated names from the files) into a foreach that combines them into one line. We need the $() around those statements to allow the output to be piped to Out-File.
I'm nearly positive that there is a cleaner way to do the inside of the foreach, but this does work.
The output (text.csv):
ALB1,thing_alb1_1,thing_alb1_2
BLA2,thing_bla2_1,thing_bla2_2
ALB3,thing_alb3_1,thing_alb3_2
BLA1,thing_bla1_1,thing_bla1_2
ALB2,thing_alb2_1,thing_alb2_2
BLA3,thing_bla3_1,thing_bla3_2
If you want to do a LEFT JOIN, you could load the files into a temporary database and actually do a LEFT JOIN. See here for an example using SQLite.
I add my session variables like this:
foreach ( $data as $key => $value ) {
$this->Session->write("MyVariable.$key", $value );
}
Is it possible to add element to session variable array without passing the key ?
I mean like this:
$MyArray[] = "apple";
$MyArray[] = "banana";
So is it possible to add like this? Pseudo code:
$this->Session->write('MyVariable'.[], "apple");
$this->Session->write('MyVariable'.[], "banana");
Edit: $data array was for giving an example. The data that will be saved is not array. It is a string. Everytime I add to session variable I don't want to give key by code. I wonder whether is it possible out of the box. In my current codes I make it like this:
$newKey = count( $this->Session->read("MyVariable") );
$this->Session->write("MyVariable.$newKey", "apple");
Hi i guess it should be like this:
foreach ( $data as $key => $value ) {
$this->Session->write('MyVariable.'.$key, $value );
}
You have to place a dot inside the quotation mark.
If you don't want to give the key value each time, then store it as an array like #mark said
$this->Session->write("MyVariable", $data);
If you want to add a new value to the $data array in some other part of your code, you'll have to do something like
$data = $this->Session->read("MyVariable");
$data[] = array('other'=>'value');
$this->Session->write("MyVariable", $data);
Or add the exact key like #mmahgoub said
$this->Session->write("MyVariable".$key, $value);