Hi i need to solve this case in session cart that have array in session
The size duplicated many times in blade like this
Size:
large
large
large
array:1 [▼ // app/Http/Controllers/Controller.php
1 => array:3 [▼
"id" => 1
"quantity" => 36
"color_items" => array:3 [▼
0 => array:2 [▼
"color" => "3"
"size" => "2"
]
1 => array:2 [▼
"color" => "2"
"size" => "2"
]
2 => array:2 [▼
"color" => "1"
"size" => "2"
]
]
]
]
<dl class="card-item-desc-1">
<dt>Size:</dt> #foreach($details['color_items'] as $de)
<dd>
{{ \App\Models\Size::find($de['size'][0])->name }}
</dd>
#endforeach
</dl>
You have a couple of options.
First strip the size out of the color_items array and use as its own array.
Second would be to use the power of collections
Change the foreach to
#foreach(collect($details['color_items'])->pluck('size')->unique() as $size)
<dd>
{{ \App\Models\Size::find($size)->name }}
</dd>
#endforeach
So what we are doing is adding the color_items to a collection, taking only the size field and then making sure its unique.
Hope that helps
Related
My project has an array made of a request with an array of inputs inside, as it follows:
array:8 [▼
"type_id" => array:1 [▼
0 => "1"
]
"zip_code" => array:1 [▼
0 => "88801500"
]
"street_address" => array:1 [▼
0 => "Avenida Getúlio Vargas"
]
"number" => array:1 [▼
0 => "asdasd"
]
"street_address_2" => array:1 [▼
0 => "asdasd"
]
"city_id" => array:1 [▼
0 => "4384"
]
"neighborhood" => array:1 [▼
0 => "Centro"
]
"created_by" => 2
]
But when I try to run said array on a foreach to insert it on the database, I get a result that doesnt make sense:
array:1 [▼
0 => "1"
]
My code:
dd($dataEnderecos); //This is for debug purposes, it shows the initial array on this question.
foreach ($dataEnderecos as $enderecos) {
dd($enderecos); //This is for debug purposes, it shows the secondary array on this question.
$enderecoID = $this->address->create($enderecos)->id;
$this->repository->enderecos()->attach($enderecoID);
}
I managed to fix this by using a for loop and using another variable to receive the results of the initial array:
for ($i = 0; $i < $limit; $i++) {
$insert = array(
'type_id' => $dataEnderecos['type_id'][$i],
// ^^^^ ^^^^ ^^^^
// Initial Array Secondary Array Index
);
}
i have form to make a bill for multiple products,
that form return request like this
#parameters: array:5 [▼
"quantity" => array:2 [▼
0 => "1"
1 => "2"
]
"product" => array:2 [▼
0 => "Mr. Jasen Beer,OliveDrab,XS"
1 => "Carlotta Yundt"
]
"date" => array:2 [▼
0 => "2019-12-29"
1 => "2019-12-29"
]
"id" => array:2 [▼
0 => "15"
1 => "11"
]
]
}
i need to loop all arrays to make insert all at once
thanks.
Try to loop it like this, you will get the array wrap every product's attributes:
$inserted_array = [];
foreach($parameters as $key => $values) {
foreach($values as $index => $val) {
$inserted_array[$index][$key] = $val;
}
}
\DB::table('table_name')->insert($inserted_array);
And I found that there is id in your array, remember to make it fillable.
I have the following multidimensional array :
meeting[$loop->index][person]are checkboxes
meeting[$loop->index][date] are input-fields
array:9 [▼
0 => array:1 [▼
"date" => null
]
1 => array:2 [▼
"person" => "Max Example"
"date" => "10.05"
]
2 => array:1 [▼
"date" => null
]
3 => array:1 [▼
"date" => null
]
4 => array:1 [▼
"date" => null
]
5 => array:1 [▼
"date" => null
]
6 => array:1 [▼
"date" => null
]
7 => array:1 [▼
"date" => null
]
8 => array:1 [▼
"date" => null
]
]
person in this case is a checkbox, so it's only there when it's checked.
Now I want to know, how many persons are 'invited' to the meeting.
So I need to count the amount of person (check how often person exists).
When only one person is invited I want to display something else than when more people are invited.
I tried it with this:
#if ($counts = array_count_values(array_flip(array_column($ticketDaten['hefte'], 'heft'))) == 1)
[...]
but if I var_dump it I only get bool(false) in both cases (only one person or more).
You should be able to simply
echo count(array_column($meeting, 'person'));
Demo on 3v4l.org
I have completely changed the format of my array now, and I had what I hope is a simple misunderstanding on my part. So my array now looks like the following
array:9 [▼
0 => array:4 [▼
"leadData" => array:7 [▼
"LeadID" => "1232806"
"Client" => "Some Client"
"LeadName" => "Test"
"Owner" => "Someone"
"Value" => "2160.00"
]
"clientData" => array:2 [▼
"Prospect" => "No"
]
"quoteData" => array:8 [▼
"QuoteID" => "Q0020"
"ProjectName" => "Test"
"Amount" => "1234"
]
"customData" => array:2 [▼
0 => array:1 [▼
"Type" => "New"
]
1 => array:1 [▼
"Month" => "June 16"
]
]
]
2 => array:4 [
...
]
]
So it is essentially now 4 inner arrays. Now if I do the following, I can print out all the data for the leadData inner array
foreach($leadArray as $array)
<tr>
foreach($array['leadData'] as $leadKey => $leadData)
<td>
{{ $leadData }}
</td>
endforeach
</tr>
endforeach
That works fine. However, I only want to display certain parts of this array. I would have presumed doing something like the following would work
foreach($leadArray as $array)
<tr>
foreach($array['leadData'] as $leadKey => $leadData)
<td>
{{ $leadData['LeadID'] }}
</td>
<td>
{{ $leadData['LeadName'] }}
</td>
endforeach
</tr>
endforeach
However if I do this I get and Illegal String Offset error. Is this not how I would access this data?
p.s. Ignore the way I do the foreach loop etc, this is because I am using a template engine.
Thanks
You don't have to loop over the second array, you can use the keys to get the data.
foreach($leadArray as $array)
<tr>
<td>
{{ $array['leadData']['LeadID'] }}
</td>
<td>
{{ $array['leadData']['LeadName'] }}
</td>
</tr>
endforeach
Your main array will be like this, where you want to work, right?
<pre>
$aMainArray = array(
0 => array(
"leadData" => array(
"LeadID" => "1232806",
"Client" => "Some Client",
"LeadName" => "Test",
"Owner" => "Someone",
"Value" => "2160.00",
)
)
);
foreach ($aMainArray AS $aSubArray) {
print_r($aSubArray);
// You can echo your required values like below
echo $aSubArray['leadData']['LeadID'];
echo $aSubArray['leadData']['LeadName'];
// OR like this one
foreach ($aSubArray AS $value) {
echo $value['LeadID'];
echo $value['LeadName'];
}
}
</pre>
I am trying to get the first element from a subarray. The data needed in my result collection looks like this:
#attributes:array:11 [▼
"id" => 1
"title" => "Eerst nieuwsbericht voor Jochen"
"content" => "<p>Dit is een test</p>\n"
"tags" => ""
"images" => "[2,3,4,1]"
"social_media" => ""
"publish_date" => "2015-08-27 01:40:17"
"created_at" => "2015-08-27 13:40:17"
"updated_at" => "2015-08-27 13:40:17"
"slug" => "eerst-nieuwsbericht-voor-jochen"
"files" => array:4 [▼
2 => array:11 [▼
"id" => 2
"parent_id" => 0
"type" => "file"
"path" => "/uploads/flyfish"
"name" => "05-vogelkers1-E2.jpg"
"file_type" => "jpg"
"size" => 162936
"width" => 1024
"height" => 768
"created_at" => Carbon {#368 ▶}
"updated_at" => Carbon {#338 ▶}
]
3 => array:11 [▶]
4 => array:11 [▶]
1 => array:11 [▶]
]
]
I want to get the 'name' from the first element in the sub array 'files'. Is there a shorthand notation to do so?
I can get to my needed result with
$newsDetail->files[2]['name']
But then I need to know that key 2 is actually the first key in the array 'files'.
reset() rewinds array's internal pointer to the first element and
returns the value of the first array element.
So i believe in your case:
var_dump(reset($newsDetail->files)['name'])
Simply do:
current($newsDetail['files']);