Convert array laravel - arrays

I use Laravel framework and I have an array:
[▼
"0.022" => "24.00000000"
"0.013" => "506.00000000"
"0.041" => "65.00000000"
]
Could you help me to convert it to new format like this:
[▼
0 => {▼
"xxx": 0.022
"yyy": 24.00000000
}
1 => {▼
"xxx": 0.013
"yyy": 506.00000000
}
2 => {▼
"xxx": 0.041
"yyy": 65.00000000
}
]
Thank you very much.

$inputArray=array(
"0.022" => "24.00000000"
"0.013" => "506.00000000"
"0.041" => "65.00000000"
);
$outputArray=array();
foreach($inputArray as $key=>$val)
{
$obj['xxx']= $key;
$obj['yyy']= $val;
array_push($outputArray,$obj)
}
echo $outputArray;

$array = [
"0.022" => "24.00000000",
"0.013" => "506.00000000",
"0.041" => "65.00000000"
];
$data=array();
foreach($array as $key=>$value)
{
$data[]= ['xxx'=>$key,'yyy'=>$value];
}
echo "<pre>";
echo(json_encode($data));

Based on your output, you want increment index as the key
$item = [
'0.022' => '24.00000000',
'0.013' => '506.00000000',
'0.041' => '65.00000000'
];
$output = [];
$count = 0;
foreach($item as $key => $value) {
$output[$count]['xxx'] = $key;
$output[$count]['yyy'] = $value;
$count++;
}
echo json_encode($output);

Related

Laravel - How to insert multi-dimensional Array in DB

I have the array as below;
I'd like to insert each name keys into tableName and get the inserted id.
For the steps, each of them will be inserted into another table tableSteps including the last inserted id of the name.
Like as below screenshot.
In my controller,
Here's what I've done so far.
$instructionsArrays = $request->instructions;
$max = count($instructionsArrays);
for ($x = 1; $x <= $max; $x++) {
foreach($instructionsArrays as $instructionsArray){
Instruction::updateOrCreate(
['recipe_id' => session()->get('recipeArr.id'), 'sequence' => $x],
['name' => $instructionsArray['name']],
);
}
}
I was able to save sequence numbers but for names it saves only the last name key.
And... I'm really lost..
You can achieve what you want from 2 for loops
foreach($request->instructions as $key => $val){
$id = Instruction::insertGetId(
['recipe_id' => session()->get('recipeArr.id'), 'sequence' => $key + 1],
['name' => $val['name']],
);
$data = []; //bulk insertion
$created_at = now();
foreach($val["steps"] as $step){
array_push($data, ["header_id" => $id, "name" => $step, "sequence" => $key+1, "created_at" => $created_at]); //why insert sequence when you can obtain it from the relationship?
}
Steps::insert($data);
}
With the help of the answer of #Kneegrows, I came up with the code below and it is now working. Thank you.
foreach ($request->instructions as $key => $val) {
$instruction = Instruction::updateOrCreate(
['recipe_id' => session()->get('recipeArr.id'), 'sequence' => $key + 1],
['instructions_name' => $val['name']],
);
$id = $instruction->id;
$data = []; //bulk insertion
$i = 1;
foreach ($val["steps"] as $step) {
if(!is_null($step)){
array_push($data, ["instruction_id" => $id, "steps_name" => $step, "sequence" => $i]);
$i++;
}
}
Steps::insert($data);
}

Cant change single value in session array in Laravel

This my method update for products. And cant to change qty of product. Data saved inside foreach, but nothing change out of this. I can't understand what the problem.
if (!session()->has('products')) {
session()->put('products', []);
}
$products = session()->get('products');
foreach ($products as $item) {
if (isset($item[$product->id])) {
$item[$product->id]['qty'] = 10;
dd($products); // dd($item) - changes
$request->session()->put('products', $products);
return redirect()->back();
}
}
session()->push('products', [
$product->id => [
'product' => $product,
'qty' => 1
]
]);
return redirect()->back();
As you can see there is dd($products) - so its not change. But if dd($item) so it has qty 10.
I can solved this with next:
foreach ($products as $key => $value) {
if (isset($value[$product->id])) {
$value[$product->id]['qty'] = 10;
$products[$key] = $value;
$request->session()->put('products', $products);
return redirect()->back();
}
}
Just add a $key => $value and save it in $products[$key] = $value;

