I have an array tree from a database, I want to change the key of a child element in this case the second array 'eric'=>array into integer '0'=>array as follow :
0 => Array
('text' => 'paris',
'nodes' => Array
('eric' => Array
( 'text' => 'eric',
'nodes' => Array
(0 => Array
(
'text' => 'so.png',
),
),
),
),
),
there is my code :
while($d = mysqli_fetch_assoc($result)) {
if(!isset($data[$d['country']])) {
$data[$d['country']] = array(
'text' => $d['country'],
'nodes' => array()
);
}
if(!isset($data[$d['country']]['nodes'][$d['name']])) {
$data[$d['country']]['nodes'][$d['name']] = array(
'text' => $d['name'],
'nodes' => array()
);
}
array_push($data[$d['country']]['nodes'][$d['name']]['nodes'], $d['n_doc']);
}
To change all of the child keys to numeric values, you can simply just use array_values()
Live Demo
for($i = 0; $i <= count($data) -1; $i++) { # This loops through each country
$data[$i]['nodes'] = array_map(function($node) { # This preserves the parent text value
return array_values($node); # [0] => Paris, [1] => array(...)
}, $data[$i]['nodes']);
}
Output
[ ... => [ text => Paris, nodes => [ 0 => Paris, 1 => [ ... ] ] ... ] ... ]
can you change your code for this input:
Array
(
[0] => Array
(
[text] => paris
[nodes] => Array
(
[jacque] => Array
(
[text] => jacque
[nodes] => Array
(
[0] => 32.png
)
)
[anis] => Array
(
[text] => anis
[nodes] => Array
(
[0] => 5384a97ee9d6b (2).pd
)
)
)
)
[1] => Array
(
[text] => london
[nodes] => Array
(
[dodo] => Array
(
[text] => dodo
[nodes] => Array
(
[0] => 148782.svg
[1] => 333.png
)
)
[sd] => Array
(
[text] => sd
[nodes] => Array
(
[0] => 1014-favicon.ico
)
)
)
)
)
Related
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
)
)
)
)
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
)
)
i'm new in php and i stuck some where actually i need to generate multidimensional array from array value.
e.g my array is like that and remember all array and value are dynamic
array(
0 => array(
0 => "college"
1 => "student"
2 => "contact"
),
1 => array(
0 => "college"
1 => "parents"
2 => "contact"
),
2 => array(
0 => "school"
1 => "parents"
2 => "contact"
),
3 => array(
0 => "school"
1 => "student"
2 => "contact"
))
and i want result like that
0 => array (
"college" => array(
"student" => array (
"contact" => array (
"address" => "address_value"
)
),
"parents" => array (
"contact" => array (
"address" => "address_value"
)
),
),
"school" => array(
"student" => array (
"contact" => array (
"address" => "address_value"
)
),
"parents" => array (
"contact" => array (
"address" => "address_value"
)
),
)),
i want to generate multidimensional array till the array value and last array has some value
can any one help me with standard way.
help will appreciated..
thanks in advance
Try this:
<?php
function group($a, $level, $previous = '') {
$b = [];
for( $i = 0, $n = count($a); $i < $n; ++$i ) {
if( $level > 0 && $a[$i][$level-1] !== $previous ) {
continue;
}
$key = $a[$i][$level];
$b[$key] = [];
if( array_key_exists($level+1, $a[$i]) ) {
$b[$key] = group($a, $level+1, $key);
}
}
return $b;
}
print_r(group($a, 0));
Output:
Array(
[college] => Array (
[student] => Array (
[contact] => Array ()
)
[parents] => Array (
[contact] => Array ()
)
)
[school] => Array (
[student] => Array (
[contact] => Array ()
)
[parents] => Array (
[contact] => Array ()
)
)
)
Using #AlivetoDie example:
Array (
[college] => Array (
[student] => Array (
[contact] => Array ()
)
[parents] => Array (
[contact] => Array ()
)
)
[school] => Array (
[parents] => Array (
[contact] => Array ()
)
[student] => Array (
[contact] => Array ()
)
[data] => Array (
[contact] => Array()
)
)
)
I have the following array
Array
(
[0] => Array
(
[text] => Array
(
[content] => I
[beginOffset] => 0
)
[partOfSpeech] => Array
(
[tag] => PRON
[aspect] => ASPECT_UNKNOWN
[case] => NOMINATIVE
[form] => FORM_UNKNOWN
[gender] => GENDER_UNKNOWN
[mood] => MOOD_UNKNOWN
[number] => SINGULAR
[person] => FIRST
[proper] => PROPER_UNKNOWN
[reciprocity] => RECIPROCITY_UNKNOWN
[tense] => TENSE_UNKNOWN
[voice] => VOICE_UNKNOWN
)
[dependencyEdge] => Array
(
[headTokenIndex] => 1
[label] => NSUBJ
)
[lemma] => I
)
...
I want to remove all elements that contain the string "_UNKNOWN" as they are not necessairy
how would I go about that?
Assuming all your 'UNKNOWN' are going to be in 'partOfSpeech', you can use this simple code to remove all the elements containing the string '_UNKNOWN':
$array = ['text' => ['content' => 'I', 'beginOffset' => 0], 'partOfSpeech' => ['tag' => 'PRON', 'aspect' => 'ASPECT_UNKNOWN', 'form' => 'FORM_UNKNOWN']]; // Example array
$array['partOfSpeech'] = array_filter($array['partOfSpeech'],
function($item) {
return strpos($item, '_UNKNOWN') === false;
});
print_r($array);
Output:
Array ( [text] => Array ( [content] => I [beginOffset] => 0 ) [partOfSpeech] => Array ( [tag] => PRON ) )
I thought I have some understanding of arrays. But it looks like I have no understanding. Or my head don't want to work. I have arrays:
[0] => Array
(
[key] => Person 1
[values] => Array
(
[0] => Array
(
[0] => 1436821440000,12
)
)
)
[1] => Array
(
[key] => Person 2
[values] => Array
(
[0] => Array
(
[0] => 1437562620000,24
)
)
)
[2] => Array
(
[key] => Person 3
[values] => Array
(
[0] => Array
(
[0] => 1437080040000,10
)
)
)
[3] => Array
(
[key] => Person 1
[values] => Array
(
[0] => Array
(
[0] => 1437082860000,1
)
)
)
[4] => Array
(
[key] => Person 3
[values] => Array
(
[0] => Array
(
[0] => 1437081840000,9
)
)
)
And here is what I want to achieve:
[0] => Array
(
[key] => Person 1
[values] => Array
(
[0] => Array
(
[0] => 1436821440000,12
[1] => 1437082860000,1
)
)
)
[1] => Array
(
[key] => Person 2
[values] => Array
(
[0] => Array
(
[0] => 1437562620000,24
)
)
)
[2] => Array
(
[key] => Person 3
[values] => Array
(
[0] => Array
(
[0] => 1437080040000,10
[1] => 1437081840000,9
)
)
)
How could I remove duplicates and merge data?
The sample input doesn't offer any variation in the data contained in the deept subarrays, so I'll demonstrate the utility of my snippet by adding a little more complexity in the data.
Effectively, each time a new key is encountered, you declare a new row containing the key element and a values element with an empty array. To identify that key as "encountered", the new row is assigned to the result array using the key value as the first level key.
Now that the values subarray is guaranteed to be declared, an inner loop will comb through the deeper arrays and push one or more (all) deep values into the respective values subarray -- in a flat fashion.
Code: (Demo)
$array = [
['key' => 'Person 1', 'values' => [['1436821440000,12']]],
['key' => 'Person 2', 'values' => [['1437562620000,24']]],
['key' => 'Person 3', 'values' => [['1437080040000,10']]],
['key' => 'Person 1', 'values' => [['1437082860000,1'], ['1437082860000,2', '1437082860000,3']]],
['key' => 'Person 3', 'values' => [['1437081840000,9']]],
];
$result = [];
foreach ($array as $row) {
if (!isset($result[$row['key']])) {
$result[$row['key']] = ['key' => $row['key'], 'values' => []];
}
foreach ($row['values'] as $values) {
array_push($result[$row['key']]['values'], ...$values);
}
}
var_export(
array_values($result)
);
Output:
array (
0 =>
array (
'key' => 'Person 1',
'values' =>
array (
0 => '1436821440000,12',
1 => '1437082860000,1',
2 => '1437082860000,2',
3 => '1437082860000,3',
),
),
1 =>
array (
'key' => 'Person 2',
'values' =>
array (
0 => '1437562620000,24',
),
),
2 =>
array (
'key' => 'Person 3',
'values' =>
array (
0 => '1437080040000,10',
1 => '1437081840000,9',
),
),
)
$result_arr = array();
foreach ($arr as $sub_arr)
{
$result_arr = array_merge($result_arr, $sub_arr);
$result_arr = array_unique($result_arr);
}