Add values from multidimensional array to another array - php

Hello good morning wherever you are :D, i have a lil problem, i have this code of arrays
$arrayToView is the info of every single user that i want.
$tagsArray are only tags that use every user but i need to merge all the info something like the last array...
$arrayToView = array(
'IVOFACUNDO' = array(
'mails' => 3,
'contacts' => 34,
'blocked' => 23
),
'ESRAYCU' = array(
'mails' => 23,
'contacts' => 124,
'blocked' => 44
)
)
And i have another one like this
$tagsArray= array(
'IVOFACUNDO' = array(
'14' => array(
'id' => 14,
'name' => 'php',
'value' => 1
),
'15' => array(
'id' => 15,
'name' => 'javascript',
'value' => 1
)
),
'ESRAYCU' = array(
'1' => array(
'id' => 1,
'name' => 'python',
'value' => 1
),
'15'=> array(
'id' => 15,
'name' => 'javascript',
'value' => 1
)
)
)
so the question is how i can merge both arrays obviously respectively with the same admin something like this
$arrayToView = array(
'IVOFACUNDO' = array(
'mails' => 3,
'contacts' => 34,
'blocked' => 23,
'tags' => array(
'14' => array(
'id' => 14,
'name' => 'php',
'value' => 1
),
'15' => array(
'id' => 15,
'name' => 'javascript',
'value' => 1
)
)
),
'ESRAYCU' = array(
'mails' => 23,
'contacts' => 124,
'blocked' => 44,
'tags' => array(
'1' => array(
'id' => 1,
'name' => 'python',
'value' => 1
),
'15'=> array(
'id' => 15,
'name' => 'javascript',
'value' => 1
)
)
)
)
The key 'tags' need to be created in the merge of every iteration to add and get one array with all the values, how i can do this?

You can try this snippet.
foreach($arrayToView as $key => $arr){
if(array_key_exists($key, $tagsArray)){
$arrayToView[$key]['tags'] = $tagsArray[$key];
}
}
echo '<pre>';print_r($arrayToView);echo '</pre>';

Use php inbuilt function
$result_Arr = array_merge_recursive($arrayToView,$tagsArray);

<?php
$arrayToView = array(
'IVOFACUNDO' => array(
'mails' => 3,
'contacts' => 34,
'blocked' => 23
),
'ESRAYCU' => array(
'mails' => 23,
'contacts' => 124,
'blocked' => 44
)
);
$tagsArray= array(
'IVOFACUNDO' => array(
'14' => array(
'id' => 14,
'name' => 'php',
'value' => 1
),
'15' => array(
'id' => 15,
'name' => 'javascript',
'value' => 1
)
),
'ESRAYCU' => array(
'1' => array(
'id' => 1,
'name' => 'python',
'value' => 1
),
'15'=> array(
'id' => 15,
'name' => 'javascript',
'value' => 1
)
)
);
foreach($arrayToView as $key => $value){
if(isset($tagsArray[$key])){
$arrayToView[$key]['tags'] = array();
foreach($tagsArray[$key] as $key2 => $value2){
$arrayToView[$key]['tags'][$key2] = $tagsArray[$key][$key2];
}
}
}
echo'<pre>';
print_r($arrayToView);
echo'</pre>';
?>

Related

Loop Through Nested JSON Response PHP

