This shouldn't be confusing me as much as it is but I am looking to turn this:
array:3 [▼
"subject" => array:2 [▼
0 => "math"
1 => "english"
]
"grade" => array:2 [▼
0 => "a"
1 => "b"
]
"received" => array:2 [▼
0 => "2017"
1 => "2016"
]
]
into this:
array:2 [▼
"0" => array:3 [▼
"subject" => "math"
"grade" => "a"
"received" => "2017"
]
"1" => array:3 [▼
"subject" => "english"
"grade" => "b"
"received" => "2016"
]
]
Tried looping through in a couple different ways but never seem to get the result I am looking for, any help would be much appreciated!
$keys = array_keys($array);
$result = array_map(
function (...$values) use ($keys) { return array_combine($keys, $values); },
...array_values($array)
);
Which is essentially this, but less repetitive:
array_map(
function ($subject, $grade, $received) {
return [
'subject' => $subject,
'grade' => $grade,
'received' => $received
];
},
$array['subject'],
$array['grade'],
$array['received']
)
See the manual for array_map and ... for more explanation.
simple Version:
$arr1 = array(...);
$arr2 = array();
foreach ($arr1 as $k => $v) {
foreach ($v as $x => $y) {
$arr2[$x][$k] = $y;
}
}
But you should add conditions, if the array element not exists, create it, or you may get Errors, depending on your PHP configuration.
Related
I got an array as follows.
I need to convert the values as integer
array:17 [
0 => array:2 [
"c" => "gmail"
"co" => "12"
]
1 => array:2 [
"c" => "dddd"
"co" => "2"
]
2 => array:2 [
"c" => "mmmmm"
"co" => "2"
]
3 => array:2 [
"c" => "dsf"
"co" => "2"
]
4 => array:2 [
"c" => "aaaa"
"co" => "1"
]
5 => array:2 [
"c" => "bbbb"
"co" => "1"
]
6 => array:2 [
"c" => "ccc"
"co" => "1"
]
7 => array:2 [
"c" => "yopmail"
"co" => "1"
]
8 => array:2 [
"c" => "yahoo"
"co" => "1"
]
]
I need to convert all values of the key co to integer ,where currently they are string.
Is this is the way to use the foreach,which didn't give me correct output
foreach($getDashboardDetails as $getDashboardDetails)
{
$getDashboardDetails['co']=(int)$getDashboardDetails['co'];
}
Hope Someone can help
I think the for loop is more what are you looking for as you want to change the initial array.
for($i=0;$i<=count($getDashboardDetails)-1;$i++) {
$getDashboardDetails[$i]["co"] = (int)$getDashboardDetails[$i]["co"];
$i++;
}
Or you can use foreach with a key-value pair on both dimensions, but I don't find it neccessary.
This might help you on your way(assuming $getDashboardDetails is the source array):
foreach($getDashboardDetails as $key => $value) {
foreach($value as $key1 => $value1) {
if ($key1 === "co") {
$getDashboardDetails[$key][$key1] = (int)$getDashboardDetails[$key][$key1];
}
}
}
Use below code to get it, your foreach is in incorrect foam.
$new_array = array();
foreach($getDashboardDetails as $key=>$value)
{
$new_array[$key]=array("c"=>$value['c'], "co"=>(int)$value['co']);
}
Now you have $new_array with expected results.
category:
array:3 [▼
"78f895684c" => "blue"
"f71db561ba" => "green"
"3e231651de" => "blue"
]
numbersGroup:
array:3 [▼
0 => array:4 [▼
"uuid" => "78f895684c"
"price" => "10"
"discount" => "0"
"total" => "10.00"
]
1 => array:4 [▼
"uuid" => "f71db561ba"
"price" => "2"
"discount" => "0"
"total" => "2.00"
]
2 => array:4 [▼
"uuid" => "3e231651de"
"price" => "50"
"discount" => "10"
"total" => "40.00"
]
]
I try to create an array that sorts my items into categories, but also sums all items in a category together. This is what I have:
foreach ($numbersGroup as $group) {
$cat = $category[$group['uuid']];
unset($group['uuid']);
$numbersArray[$cat][] = $group;
}
With the result:
array:2 [▼
"blue" => array:2 [▼
0 => array:3 [▼
"price" => "10"
"discount" => "0"
"total" => "10.00"
]
1 => array:3 [▼
"price" => "50"
"discount" => "10"
"total" => "40.00"
]
]
"green" => array:1 [▼
0 => array:3 [▼
"price" => "2"
"discount" => "0"
"total" => "2.00"
]
]
]
This is what I am trying to achieve:
array:2 [▼
"blue" => array:2 [▼
0 => array:3 [▼
"price" => "60"
"discount" => "10"
"total" => "50.00"
]
]
"green" => array:1 [▼
0 => array:3 [▼
"price" => "2"
"discount" => "0"
"total" => "2.00"
]
]
]
This is my approach:
foreach ($numbersGroup as $group) {
$cat = $category[$group['uuid']];
unset($group['uuid']);
foreach ($group as $key => $value) {
$sum += $value;
}
$numbersArray[$cat][] = $sum;
}
But I get the error:
Unsupported operand types
Check if $numbersArray[$cat] is already set - if so, don’t add a new array element, but add the individual values to the existing array keys.
foreach ($numbersGroup as $group) {
$cat = $category[$group['uuid']];
unset($group['uuid']);
if(isset($numbersArray[$cat])) {
$numbersArray[$cat]['price'] += $group['price'];
$numbersArray[$cat]['discount'] += $group['discount'];
$numbersArray[$cat]['total'] += $group['total'];
}
else {
$numbersArray[$cat] = $group; // no [] here, because we want only a single element
}
}
Or, without an explicit if, using the ?? operator instead:
foreach ($numbersGroup as $group) {
$cat = $category[$group['uuid']];
$numbersArray[$cat]['price'] = ($numbersArray[$cat]['price'] ?? 0) + $group['price'];
$numbersArray[$cat]['discount'] = ($numbersArray[$cat]['discount'] ?? 0) + $group['discount'];
$numbersArray[$cat]['total'] = ($numbersArray[$cat]['total'] ?? 0) + $group['total'];
}
--
Edit:
I unfortunately I cannot write into my code "price", "discount" etc, because these values are always different, this cannot be hardcoded
Then loop over the keys you get from group, as you tried to with your initial code already:
foreach ($numbersGroup as $group) {
$cat = $category[$group['uuid']];
unset($group['uuid']);
foreach($group as $key => $value) {
$numbersArray[$cat][$key] = ($numbersArray[$cat][$key] ?? 0) + $value;
}
}
Can you try the below code
foreach ($numbersGroup as $group) {
$cat = $category[$group['uuid']];
if(!isset( $numbersArray[$cat][0]) )
$numbersArray[$cat][] = $group;
else{
// If index is known, you can use below code
/*$numbersArray[$cat][0]["price"]+= $group['price'];
$numbersArray[$cat][0]["discount"]+= $group['discount'];
$numbersArray[$cat][0]["total"]+= $group['total'];*/
// If index is not known
foreach($group as $key => $value){
if (is_numeric($value)) {
$numbersArray[$cat][0][$key]+= $group[$key];
}
}
}
}
Now I have added the case if array index is not known. We need a numeric checking before adding the values.
I hope this helps you :)
Am working on some set of PHP array. Am trying to loop through each of them and check
the array whose name is equal to Josw Acade. Am using a for loop but I get zero
after extracting the data. I want to store the data in an array.
Array
array:6 [
0 => array:4 [
"id" => 1
"name" => "Josw Acade"
"value" => "Unlimited"
"plan_type" => "Superior"
]
1 => array:4 [
"id" => 2
"name" => "Verbal"
"value" => "true"
"plan_type" => "Superior"
]
2 => array:4 [
"id" => 12
"name" => "Josw Acade"
"value" => "$1,500,00"
"plan_type" => "Classic"
]
3 => array:4 [
"id" => 13
"name" => "Leon"
"value" => "true"
"plan_type" => "Classic"
]
4 => array:4 [
"id" => 14
"name" => "One Time"
"value" => "true"
"plan_type" => "Classic"
]
5 => array:4 [
"id" => 15
"name" => "Deat"
"value" => "$25,000"
"plan_type" => "Classic"
]
6 => array:4 [
"id" => 23
"name" => "Josw Acade"
"value" => "$100,000"
"plan_type" => "Essential"
]
]
Logic
$Inst = [];
for($med = 0; $med < count($array); $med++){
if($med['name'] == "Josw Acade"){
$Inst = $med['value'];
}
}
dd($Inst);
Your variables is not corretly set in the for loop, you are setting $med = 0 and acessing $med as an array.
Use filter, that runs a condition on each element and returns the items that satisfy that condition.
array_filter($array, function ($item) {
return $item['name'] === 'Josw Acade';
});
In general you don't have to make old school arrays anymore, foreach does the same.
$results = [];
foreach($array as $item)
{
if ($item['name'] === 'Josw Acade') {
$results[] = $item['value'];
}
}
You can use array_filter with callback
$filtered = array_filter($array, function($v){ return $v['name'] == 'Josw Acade'})
print_r($filtered);
You are looping through array; so on each iteration to get values; you need to pass index value and you are missing that. You are using $med as index.
Here is code.
$Inst = [];
for($med = 0; $med < count($array); $med++){
if($array[$med]['name'] == "Josw Acade"){
$Inst[] = $array[$med]['value'];
}
}
there is many way to do this but according to me the best way to use array_filer()
array_filter($array, function ($item) {
return $item['name'] === 'Josw Acade';
});
i need to convert a associative array to a 2 dimensional array in php, the origin array is as followed
array:7 [▼
"data" => "data"
"id_1553539135251" => "<p>nsmn</p>"
"about" => "about"
"id_1553539141598" => "<p>uiu</p>"
my code
$data = $request->all();
$json = array();
foreach($data as $key => $value){
if(strpos($key, 'id') !== false){
$json[$key]['content'] = $value;
}
}
i need the output of the following for each to be
array:3 [▼
"id_1553539135251" => array:1 [▼
"content" => "<p>nsmn</p>"
"data" => "data"
]
"id_1553539141598" => array:1 [▼
"content" => "<p>uiu</p>"
"about" => "about"
]
]
but my code outputs
array:3 [▼
"id_1553539135251" => array:1 [▼
"content" => "<p>nsmn</p>"
]
"id_1553539138029" => array:1 [▼
"content" => "<p>jjkjk</p>"
]
"id_1553539141598" => array:1 [▼
"content" => "<p>uiu</p>"
]
]
guidance on how to achieve the desired output is appreciated.
<?php
$test=array(
array(
"data" => "data",
"id_1553539135251" => "<p>nsmn</p>",
"about" => "about",
"id_1553539141598" => "<p>uiu</p>"
),
);
$output=array();
foreach($test as $item){
$i=0;
$tt='';
foreach($item as $k=>$v){
if(strpos($k, 'id') !== false){
$output[$k]=array(
'content'=>$item[$k],
'header'=>$tt,
);
}else{
$tt=$v;
}
}
}
print_r($output);
I encountered this kind of array in my tasks. It's a multi dimensional array.
In the form, I have this,
particular[particular][]
particular[percentage][]
particular[remarks][]
So what I'm doing is, I'm getting request
$inputs = $request->all();
this returns
array:3 [▼
"particular" => array:3 [▶]
"percentage" => array:3 [▶]
"remarks" => array:3 [▶]
]
inside, each of them have
array:3 [▼
"particular" => array:3 [▼
0 => "11"
1 => "22"
2 => "33"
]
"percentage" => array:3 [▼
0 => "11"
1 => "22"
2 => "33"
]
"remarks" => array:3 [▼
0 => "na1"
1 => "na2"
2 => "na3"
]
]
I tried looping it
foreach ($inputs as $input) {
dd($input);
}
but I only get the first one "particular" => array:3 [▶]
What I'm after is, I need to save those value in database
$particular = new Particular;
$particular->particular = particular;
$particular->percentage = percentage;
$particular->remarks = remarks;
$particular->save();
dd means "dump and DIE" so your script dies on first iteration.
You can:
$particular = new Particular;
$particular->particular = $inputs['particular'];
$particular->percentage = $inputs['percentage'];
$particular->remarks = $inputs['remarks'];
$particular->save();
Also, if you need to just dump something - there's a dump function (surprise!):
foreach ($inputs as $input) {
dump($input);
}
I managed to find a solution. I don't know if this is the proper, but for now atleast it work for me.
$array1 = $request->particular;
$array2 = $request->percentage;
$array3 = $request->remarks;
$merge = [$array1, $array2, $array3];
foreach ($merge[0] as $key => $value) {
$particular = new Particular;
$particular->particular = $merge[0][$key];
foreach ($merge[1] as $x => $y) {
$particular->percentage = $merge[1][$key];
}
foreach ($merge[2] as $i => $d) {
$particular->remarks = $merge[2][$key];
}
$particular->report_id = $report->id;
$particular->save();
}