<?php
$userData = [
[
"UID" => "5f10482574d83d4b726fe5",
"name" => "Yug Gill",
"orgID" => "5f10481d74d83d4b726",
"imageURL" => "female.png"
]
];
$userProductsData = [
[
"UPID" => "5f10482574d83d4b6fe007",
"UID" => "5f10482574d83d4b726fe5",
]
];
$userDetailsResult = [];
foreach ($userData as $key => $value) {
$userData[$key]["UPID"] = $userProductsData[$value["UID"]] ?? [];
}
Expected Output
$userData = [
[
"UID" => "5f10482574d83d4b726fe5",
"name" => "Yug Gill",
"orgID" => "5f10481d74d83d4b726",
"imageURL" => "female.png",
"UPID" => "5f10482574d83d4b6fe007"
]
];
i have two aray UID common for both array, now i want to take UPID from $userProductsData and push into $userData, i have tried not working properly, kindly anyone update my code please
?>
Try this one.
$userData = [
[
"UID" => "5f10482574d83d4b726fe5",
"name" => "Yug Gill",
"orgID" => "5f10481d74d83d4b726",
"imageURL" => "female.png"
]
];
$userProductsData = [
[
"UPID" => "5f10482574d83d4b6fe007",
"UID" => "5f10482574d83d4b726fe5",
]
];
$userDetailsResult = [];
foreach ($userProductsData as $key => $value) {
$userData[$key]["UPID"] = $value['UPID'];
}
print_r($userData);
Your $userProductsData is a regular array, it has indexes 0, 1, 2 etc... Then you are trying to get an item from this array by string key "5f10482574d83d4b726fe5".
$userProductsData should be a key array like that:
$userProductsData = [
"5f10482574d83d4b726fe5" => [
"UPID" => "5f10482574d83d4b6fe007",
"UID" => "5f10482574d83d4b726fe5",
],
];
Then you can get an item from this array by key "5f10482574d83d4b726fe5".
Related
I have a function which gives an array of the below format
$result = [
[
"name" => "text",
"id" => "928610",
"entity_type" => "node"
],
[
"name" => "folder",
"id" => "987620",
"entity_type" => "folder"
],
[
"name" => "text",
"id" => "956720",
"entity_type" => "node"
],
];
Each Folder "entity_type" => "folder" item has again child which returns same format array.
like if we run a foreach loop $result and if it is "entity_type" => "folder" then we pass the id to a function it will also give a similar array format as that of result.
So i need if it is "entity_type" => "folder" the below key added to the "entity_type" => "folder" item
"children" => [
'#theme' => 'child_elements',
'#child_elements' => [
[
'name' => 'text',
"id" => "333421",
"entity_type" => "node"
],
[
'name' => 'folder',
"id" => "897622",
"entity_type" => "folder"
],
[
'name' => 'text',
"id" => "342214",
"entity_type" => "node"
],
],
],
and recursively it should keep on adding if "entity_type" => "folder"
The final array should be
$result = [
[
"name" => "text",
"id" => "928610",
"entity_type" => "node"
],
[
"name" => "folder",
"id" => "987620",
"entity_type" => "folder"
"children" => [
'#theme' => 'child_elements',
'#child_elements' => [
[
'name' => 'text',
"id" => "333421",
"entity_type" => "node"
],
[
'name' => 'folder',
"id" => "897622",
"entity_type" => "folder"
],
[
'name' => 'text',
"id" => "342214",
"entity_type" => "node"
],
],
],
],
[
"name" => "text",
"id" => "956720",
"entity_type" => "node"
],
];
public function buildTree($elements) {
$branch = [];
$branch = ['#theme' => 'child_elements'];
foreach ($elements as $key => $element) {
foreach($element as $keys => $values){
if ($element['bundle'] == 'folder') {
$child = $this->loadElements($element['id']);
$branch['#child_elements'] = $child;
$element[$key]['children'] = $branch['#child_elements'];
$this->buildTree($child);
array_push($branch, $element);
}else{
$branch['#child_elements'][] = $element;
}
}
}
return $branch;
}
you need to use some logic statement and use array_push when the condition is met.
Is it possible to stop the loop from overwriting the previous value?
<?php
foreach (array('email1', 'email2') as $line) {
$x = [
$line => [
"Reports" => [
(object) [
"ReportType" => "1",
"SummaryFrequency" => [
(object) [
"FrequencyType" => "8011",
"SecondsPast" => "32400",
],
],
"Filter" => (object) [
"ClauseType" => "or",
"RuleField" => "",
"RuleOperator" => "",
"RuleValue" => "",
"ClauseChildren" => [
(object) [
"ClauseType" => "",
"RuleField" => "BackupJobDetail.TimeSinceStarted",
"RuleOperator" => "int_lte",
"RuleValue" => "86400",
],
],
],
],
],
],
];
}
print_r($x);
https://phpize.online/?phpses=e658de0de3dc1ed5a4d8d27ecebf567a&sqlses=null&php_version=php8&sql_version=mysql57
At the moment you overwrite $x each time, if you want to make it an array with $line as each index, create an empty array and then add the new items in with new index...
$x = [];
foreach (array('email1', 'email2') as $line) {
$x[$line] = [
"Reports" => [
(object) [
I need to take an array that turns the key (split by /) of each element into a child array, and assigns the data in the right format in the new array.
There can be multiple levels of nesting, realistically never more then 10, but that is to be decided.
For example;
given the input of
$i_have_this = [
"Base/child" => [
[
"filename" => "child-1",
"last_modified" => "29/01/2020"
],
[
"filename" => "child-2",
"last_modified" => "29/01/2020"
],
[
"filename" => "child-3",
"last_modified" => "29/01/2020"
]
],
"Base/child/grandChild1" => [
[
"filename" => "grandChild1-1",
"last_modified" => "29/01/2020"
]
],
"Base/child/grandChild2" => [
[
"filename" => "grandChild2-1",
"last_modified" => "29/01/2020"
],
[
"filename" => "grandChild2-2",
"last_modified" => "29/01/2020"
],
[
"filename" => "grandChild2-3",
"last_modified" => "29/01/2020"
],
[
"filename" => "grandChild2-4",
"last_modified" => "29/01/2020"
],
[
"filename" => "grandChild2-5",
"last_modified" => "29/01/2020"
]
]
];
I would like the output of
$want_this = [
'name' => 'Base',
'children' => [
[
'name' => 'child',
'children' => [
["name" => "child-1"],
["name" => "child-2"],
["name" => "child-3"],
[
"name" => "grandChild1",
"children" => [
["name" => "grandChild1-1"]
]
],
[
"name" => "grandChild2",
"children" => [
["name" => "grandChild2-1"],
["name" => "grandChild2-2"],
["name" => "grandChild2-3"],
["name" => "grandChild2-4"]
]
],
]
]
]
];
So far I have;
foreach($i_have_this as $path => $value) {
$temp = &$want_this;
foreach (explode('/', $path) as $key) {
$temp = &$temp[$key];
}
$temp = $value;
}
but can't quite finish it off.
Example code run here
I think you could treat this the same way that most use "dot" notation for arrays (like in Laravel). Just replace "." with "/" in your case:
Example Code
function unflatten($data) {
$output = [];
foreach ($data as $key => $value) {
$parts = explode('/', $key);
$nested = &$output;
while (count($parts) > 1) {
$nested = &$nested[array_shift($parts)];
if (!is_array($nested)) $nested = [];
}
$nested[array_shift($parts)] = $value;
}
return $output;
}
print_r(unflatten($i_have_this));
There are two arrays of arrays. $user and $experience are two arrays and i want to achieve $final as my expected output.
I have two arrays:
$user = [
[
"country" => "aus",
"new_experiences" => 0,
"new_users" => 6
],
[
"country" => "jpn",
"new_experiences" => 0,
"new_users" => 5
]
];
$experience = [
[
"country" => "jpn",
"new_experiences" => 12,
"new_users" => 0
],
[
"country" => "usa",
"new_experiences" => 10,
"new_users" => 0
]
];
After merging these two arrays based on country, how can i get:
$final = [
[
"country" => "aus",
"new_experiences" => 0,
"new_users" => 6
],
[
"country" => "jpn",
"new_experiences" => 12,
"new_users" => 5
],
[
"country" => "usa",
"new_experiences" => 10,
"new_users" => 0
]
];
Try this
array_unique(array_merge($array1,$array2), SORT_REGULAR);
http://se2.php.net/manual/en/function.array-unique.php
You can achieve your GOal using this simple steps:
Iterate and arrange your array into a single array using foreach
Create a key-value array, just like I have created $issetArray
Push all the value in a $final array, you will get the desired output
I have created a simple logic using foreach loop, isset() and array_push()
$user = array(
array(
"country" => "aus",
"new_experiences" => 0,
"new_users" => 6
),
array(
"country" => "jpn",
"new_experiences" => 0,
"new_users" => 5
)
);
$experience = array(
array(
"country" => "jpn",
"new_experiences" => 12,
"new_users" => 0
),
array(
"country" => "usa",
"new_experiences" => 10,
"new_users" => 0
)
);
$final = array();
$issetArray = array();
foreach($user as $key => $value)
{
if(isset($issetArray[$value['country']]))
{
$issetArray[$value['country']]['new_experiences'] = $value;
$issetArray[$value['country']]['new_users'] = $value;
}
else
{
$issetArray[$value['country']] = $value;
}
}
foreach($experience as $key => $value)
{
if(isset($issetArray[$value['country']]))
{
$issetArray[$value['country']]['new_experiences'] = $value['new_experiences'];
$issetArray[$value['country']]['new_users'] = $value['new_users'];
}
else
{
$issetArray[$value['country']] = $value;
}
}
foreach($issetArray as $value)
{
array_push($final, $value);
}
echo "<pre>";
print_r($final);
Click on the link to know more about isset() and array_push()
I have a PHP multi-dimensional array organized with each node listing its parent nodes under it. I'm trying to transform the array so that the output lists hierarchically with each node listing any child nodes and only listing unique paths within the array.
for instance this input array:
$input = [
[
"name" => "home",
"parents" => [],
],
[
"name" => "newslist",
"parents" => [
[
"name" => "home",
"parents" => [],
],
],
],
[
"name" => "newsdetail",
"parents" => [
[
"name" => "newslist",
"parents" => [
[
"name" => "home",
"parents" => [],
],
],
],
[
"name" => "home",
"parents" => [],
],
],
],
[
"name" => "knowledge",
"parents" => [],
],
];
Should output this array:
$output = [
[
"name" => "home",
"children" => [
[
"name" => "newslist",
"children" => [
[
"name" => "newsdetail",
"children" => [],
],
],
],
],
],
[
"name" => "knowledge",
"children" => [],
],
];
This could probably be done in a much nicer way, but this method works. just procedural functions as a proof of concept.
<?php
$input = [
[
"name" => "home",
"parents" => [],
],
[
"name" => "newslist",
"parents" => [
[
"name" => "home",
"parents" => [],
],
],
],
[
"name" => "newsdetail",
"parents" => [
[
"name" => "newslist",
"parents" => [
[
"name" => "home",
"parents" => [],
],
],
],
[
"name" => "home",
"parents" => [],
],
],
],
[
"name" => "knowledge",
"parents" => [],
],
];
//recursively get all parents and the level the parent is at
function getParents($nodes,$level,&$parents)
{
foreach($nodes AS $key => $node)
{
$parents[ $node['name'] ] = array( "name" => $node['name'], "level" => $level);
if(isset($node['parents']) && !empty($node['parents']))
{
$level += 1;
getParents($node['parents'],$level,$parents);
}
}
}
//sort the parents by level
function sortParentsByLevel($a, $b)
{
if ($a['level'] == $b['level']) {
return 0;
}
return ($a['level'] > $b['level']) ? -1 : 1;
}
//find the output path based on parents array to add new value to
function setValueFromPath(&$paths, $parents, $value)
{
$dest = &$paths;
if(empty($parents))
{
if(!isset($dest[$value]))
$dest[$value] = array();
} else {
$finalNode = array_pop($parents);
foreach ($parents as $parent)
{
$dest = &$dest[$parent];
}
$dest[$finalNode][$value] = array();
}
}
//init new variable
$output = array();
//loop through each input node
foreach($input AS $key => $node)
{
//init a parent array
$parents = array();
//if we have parents use the getParents method to set them
if(isset($node['parents']) && is_array($node['parents']) && !empty($node['parents']))
{
getParents($node['parents'],1,$parents);
}
//sort the parents according to their level
uasort($parents, 'sortParentsByLevel');
//we're only interested in the associative key
$parentKeys = array();
foreach($parents AS $parent)
{
$parentKeys[] = $parent['name'];
}
//add the $node['name'] value in the appropriate parent array
setValueFromPath($output, $parentKeys, $node['name'] );
}
echo '<pre>';
print_r($output);
echo '</pre>';
die();
/*
Array
(
[home] => Array
(
[newslist] => Array
(
[newsdetail] => Array
(
)
)
)
[knowledge] => Array
(
)
)
*/