make nested array on the basis of specific array value in php - php

Array
(
[0] => Array
(
[user_id] => 40718
[name] => abc1
)
[1] => Array
(
[user_id] => 40718
[name] => abc2
)
[2] => Array
(
[user_id] => 40719
[name] => abc3
)
)
my array is like having user_id as you can see above i want to convert it into nested array on the basis of specific value from array as user_id like mention below
Array
(
[40718] => Array
(
[0]=>array(
[name] => abc1
)
[1]=>array(
[name] => abc2
)
)
[40719] => Array
(
[0] => (
[name] => abc3
)
)
)

Though I haven't tested it, check if it can help you to get your result :
<?php
$arrTest = [
[
'user_id' => 40718,
'name' => 'abc1'
],
[
'user_id' => 40718,
'name' => 'abc2'
],
[
'user_id' => 40719,
'name' => 'abc3'
]
];
$resultArr = [];
foreach ($arrTest as $val) {
$resultArr[$val['user_id']][]['name'] = $val['name'];
}
echo '<pre>'; print_r($resultArr); exit;
?>

Related

PHP Sort multi-dimension array by keys

I have a multi dimension array like below:
Array
(
[1200] => Array
(
[B] => Array
(
[4] => Array
(
[Name] => 'Joe']
)
)
[A] => Array
(
[3] => Array
(
[Name] => 'Paul']
)
)
)
[1100] => Array
(
[F] => Array
(
[2] => Array
(
[Name] => 'Sam']
)
)
[D] => Array
(
[1] => Array
(
[Name] => 'Jane']
)
)
)
What I wish to achieve is having the 4 digit number 1100 and 1200 in order ascending, then I need the letters (B A) and (F D) also in order, and then the single digit number under them in order ascending too. I believe I'm looking at a multi dimension array but any help would be appreciated.
The below function might be what you're looking for. It recursively orders arrays by their key.
function ksort_r(&$array) {
foreach ($array as &$value) {
if (is_array($value)) {
ksort_r($value);
}
}
return ksort($array);
}
Example usage
function ksort_r(&$array) {
foreach ($array as &$value) {
if (is_array($value)) {
ksort_r($value);
}
}
return ksort($array);
}
$data = [
1200 => [
'B' => [
4 => [
'Name' => 'Joe'
]
],
'A' => [
3 => [
'Name' => 'Paul'
]
]
],
1100 => [
'F' => [
2 => [
'Name' => 'Sam'
]
],
'D' => [
1 => [
'Name' => 'Jane'
]
]
]
];
ksort_r($data);
print_r($data);
The above will output...
Array
(
[1100] => Array
(
[D] => Array
(
[1] => Array
(
[Name] => Jane
)
)
[F] => Array
(
[2] => Array
(
[Name] => Sam
)
)
)
[1200] => Array
(
[A] => Array
(
[3] => Array
(
[Name] => Paul
)
)
[B] => Array
(
[4] => Array
(
[Name] => Joe
)
)
)
)

Convert multi-dimensional array to flat array php

I have a problem in converting multi-dimensional array to flat array
I have an input array like this:
Array
(
[0] => Array
(
[type] => MagentoCatalogRuleModelRuleConditionCombine
[aggregator] => all
[conditions] => Array
(
[0] => Array
(
[type] => MagentoCatalogRuleModelRuleConditionProduct
[attribute] => category_ids
)
)
)
[1] => Array
(
[type] => MagentoCatalogRuleModelRuleConditionProduct
[attribute] => category_ids
)
[2] => Array
(
[type] => MagentoCatalogRuleModelRuleConditionProduct
[attribute] => attribute_set_id
)
)
and I want the output is an array like this, the child array will be flattened as parent array level and it will be recognized by the index:
Array
(
[1--1] => Array
(
[type] => Magento\CatalogRule\Model\Rule\Condition\Combine
[aggregator] => all
)
[1--1--1] => Array
(
[type] => Magento\CatalogRule\Model\Rule\Condition\Product
[attribute] => category_ids
)
[1--2] => Array
(
[type] => Magento\CatalogRule\Model\Rule\Condition\Product
[attribute] => category_ids
)
[1--3] => Array
(
[type] => Magento\CatalogRule\Model\Rule\Condition\Product
[attribute] => attribute_set_id
)
)
the input array may contain many child layers and the output array must be index in that format.
I'm stucking to write this logic into code. Thank you!
You can use a recursive function like this:
function flatten($array, $parent_key = '1') {
$flattened_array = [];
foreach ($array as $key => $item) {
$tmp = $item;
unset($tmp['conditions']);
$child_key = $parent_key . '--' . strval($key + 1);
$flattened_array[$child_key] = $tmp;
if (isset($item['conditions'])) {
$flattened_array = array_merge($flattened_array, flatten($item['conditions'], $child_key));
}
}
return $flattened_array;
}
$input = [
0 => [
'type' => 'MagentoCatalogRuleModelRuleConditionCombine',
'aggregator' => 'all',
'conditions' => [
0 => [
'type' => 'MagentoCatalogRuleModelRuleConditionProduct',
'attribute' => 'category_ids'
]
]
],
1 => [
'type' => 'MagentoCatalogRuleModelRuleConditionProduct',
'attribute' => 'category_ids'
],
2 => [
'type' => 'MagentoCatalogRuleModelRuleConditionProduct',
'attribute' => 'attribute_set_id'
]
];
print_r(flatten($input));
Output:
Array
(
[1--1] => Array
(
[type] => MagentoCatalogRuleModelRuleConditionCombine
[aggregator] => all
)
[1--1--1] => Array
(
[type] => MagentoCatalogRuleModelRuleConditionProduct
[attribute] => category_ids
)
[1--2] => Array
(
[type] => MagentoCatalogRuleModelRuleConditionProduct
[attribute] => category_ids
)
[1--3] => Array
(
[type] => MagentoCatalogRuleModelRuleConditionProduct
[attribute] => attribute_set_id
)
)