I'm trying to loop through and return data ('rank' from 'rank_details') from a returned JSON response.
Here is a snippet of the JSON response (what I'm getting from: $array = json_decode($apiResponse); )
(object) array(
'obj' =>
array (
0 =>
(object) array(
'name' => 'I\'m a HellRazor (feat. Crucifix)',
'id' => 13859011,
'data' =>
array (
0 =>
(object) array(
'timestp' => '2019-10-27T00:00:00.000Z',
'score' => 1.9610844011276853,
'rank_details' =>
array (
0 =>
(object) array(
'rank' => 191,
'country' => 'RU',
'score' => 1.9610844011276853,
'genre' => 'Country',
),
),
),
1 =>
(object) array(
'timestp' => '2019-12-04T00:00:00.000Z',
'score' => 14.70808550760029,
'rank_details' =>
array (
0 =>
(object) array(
'rank' => 9,
'country' => 'CH',
'score' => 14.70808550760029,
'genre' => 'Country',
),
),
),
2 =>
(object) array(
'timestp' => '2020-03-18T00:00:00.000Z',
'score' => 13.299189761918104,
'rank_details' =>
array (
0 =>
(object) array(
'rank' => 5,
'country' => 'RU',
'score' => 13.299189761918104,
'genre' => 'Country',
),
),
),
3 =>
(object) array(
'timestp' => '2020-07-12T00:00:00.000Z',
'score' => 19.02841337415393,
'rank_details' =>
array (
0 =>
(object) array(
'rank' => 77,
'country' => 'DE',
'score' => 19.02841337415393,
'genre' => 'Country',
),
),
),
4 =>
(object) array(
'timestp' => '2020-10-02T00:00:00.000Z',
'score' => 2.631257456412845,
'rank_details' =>
array (
0 =>
(object) array(
'rank' => 154,
'country' => 'RU',
'score' => 2.631257456412845,
'genre' => 'Country',
),
),
),
5 =>
(object) array(
'timestp' => '2020-10-03T00:00:00.000Z',
'score' => 1.896575572629275,
'rank_details' =>
array (
0 =>
(object) array(
'rank' => 195,
'country' => 'RU',
'score' => 1.896575572629275,
'genre' => 'Country',
),
),
),
),
),.....
Here is a snippet of my code:
$apiResponse = curl_exec($cc);
$array = json_decode($apiResponse);
foreach ($array as $key => $arrays) { // This will search in the 2 jsons
foreach($arrays as $key => $value) {
echo "\n Record ID: " . $value->id;
echo "\n Record Name: " . $value->name;
echo "\n Record Rank: " . $value->obj->data->rank_details->rank;
echo "\n";
}
}
Record Name and ID come over fine, but anything not in the "top level" isn't coming over. Any help is GREATLY appreciated.
You have to index into the data and rank_details arrays even if there's only one entry.
This worked for me:
echo "\n Record Rank: " . $value->data[0]->rank_details[0]->rank;

Sum Integer with String and Merge With Same String - PHP

i have a problem with this array, i need get sum form string merge with same key
$data = array(
0 => array(
'name' => 'Alfa Edison, Dwiki',
'budget' => 3700,
),
1 => array(
'name' => 'Maverick Sam',
'budget' => 500,
),
2 => array(
'name' => 'Dwiki',
'budget' => 1000,
),
3 => array(
'name' => 'Steve, Dwiki',
'budget' => 2000,
),
4 => array(
'name' => 'Alfa Edison',
'budget' => 700,
),
5 => array(
'name' => 'Maverick Sam',
'budget' => 4000,
),
6 => array(
'name' => 'Steve, Alfa Edison',
'budget' => 334,
),
);
i want the result this:
array(
0 => array(
'name' => 'Alfa Edison',
'budget' => 4734,
),
1 => array(
'name' => 'Dwiki',
'budget' => 6700,
),
2 => array(
'name' => 'Maverick Sam',
'budget' => 4500,
),
3 => array(
'name' => 'Steve',
'budget' => 2334,
),
);
How to merge String with same Key and Sum the Budget. i try to for each but i'm fail.
i try array_reduce and explode the name but fail.
The problem is that each of the "keys" (names) is really more than one key. So as you iterate the input array you'll need to split those up, then add an inner loop to use the names as keys in the result.
foreach ($data as $item) {
// separate the names
$names = explode(', ', $item['name']);
// iterate the names and set/increase values for them in the result array
foreach ($names as $name) {
$result[$name]['name'] = $name;
$result[$name]['budget'] = $item['budget'] + ($result[$name]['budget'] ?? 0);
}
}
// remove the string keys if necessary
$result = array_values($result);

Check if key exist in array if so, count it and create a new array php

From my SQL query, I will get the following array as a result. This is the last result I can get from SQL.I cant change any SQL because of some constraint.
I need to check if the same id exists or not and if it does need to count them and remove one of the duplicate array having the same id.
Sample array is
$array = array(
0 => array(
'id' => '17',
'status' => 1,
),
1 => array(
'id' => '18',
'status' => 1,
),
2 => array(
'id' => '21',
'status' => 1,
),
3 => array(
'id' => '5',
'status' => 2,
),
4 => array(
'id' => '18',
'status' => 1,
),
5 => array(
'id' => '22',
'status' => 5,
),
6 => array(
'id' => '6',
'status' => 1,
),
);
I need to check if they have a duplicate id or not, if yes need to count them and remove one of the duplicates.We need to preserve the array structure.
End Results should be
array(
0 => array(
'id' => '17',
'status' => 1,
'count'=1,
),
1 => array(
'id' => '18',
'status' => 1,
'count'=2,
),
2 => array(
'id' => '21',
'status' => 1,
'count'=1,
),
3 => array(
'id' => '5',
'status' => 2,
'count'=1,
),
4 => array(
'id' => '22',
'status' => 5,
'count'=1,
),
5 => array(
'id' => '6',
'status' => 1,
'count'==>1,
),
)
You can Check it by this way but duplicate entry still in the array.
$ids = array();
foreach ($array as $key => $value)
{
$ids[] = $value['id'];
$count = array_count_values($ids);
}
for($i = 0; $i < count($array);$i++)
{
$array[$i]['count'] = $count[$array[$i]['id']];
}
You can do this with the array_unique function.
Use it like so:
$a=array("a"=>"red","b"=>"green","c"=>"red");
print_r(array_unique($a));
Which outputs:
Array ( [a] => red [b] => green )

php reformat nested array

I have a stored unlimited nested levels array with unwanted extra data.
$arr = array(
array(
'name' => 'item1',
'level' => 0,
'extra_key' => 'some_data',
'children' => array(
'name' => 'sub-item1',
'level' => 1,
'extra_key' => 'some_data',
'children' => array(
'name' => 'sub-sub-item1',
'level' => 2,
'extra_key' => 'some_data',
'children' => array()
),
array(
'name' => 'sub-sub2-item1',
'level' => 2,
'extra_key' => 'some_data',
'children' => array()
),
)
),
array(
'name' => 'item2',
'level' => 0,
'extra_key' => 'some_data',
'children' => array(
'name' => 'sub-item2',
'level' => 2,
'extra_key' => 'some_data',
'children' => array(
'name' => 'sub-sub-item2',
'level' => 2,
'extra_key' => 'some_data',
'children' => array()
),
array(
'name' => 'sub-sub2-item2',
'level' => 2,
'extra_key' => 'some_data',
'children' => array()
),
)
),
array(
'name' => 'item3',
'level' => 0,
'extra_key' => 'some_data',
'children' => array(
'name' => 'sub-item3',
'level' => 1,
'extra_key' => 'some_data',
'children' => array(
'name' => 'sub-sub-item3',
'level' => 2,
'extra_key' => 'some_data',
'children' => array()
),
array(
'name' => 'sub-sub2-item3',
'level' => 2,
'extra_key' => 'some_data',
'children' => array()
),
)
),
);
Expected output array:
$arr = array(
array(
'name' => 'item1',
'nodes' => array(
'name' => 'sub-item1',
'nodes' => array(
'name' => 'sub-sub-item1',
'nodes' => array()
),
array(
'name' => 'sub-sub2-item1',
'nodes' => array()
),
)
),
array(
'name' => 'item2',
'nodes' => array(
'name' => 'sub-item2',
'nodes' => array(
'name' => 'sub-sub-item2',
'nodes' => array()
),
array(
'name' => 'sub-sub2-item2',
'nodes' => array()
),
)
),
array(
'name' => 'item3',
'nodes' => array(
'name' => 'sub-item3',
'nodes' => array(
'name' => 'sub-sub-item3',
'nodes' => array()
),
array(
'name' => 'sub-sub2-item3',
'nodes' => array()
),
)
)
);
I want to remove unwanted keys like level , extra_key from all levels and i want also to change the name of the key children to nodes then reproduce the same array with the same structure with the new format.
How can i achieve that?
I tried to do it by recursive function but i failed to reproduce the same structrue
Your structure doesn't make sense, probably because of that you was unable to write recursive function. If it is possible to change structure, I would suggest this one (with reformat function implementation):
<?php
$actual = array(
array(
'name' => 'item1',
'level' => 0,
'extra_key' => 'some_data',
'children' => array(
array(
'name' => 'sub-item1',
'level' => 1,
'extra_key' => 'some_data',
'children' => array(
array(
'name' => 'sub-sub-item1',
'level' => 2,
'extra_key' => 'some_data',
'children' => array()
),
array(
'name' => 'sub-sub2-item1',
'level' => 2,
'extra_key' => 'some_data',
'children' => array()
),
)
),
)
),
);
$expected = array(
array(
'name' => 'item1',
'nodes' => array(
array(
'name' => 'sub-item1',
'nodes' => array(
array(
'name' => 'sub-sub-item1',
'nodes' => array()
),
array(
'name' => 'sub-sub2-item1',
'nodes' => array()
),
),
),
)
),
);
function change_array($original)
{
return array_map('change_node', $original);
}
function change_node($node)
{
return [
'name' => $node['name'],
'nodes' => array_map('change_node', $node['children']),
];
}
var_dump($expected === change_array($actual));
This function will do what you want recursively.
You can add extra excluded and/or renamed indices if required;
function parseArray($array): array
{
// The resulting array
$result = [];
// Indices you want renamed [`from` => `to`]
$renameIndices = ['children' => 'nodes'];
// Indices you want excluded [`1`, `2`]
$excludedIndices = ['level', 'extra_key'];
foreach ($array as $idx => $content)
{
// If excluded, continue (skip) node.
if (in_array($idx, $excludedIndices, true))
{
continue;
}
// Setting the resulting (new) index.
$resultIdx = $idx;
// Check if this index should be renamed
if (array_key_exists($idx, $renameIndices))
{
// If index should be renamed, apply new name
$resultIdx = $renameIndices[$idx];
}
// If this content block is an array. Parse it.
if (is_array($content))
{
$content = parseArray($content);
}
// Save content to resulting array.
$result[$resultIdx] = $content;
}
return $result;
}

Recursion PHP function returns 500 error

Hi I'm have recursion function that makes binary tree array, so from that
array(
array(
'name' => 'John',
'id' => 1,
'mother_id' => 2,
'father_id' => 3
),
array(
'name' => 'Lucy',
'id' => 2,
'mother_id' => 5,
'father_id' => 4
),
array(
'name' => 'Jim',
'id' => 3,
'mother_id' => 7,
'father_id' => 9
),
array(
'name' => 'Paul',
'id' => 4,
'mother_id' => 534,
'father_id' => 54
),
array(
'name' => 'Laura',
'id' => 5,
'mother_id' => 554,
'father_id' => 51
),
array(
'name' => 'Vanessa',
'id' => 7,
'mother_id' => 5354,
'father_id' => 514
),
array(
'name' => 'Adam',
'id' => 9,
'mother_id' => 245354,
'father_id' => 514234
),
);
I'm getting that:
array(
array(
'person' => array(
'name' => 'John',
'id' => 1,
'mother_id' => 2,
'father_id' => 3
),
'parents' => array(
'mother' => array(
'person' => array(
'name' => 'Lucy',
'id' => 2,
'mother_id' => 5,
'father_id' => 4
),
'parents' => array(
'mother' => array(
'person' => array(
'name' => 'Laura',
'id' => 5,
'mother_id' => 554,
'father_id' => 51
),
'parents' => array(...)
),
'father' => array(
'person' => array(
'name' => 'Paul',
'id' => 4,
'mother_id' => 534,
'father_id' => 54
),
'parents' => array(...)
),
)
),
'father' => ...
)
and it works completely fine until it gets array with more than -+100 values, and when it gets this array, it returns 500 error, that's seems strange for me, cos obviously I don't have infinite loop or something like that, cos smallest arrays parses fine but I can't figure out what's reason for this kind of behavior.
function parseTree(&$tree, $root = null)
{
$return = null;
foreach ($tree as $key => $item) {
if ($root == null || $item['id'] == $root) {
$return = [
'person' => $item,
'parents' => [
'father' => parseTree($tree, $item['father_id']),
'mother' => parseTree($tree, $item['mother_id'])
]
];
unset ($tree[$key]);
}
}
return $return;
}

Categories