Array to string conversion insert to database

I have a problem with array to string again. But this time I need insert to database.
Checks::find($id)->update($request->all());
$issue[] = $request->issue;
foreach ($issue as $item) {
dd($item);
addIssues::create([
'check_id' => $id,
'issue_id' => $item
]);
}
Migration:
Schema::create('checks_issues', function (Blueprint $table) {
$table->string('check_id');
$table->string('issue_id');
$table->timestamps();
$table->primary(['check_id','issue_id']);
});
foreach ($issue as $item){
foreach ($item as $item1){
addIssues::create([
'check_id' => $id,
'issue_id' => $item1
]);
}
You should try this:
Checks::find($id)->update($request->all());
if(!empty($request->issue)){
$issue = explode(',',$request->issue);
foreach ($issue as $item){
addIssues::create([
'check_id' => $id,
'issue_id' => $item
]);
}
}

Merge 2 array/Hash into 1 variable Perl

I have 2 variables that are returned on form submission. I need to merge the values into 1 Variable that I can use to create SQL later on.
How can I get results that is union of all the values:
i.e. location_setting = '409','405' and Status = '501', '137', '124'
Here is my current code, but spits only values from h2
use Data::Dumper;
my $h1 = { 'location_setting' => [ '409' ], 'status' => [ '501' ] };
my $h2 = { 'status' => [ '137', '124' ], 'location_setting' => ['405'], 'classification' => ['0']};
my $x = {%$h1, %$h2};
print Dumper $x;
use List::Util qw( uniq );
my %h =
map {
$_ => [
uniq
$h1->{$_} ? #{ $h1->{$_} } : (),
$h2->{$_} ? #{ $h2->{$_} } : (),
]
}
uniq
keys(%$h1), keys(%$h2);
If you have lots of hashes or a variable number of hashes,
use List::Util qw( uniq );
my #hashes = ( $h1, $h2, ... );
my %h =
map {
my $key = $_;
$key => [ uniq map #$_, grep $_, map $_->{$key}, #hashes ]
}
uniq map keys(%$_), #hashes;
use Data::Dumper;
use List::Util qw( uniq );
my $h1 = { 'location_setting' => [ '409' ], 'status' => [ '501' ] };
my $h2 = { 'status' => [ '137', '124' ], 'location_setting' => [], 'classification' => ['0']};
my %x;
foreach my $h1key (keys %{$h1}) {
push #{$x{$h1key}}, #{${$h1}{$h1key}};
}
foreach my $h2key (keys %{$h2}) {
push #{$x{$h2key}}, #{${$h2}{$h2key}};
}
#$_ = uniq #$_
for values(%x);
my $x = \%x;
print Dumper $x;

foreach looping in multidimension array in php

Here is my PHP code:
$marks = array(
'Mohammad' => array('Phisics' => 50, 'Math' => 80),
'Arif' => array('Phisics' => 55, 'Math' => 95),
);
Now I want to get the Mohammad and Arif's marks with subject using foreach loop.
You can use a nested foreach loop:
foreach ($marks as $name => $subjects) {
foreach ($subjects as $subjectName => $subjectMark) {
echo "${name}'s mark for ${subjectName} is ${subjectMark}.";
}
}
Try this
$marks = array(
'Mohammad' => array('Phisics' => 50, 'Math' => 80),
'Arif' => array('Phisics' => 55, 'Math' => 95),
);
foreach ($marks as $key=>$names) {
echo "--$key Marks-- <br/>";
foreach ($names as $key=>$value) {
echo "$key:$value <br/>";
}
}
Output
--Mohammad Marks--
Phisics:50
Math:80
--Arif Marks--
Phisics:55
Math:95

Resources