Shift array elements one level to the right (down one level) when using foreach

Sorry if there's an obvious answer, but I've been trying for hours to google the solution and tried various ways. Basically I'm trying to shift an array within a multidimensional array one level to the right. The output array is to be fed into a program which only accepts the desired format of multidimensional array.
We have ($result):
Array (
[0] => Array (
[id] => 1
[data] => AAA
)
[1] => Array (
[id] => 2
[data] => BBB
)
[2] => Array (
[id] => 3
[data] => CCC
)
)
We want to insert a new array [test] in between the multidimensional array. Desired result:
Array (
[0] => Array (
[test] => Array (
[id] => 1
[data] => AAA
)
)
[1] => Array (
[test] => Array (
[id] => 2
[data] => BBB
)
)
[2] => Array (
[test] => Array (
[id] => 3
[data] => CCC
)
)
)
What I've got so far:
foreach ($result as $key => $value) {
$newarray[] = array($key => $value)
};
return $newarray;
Unfortunately the above inserts an incremental index instead of [test] where we want it to.
Can anyone help?
You could use a loop and index in the same array using the current key. Then update the value with a new array where using test as the key and $v as the value.
$arrays = [
0 => [
"id" => 1,
"data" => "AAA"
],
1 => [
"id" => 2,
"data" => "BBB"
],
2 => [
"id" => 3,
"data" => "CCC"
],
];
foreach ($arrays as $k => $v) {
$arrays[$k] = ['test' => $v];
};
print_r($arrays);
Result
Array
(
[0] => Array
(
[test] => Array
(
[id] => 1
[data] => AAA
)
)
[1] => Array
(
[test] => Array
(
[id] => 2
[data] => BBB
)
)
[2] => Array
(
[test] => Array
(
[id] => 3
[data] => CCC
)
)
)
Demo
Try this:
foreach ($result as $key => $value) {
$newarray[$key] = ['test' => $value];
};
This would be a working and flexible approach:
<?php
$data = [
[
'id' => 1,
'data' => "AAA"
],
[
'id' => 2,
'data' => "BBB"
],
[
'id' => 3,
'data' => "CCC"
]
];
array_walk($data, function(&$entry) {
$entry = [
'test' => $entry
];
});
print_r($data);
The output obviously is:
Array
(
[0] => Array
(
[test] => Array
(
[id] => 1
[data] => AAA
)
)
[1] => Array
(
[test] => Array
(
[id] => 2
[data] => BBB
)
)
[2] => Array
(
[test] => Array
(
[id] => 3
[data] => CCC
)
)
)
$data = [
[
'id' => 1,
'data' => "AAA"
],
[
'id' => 2,
'data' => "BBB"
],
[
'id' => 3,
'data' => "CCC"
]
];
$newArray = array_map(function ($value) {
return ['test' => $value];
}, $data);
var_dump($newArray);
https://secure.php.net/manual/en/function.array-map.php

How do I remove one array from another in php?

