show array child dependent of the another array child member - php

i have an array, and i want to show the array values if the name of same array repeat in another array and have true as value
my arrays like this
$array1 = [
array(
'id' => 1
'name' => internal_evidence
'price' => 30
'course_id' => 3
),
array(
'id' => 2
'name' => international_evidence
'price' => 450
'course_id' => 3
),
array(
'id' => 3
'name' => internal_evidence
'price' => 10
'course_id' => 1
),
array(
'id' => 4
'name' => technical_evidence
'price' => 134
'course_id' => 3
),
];
$array2 = [
array(
'id' => 3
'name' => graphic
'price' => 150
'attr' => array(
'internal_evidence' => 'true',
'international_evidence' => 'false',
'technical_evidence' => 'true'
)
),
array(
'id' => 5
'name' => 3dmax
'price' => 300
'attr' => array(
)
),
array(
'id' => 1
'name' => ICDL
'price' => 480
'attr' => array(
'internal_evidence' => 'true',
)
),
];
I want to access and show all attr members with value true in array2
Also, the course_id from array1 must be the same as id in array2
Like this:
<p>internal_evidence with id=1 and price=30 </p>
<p>technical_evidence with id=4 and price=134 </p>
<p>internal_evidence with id=3 and price=10 </p>
i tried this code but its don`t work properly
$array1ByIdAndName = array_reduce($array1, static function ($byIdAndName, $entry) {
$byIdAndName[$entry['product_id']][$entry['e_name']] = $entry;
return $byIdAndName;
});
$array2 = array_map(static function ($entry) use ($array1ByIdAndName) {
$entry['total_price'] = $entry['price'];
$entry['attr'] = array_reduce(array_keys($entry['attr']), static function ($attrs, $attrName) use ($array1ByIdAndName, &$entry) {
if ($entry['attr'][$attrName] === 'true') {
$attrs[] = $attrName;
$entry['total_price'] += $array1ByIdAndName[$entry['id']][$attrName]['price'];
}
return $attrs;
}, []);
return $entry;
}, $array2);
you should know to i dont want to show by id in array1 and array2. i want to show by course_id in array1 in array2 with true value in attr member in array2

you can just use this code
$array1ByIdAndName = array_reduce($array1, static function ($byIdAndName, $entry) {
$byIdAndName[$entry['course_id']][$entry['name']] = $entry;
return $byIdAndName;
});
$array2 = array_map(static function ($entry) use ($array1ByIdAndName) {
$entry['total_price'] = $entry['price'];
$entry['attr'] = array_reduce(array_keys($entry['attr']), static function ($attrs, $attrName) use ($array1ByIdAndName, &$entry) {
if ($entry['attr'][$attrName] === 'true') {
$attrs[] = $attrName;
$entry['total_price'] += $array1ByIdAndName[$entry['id']][$attrName]['price'];
$entry[$attrName] = $array1ByIdAndName[$entry['id']][$attrName]['price'];
}
return $attrs;
}, []);
return $entry;
}, $array2);
echo '<pre>';
print_r($array2);

Related

PHP: Remove duplicate elements and get the latest data in the array

$arr = [
[
"id" => '6230061c0e88d709ca0d7bbc',
'name' => 'Mobile SamSung',
'slug' => 'mobile-samsung',
'createdAt' => '1648006346'
],
[
"id" => '5d1eff529a426778d4b92383',
'name' => 'Mobile Iphone',
'slug' => 'mobile-iphone',
'createdAt' => '1647314181'
],
[
"id" => '5d1eff6b9a426778d4b92dc4',
'name' => 'Mobile SamSung',
'slug' => 'mobile-samsung',
'createdAt' => '1647314460'
],
[
"id" => '5f894011266aea580b028cb0',
'name' => 'Mobile LG',
'slug' => 'mobile-lg',
'createdAt' => '1647314456'
]
];
I have an array, and in this array there are many duplicate subarrays, now I want to remove the duplicate arrays inside, keeping only the data with the latest createdAt. Please give me your opinion. Thanks
I would like to get an array like this:
$arr = [
[
"id" => '6230061c0e88d709ca0d7bbc',
'name' => 'Mobile SamSung',
'slug' => 'mobile-samsung',
'createdAt' => '1648006346'
],
[
"id" => '5d1eff529a426778d4b92383',
'name' => 'Mobile Iphone',
'slug' => 'mobile-iphone',
'createdAt' => '1647314181'
],
[
"id" => '5f894011266aea580b028cb0',
'name' => 'Mobile LG',
'slug' => 'mobile-lg',
'createdAt' => '1647314456'
]
];
You should not make more than one pass over your data. Just use the name values as temporary keys, then only retain a duplicate row's data if its createAt value is greater than what is stored. Re-index the array when you are finished looping.
Code: (Demo)
$result = [];
foreach ($arr as $row) {
if (!isset($result[$row['name']]) || (int)$row['createdAt'] > (int)$result[$row['name']]['createdAt']) {
$result[$row['name']] = $row;
}
}
var_export(array_values($result));
Output:
array (
0 =>
array (
'id' => '6230061c0e88d709ca0d7bbc',
'name' => 'Mobile SamSung',
'slug' => 'mobile-samsung',
'createdAt' => '1648006346',
),
1 =>
array (
'id' => '5d1eff529a426778d4b92383',
'name' => 'Mobile Iphone',
'slug' => 'mobile-iphone',
'createdAt' => '1647314181',
),
2 =>
array (
'id' => '5f894011266aea580b028cb0',
'name' => 'Mobile LG',
'slug' => 'mobile-lg',
'createdAt' => '1647314456',
),
)
Potentially helpful:
Laravel - fetch unique rows from table having highest value in x column
Remove duplicate objects from array based on one value, keep lowest of other value in PHP?
Filter rows with unique column value and prioritize rows with a particular value in another column
How to get max amount of value in same key in array
Explanation:
In this solution, I have gotten the data with a unique slug key with the latest createdAt key. we can have any unique key that matches into the multidimensional array and get the result whatever we want.
Code:
$newArray = [];
foreach ($array as $key => $value) {
$findIndex = array_search($value['slug'], array_column($newArray, 'slug'));
if ($findIndex === false) {
$newArray[] = $value;
} elseif ($findIndex !== false && $newArray[$findIndex]['createdAt'] <= $value['createdAt']) {
$newArray[$findIndex] = $value;
}
}
print_r($newArray);
Demo Link (With your Data): https://3v4l.org/f4kRM
Demo Link (Customized Data with my way): https://3v4l.org/sj4MW
First sort on created at, then remove duplicates.
<?php
$arr = [
[
"id" => '6230061c0e88d709ca0d7bbc',
'name' => 'Mobile SamSung',
'slug' => 'mobile-samsung',
'createdAt' => '1648006346'
],
[
"id" => '5d1eff529a426778d4b92383',
'name' => 'Mobile Iphone',
'slug' => 'mobile-iphone',
'createdAt' => '1647314181'
],
[
"id" => '5d1eff6b9a426778d4b92dc4',
'name' => 'Mobile SamSung',
'slug' => 'mobile-samsung',
'createdAt' => '1647314460'
],
[
"id" => '5f894011266aea580b028cb0',
'name' => 'Mobile LG',
'slug' => 'mobile-lg',
'createdAt' => '1647314456'
]
];
function sort_objects_by_created($a, $b) {
if($a['createdAt'] == $b['createdAt']){ return 0 ; }
return ($a['createdAt'] > $b['createdAt']) ? -1 : 1;
}
// Let's sort
usort($arr, 'sort_objects_by_created');
$slugs = [];
$result = [];
// Loop object
foreach($arr as $phone) {
// If slug is not found, add to result
if (!in_array($phone['slug'], $slugs)){
$slugs[] = $phone['slug'];
$result[] = $phone;
}
}
var_dump($result,$slugs);
Might be worth a note, that you might be able to improve this upstream when creating your array. (always look upstream!)
If you can give you base array a key of created At you can use Array sorting, which which will this step more effecient....
E.g.
$arr = [];
$arr[2022-01-01] = Array('id' => 123, 'name' = 'abc');
$arr[2022-04-01] = Array('id' => 123, 'name' = 'abc');
$arr[2022-08-01] = Array('id' => 123, 'name' = 'abc');

Sorting array in PHP based on another array indexes

I have two arrays
// array of objects
$records = array(
[0] => (object) [
'id' => 1, // (*)
....
],
[1] => (object) [
'id' => 2, // (*)
....
],
[2] => (object) [
'id' => 3, // (*)
....
],
);
// array 2
// the keys in this array refer to the object ids (*)
$sorted = array(
'2' => 7,
'3' => 4,
'1' => 2,
);
$new_records = array();
What I want to do is to sort the values of first array (i.e the objects) based on the order of the key index of the second array, so the end result in this case will become:
$new_records = array(
[0] => (object) [
'id' => 2,
....
],
[1] => (object) [
'id' => 3,
....
],
[2] => (object) [
'id' => 1,
....
],
);
$records = $new_records;
Try this
$new_records = array();
foreach( $sort as $id => $pos ) {
foreach( $records as $record ) {
if( $record[ 'id' ] == $id ) {
$new_records[] = $record;
break;
}
}
}
This make the job
$records = array(
0 => (object) [
'id' => 1, // (*)
],
1 => (object) [
'id' => 2, // (*)
],
2 => (object) [
'id' => 3, // (*)
],
);
// array 2
// the keys in this array refer to the object ids (*)
$sorted = array(
'2' => 7,
'3' => 4,
'1' => 2,
);
$keySorted = array_keys($sorted);
usort($records, function ($a, $b) use ($keySorted) {
$pos_a = array_search($a->id, $keySorted);
$pos_b = array_search($b->id, $keySorted);
return $pos_a - $pos_b;
});
var_dump($records);
Here, I have created sorting of array and add to your array of object using current() and key() is from php.
$i = 0;
while ($value = current($sorted)) {
$key = key($sorted); // get key from array 2
$records[i]->array('id' => $key); // set key to array of object
next($array);
$i++;
}
I hope this may help you.
Code: (Demo)
$records = [(object)['id' => 1], (object)['id' => 2], (object)['id' => 3]];
$sorted = ['2' => 7, '3' => 4, '1' => 2];
$tempArr = array();
foreach ($records as $value) {
$tempArr[$value->id] = $value;
}
$resultArr = array_values(array_replace($sorted, $tempArr));
var_export($resultArr);
Output:
array (
0 =>
stdClass::__set_state(array(
'id' => 2,
)),
1 =>
stdClass::__set_state(array(
'id' => 3,
)),
2 =>
stdClass::__set_state(array(
'id' => 1,
)),
)

How to recursively remap array in PHP?

How to recursively remap children to nodes? I tried writing recursive function, but it only runs one iteration. Array map - http://php.net/manual/en/function.array-map.php would only run for single dimension
I'm using nested sets model https://github.com/etrepat/baum#getting-started Input is generated by dumping all hierarchy
$array = Category::where('name', '=', 'All')->first()->getDescendantsAndSelf()->toHierarchy()->toArray();
/* Input */
$array = array(
'category_id' => 0,
'children' => array(
array(
'category_id' => 1,
'children' => array(
'category_id' => 2,
'children' => array(
'category_id' => 3,
)
)
),
array(
'category_id' => 4,
'children' => array(
'category_id' => 5,
'children' => array(
'category_id' => 6,
)
)
)
)
);
Output should be
/*
$array = array(
'category_id' => 0,
'nodes' => array(
array(
'category_id' => 1,
'nodes' => array(
'category_id' => 2,
'nodes' => array(
'category_id' => 3,
)
)
),
array(
'category_id' => 4,
'nodes' => array(
'category_id' => 5,
'nodes' => array(
'category_id' => 6,
)
)
)
)
);*/
function remap($items){
if(!empty($items['children'])){
$items['nodes'] = $items['children'];
unset($items['children']);
return remap($items['nodes']);
}
else{
return $items;
}
}
print_r(remap($array));
A bit complicated couse , childrens content variates by depth
function remap(array &$items){
if(array_key_exists('children',$items)){
$items['nodes'] = $items['children'];
unset($items['children']);
if(array_key_exists('children',$items['nodes'])){
$items['nodes']['nodes'] = $items['nodes']['children'];
unset($items['nodes']['children']);
}
foreach ($items['nodes'] as &$x) {
if (is_array($x)) {
remap($x);
}
}
}
else{
return $items;
}
}
remap($array);
print_r($array);
This function will change the children keys to nodes.
function remap($items) {
$result = array();
foreach ($items as $key => $value) {
if ($key == 'children') {
$result['nodes'] = remap($value);
} else {
$result[$key] = $value;
}
}
return $result;
}
$new_array = remap($array);
After your comment, though, it seems like it would be easier to create an accessor that maps children to nodes in your Eloquent model and use that in the select that gets your collection.

Recursive get path from multidimensional php array

I have array:
$adm_menu_old = array (
array(
'id' => 1,
'name' => 'Test1',
),
array(
'id' => 3,
'name' => 'Test3',
'childrens' => array(
array(
'id' => 31,
'name' => 'Test31',
),
array(
'id' => 32,
'name' => 'Test32',
'childrens' => array(
array(
'id' => 321,
'name' => 'Test321',
),
),
)
),
array(
'id' => 4,
'name' => 'Test4',
),
);
Say i know id value.
I need get path with all parents of this id.
For example i need get path to this element: id=321
i need get array with key name values:
array('Test3','Test32','Test321')
how should look like recursive function?
Try this function:
function getNames($id, $arr) {
$result = array();
foreach($arr as $key => $val) {
if(is_array($val)) {
if($val["id"] == $id) {
$result[] = $val["name"];
} elseif(!empty($val["childrens"]) && is_array($val["childrens"])) {
$sub_res = getNames($id, $val["childrens"]);
if(count($sub_res) > 0) {
$result[] = $val["name"];
$result = array_merge($result, $sub_res);
}
}
}
}
return $result;
}

php compare two multidimensional array and check for duplicate

I have two arrays
$array1 = array(
0 => array(
'user' => 'user0',
'id' => 'id0'
),
1 => array(
'user' => 'user1',
'id' => 'id1'
),
2 => array(
'user' => 'user2',
'id' => 'id2'
)
);
$array2 = array(
0 => array(
'emp' => 'emp0',
'id' => 'id3'
),
1 => array(
'emp' => 'emp1',
'id' => 'id1'
),
2 => array(
'emp' => 'emp2',
'id' => 'id2'
)
);
i need to loop array 2 first an d give input of id from array1 to the array 1 and search whether the value of id1 from arr1 exists in array2
Maybe this could work? (If I understood your question correctly)
$id_arr = array();
$final_arr = array();
checkArray($array1, $id_arr, $final_arr);
checkArray($array2, $id_arr, $final_arr);
function checkArray($arr, &$id_arr, &$final_arr) {
foreach ($arr as $key => $value) {
if (!in_array($value['id'], $id_arr)) {
$id_arr[] = $value['id'];
$final_arr[] = $value;
}
}
}
var_dump($final_arr);

Categories