Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 days ago.
Improve this question
I have one multi dimensional array in php. I want Sum of multidimensional array in php based on key value
$array = [
0 => [
"id" => "1",
"title" => "Title 1",
"price" => "2.50",
],
1 => [
"id" => "2",
"title" => "Title 2",
"price" => "2.50",
],
2 => [
"id" => "1",
"title" => "Title 1",
"price" => "2.50",
]
];
I want below output as merge array and sum of price.
OUTPUT
$array = [
0 => [
"id" => "1",
"title" => "Title 1",
"price" => "5.0",
],
1 => [
"id" => "2",
"title" => "Title 2",
"price" => "2.50",
]
];
Please help me guys.
I have tried with arrray_search and array_key_exist. but can't find any solution.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 days ago.
Improve this question
I am having array of php with parent and children mapping on mentioned columns, I had wasted more time doing r&d and execution on this can anyone please suggest on this.
$arr = [
0 => [
"id" = 1,
"parent_id" => 0,
],
1 => [
"id" = 2,
"parent_id" => 1,
],
2 => [
"id" = 3,
"parent_id" => [1,2],
],
3 => [
"id" = 4,
"parent_id" => 1,
],
4 => [
"id" = 5,
"parent_id" => 4,
],
];
i am expecting output
$output = [
0 => [1,2,3],
1=> [1,4,5]
]
I am looking for the best way to convert a multidimensional associative array into a new array, where each row is the concatenation of each column ex :
$datas[] = [
"id" => "1000",
"parent" => "0",
"level" => "1",
"children" => [
[
"id" => "1001",
"parent" => "1000",
"level" => "2",
"children" => [
[
"id" => "1002",
"parent" => "1001",
"level" => "3",
"children" => [
[
"id" => "1003",
"parent" => "1002",
"niveau" => "4",
],
[
"id" => "1004",
"parent" => "1002",
"niveau" => "4",
],
[
"id" => "1005",
"parent" => "1002",
"niveau" => "4",
]
]
],
[
]
]
],
[
"id" => "1006",
"parent" => "1000",
"level" => "2"
]
],
[
"id" => "1007",
"parent" => "0",
"level" => "1"
]
];
Here's my method :
public function recursData(array $datas, &$str ="", &$row =[], int $level = 0)
{
$level++;
foreach ($datas as $data) {
foreach($data as $key => $d) {
if (is_array($d)) {
$this->recursData($d, $str, $row,$level);
} else {
$str.= "{$level}_{$key}_{$d}|";
if ($key == "parent") {
$row[] = $str;
}
}
}
}
return $row;
}
Output (not what i'm looking for) :
array:8 [
0 => "1_id_1000|1_parent_0|"
1 => "1_id_1000|1_parent_0|2_id_1001|2_parent_1000|"
2 => "1_id_1000|1_parent_0|2_id_1001|2_parent_1000|3_id_1002|3_parent_1001|"
3 => "1_id_1000|1_parent_0|2_id_1001|2_parent_1000|3_id_1002|3_parent_1001|4_id_1003|4_parent_1002|"
4 => "1_id_1000|1_parent_0|2_id_1001|2_parent_1000|3_id_1002|3_parent_1001|4_id_1003|4_parent_1002|4_id_1004|4_parent_1002|"
5 => "1_id_1000|1_parent_0|2_id_1001|2_parent_1000|3_id_1002|3_parent_1001|4_id_1003|4_parent_1002|4_id_1004|4_parent_1002|4_id_1005|4_parent_1002|"
6 => "1_id_1000|1_parent_0|2_id_1001|2_parent_1000|3_id_1002|3_parent_1001|4_id_1003|4_parent_1002|4_id_1004|4_parent_1002|4_id_1005|4_parent_1002|2_id_1006|2_parent_1000|"
7 => "1_id_1000|1_parent_0|2_id_1001|2_parent_1000|3_id_1002|3_parent_1001|4_id_1003|4_parent_1002|4_id_1004|4_parent_1002|4_id_1005|4_parent_1002|2_id_1006|2_parent_1000|1_id_1007|1_parent_0|"
]
The output i'm looking for:
array:8 [
0 => "1_id_1000|1_parent_0"
1 => "1_id_1000|1_parent_0|2_id_1001|2_parent_1000"
2 => "1_id_1000|1_parent_0|2_id_1001|2_parent_1000|3_id_1002|3_parent_1001"
3 => "1_id_1000|1_parent_0|2_id_1001|2_parent_1000|3_id_1002|3_parent_1001|4_id_1003|3_parent_1002"
4 => "1_id_1000|1_parent_0|2_id_1001|2_parent_1000|3_id_1002|3_parent_1001|4_id_1004|3_parent_1002"
5 => "1_id_1000|1_parent_0|2_id_1001|2_parent_1000|3_id_1002|3_parent_1001|4_id_1005|3_parent_1002"
6 => "1_id_1000|1_parent_0|2_id_1006|2_parent_1000"
7 => "1_id_1007|1_parent_0"
]
I'm stuck to finish this properly, I tried lot of things, but can't understand what to do obtain what I want to resolve this. Hope someone could help :)
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 10 months ago.
Improve this question
I have array, like this and I want to join him by name:
Please help to decide problem.
$params[] =
[
"name" => "Robert"
"visitCount" => 2
"coef" => "5.50"
],
[
"name" => "Anna"
"visitCount" => 1
"coef" => "3.58"
],
[
"name" => "Joe"
"visitCount" => 1
"coef" => "8.00"
],
[
"name" => "Robert"
"visitCount" => 2
"coef" => "1.50"
]
How I can get concatenation similar name and sum of coef?
Result that I need:
[
"name" => "Robert"
"visitCount" => 2
"coef" => "7.00"
],
[
"name" => "Anna"
"visitCount" => 1
"coef" => "3.58"
],
[
"name" => "Joe"
"visitCount" => 1
"coef" => "8.00"
],
$output = [];
foreach ($params as $obj) {
$output[$obj['name']] = [
'name' => $obj['name'],
'visitCount' => $obj['visitCount'],
'coef' => (isset($output[$obj['name']])) ? $obj['coef'] + $output[$obj['name']]['coef'] : $obj['coef']
];
}
// If you not want an assossiative array
$output = array_values($output);
I want to use PHP's array_values() function solve the problem described here with json_encode and numerical indexes.
I have a PHP array with mixed things, like this (Laravel dump)
array:5 [
"#context" => "https://schema.org"
"#type" => "HowTo"
"name" => "N"
"supply" => array:1 [
1 => array:2 [
"#type" => "HowToSupply"
"name" => "S"
]
]
"step" => array:1 [
0 => array:5 [
"#type" => "HowToStep"
"name" => null
"url" => null
"text" => "D"
"image" => array:4 [
"#type" => "ImageObject"
"url" => null
"width" => null
"height" => null
]
]
]
]
When I use array_values(), it outputs this:
array:5 [
0 => "https://schema.org"
1 => "HowTo"
2 => "N"
3 => array:1 [
1 => array:2 [
"#type" => "HowToSupply"
"name" => "S"
]
]
4 => array:1 [
0 => array:5 [
"#type" => "HowToStep"
"name" => null
"url" => null
"text" => "D"
"image" => array:4 [
"#type" => "ImageObject"
"url" => null
"width" => null
"height" => null
]
]
]
]
I want to keep the items that already have keys and want to use array_values on those with numerical keys.
How could I do this?
Many thanks in advance!
PS: my real problem is that the json_encode() of the first array outputs the numerical keys, sth I don't want. People on this site suggested to use array_values() to fix this, but that gives the above problem. If you can help me with the original problem, that's of course also great 🙏
The answer lay in the fact that the array keys of my first array started with 1 rather than 0. Fixing that in the code that produced this initial array helped with it.
In my program i have arrays in the following format . I need to find whether a value is present in an the array
return [
"affiliates" => [
"name" => 'Affiliates',
"value" => 11
],
"business" => [
"name" => 'Business',
"value" => 12
],
"inquiries" => [
"name" => 'Inquiries',
"value" => 13
],
"students" => [
"name" => 'Students',
"value" => 14
],
"teachers" => [
"name" => 'Teachers',
"value" => 15
],
"Personal" => [
"name" => 'Personal',
"value" => 3
],
];
I am doing the following. I am expecting that the search will find the value(12) which is a business, but It is returning false.
$searchcategoryid = '12';
$key = array_search($searchcategoryid, array_column(config('peopletypes.students'), 'value'));
Please note that config('peopletypes.students')will return the array above mentioned. I am using laravel.
You have some sort of configuration problem, this code prints 1.
<?php
$searchcategoryid = '12';
$key = array_search($searchcategoryid, array_column([
"affiliates" => [
"name" => 'Affiliates',
"value" => 11
],
"business" => [
"name" => 'Business',
"value" => 12
],
"inquiries" => [
"name" => 'Inquiries',
"value" => 13
],
"students" => [
"name" => 'Students',
"value" => 14
],
"teachers" => [
"name" => 'Teachers',
"value" => 15
],
"Personal" => [
"name" => 'Personal',
"value" => 3
],
], 'value'));
echo $key;
Try to make sure that your config function actually returns the array you claim it to.