I have the following arrays in PHP:
print_r($employees) =
Array
(
[0] => Array
(
[p_id] => T29083999
[name] => Robert Plaxo
)
[1] => Array
(
[p_id] => T29083388
[name] => Yvan Sergei
)
[2] => Array
(
[p_id] => T21083911
[name] => Rhonda Saunders
)
[3] => Array
(
[p_id] => H02910382
[name] => Miguel Mercado
)
)
then this array:
print_r($record) =
Array
(
[0] => Array
(
[c_id] => 1
[section] => 1061
[term] => 201631
[p_id] => T29083388
[c_date] => 2016-04-01 09:14:00
)
)
I want to remove the array element from $employees that matches the p_id of $record. Array $record may have multiple entries like the one shown. If so, any p_id in $record must be removed from $employees.
I tried:
foreach ($employees as $k => $e) {
foreach ($record as $r) {
if ($e['p_id']==$r['p_id']) {
echo "found it!";
// if I uncomment the next line, it crashes! (500 server error)
// unset $employees[$k];
}
}
}
I just want to remove any element from $employees that has any employee that matches any record in $record with that employee id.
You were almost there; just needed parens around your unset()
I also took the liberty to change some of your variable names as single character variable names bother me.
$employees[] = [
'p_id' => 'T29083999',
'name' => 'Robert Plaxo',
];
$employees[] = [
'p_id' => 'T29083388',
'name' => 'Yvan Sergei',
];
$employees[] = [
'p_id' => 'T21083911',
'name' => 'Rhonda Saunders',
];
$employees[] = [
'p_id' => 'H02910382',
'name' => 'Miguel Mercado',
];
$records[] = [
'c_id' => '1',
'section' => '1061',
'term' => '201631',
'p_id' => 'T29083388',
'c_date' => '2016-04-01 09:14:00',
];
foreach ($employees as $key => $employee) {
foreach ($records as $record) {
if ($employee['p_id'] == $record['p_id']) {
echo "found it!";
unset($employees[$key]);
}
}
}
echo "<pre>";
print_r($employees);
Outputs
found it!
Array
(
[0] => Array
(
[p_id] => T29083999
[name] => Robert Plaxo
)
[2] => Array
(
[p_id] => T21083911
[name] => Rhonda Saunders
)
[3] => Array
(
[p_id] => H02910382
[name] => Miguel Mercado
)
)
The short solution using array_column and array_filter functions. It will also fit your requirement "Array $record may have multiple entries":
$p_ids = array_column($record, "p_id");
$employees = array_filter($employees, function($v) use($p_ids){
return !in_array($v["p_id"], $p_ids);
});
print_r($employees);
The output:
Array
(
[0] => Array
(
[p_id] => T29083999
[name] => Robert Plaxo
)
[2] => Array
(
[p_id] => T21083911
[name] => Rhonda Saunders
)
[3] => Array
(
[p_id] => H02910382
[name] => Miguel Mercado
)
)

building a multidimensional array from mysql with php

I am trying to form a specific multidimensional array from a mysql result set.
I would like it to look like this:
array(
'product_name' => 'prod_1',
'categories' => array(1,2,3,4)
);
The db result return an array that looks something like this
Array
(
[0] => Array
(
[id] => 1
[product_name] => prod_1
)
[1] => Array
(
[id] => 2
[product_name] => prod_1
)
[2] => Array
(
[id] => 3
[product_name] => prod_1
)
[3] => Array
(
[id] => 4
[product_name] => prod_1
)
As you can see, i would like to group the product name and place the id into another array
Does anyone have any tips on how to do this?
$yourarray = array(array('id'=>1, 'product_name' => 'prod_1'), array('id'=>2, 'product_name' => 'prod_2'),array('id'=>3, 'product_name' => 'prod_3'));
$multiarray = array();
for($i=0; $i<count($yourarray);$i++){
$multiarray['product_name'][] = $yourarray[$i]['product_name'];
$multiarray['product_id'][] = $yourarray[$i]['id'];
}
print_r($yourarray); //original array
print_r($multiarray); //gives you multi array
Something similar to this?
Your original array:
Array
(
[0] => Array
(
[id] => 1
[product_name] => prod_1
)
[1] => Array
(
[id] => 2
[product_name] => prod_2
)
[2] => Array
(
[id] => 3
[product_name] => prod_3
)
)
The result would print:
Array
(
[product_name] => Array
(
[0] => prod_1
[1] => prod_2
[2] => prod_3
)
[product_id] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
)
Did you try GROUP_CONCAT. It's something like:
SELECT name, GROUP_CONCAT(name) AS friends FROM friendships GROUP BY name;
Have a look for details here: http://forums.mysql.com/read.php?10,287931,287936#msg-287936
$yourarray = array(array('id'=>1, 'product_name' => 'prod_1'), array('id'=>2, 'product_name' => 'prod_1'), array('id'=>3, 'product_name' => 'prod_1'),array('id'=>4, 'product_name' => 'prod_1'));
$multiarray = array();
foreach ($yourarray as $value) {
if(!isset($multiarray['product_name'])) {
$multiarray['product_name'] = $value['product_name'];
}
$multiarray['categories'][] = $value['id'];
}
print_r($multiarray);

Categories