Using foreach loops in PHP I would like to add ids to the following object...
$array_before
Array
(
[1111] => Array
(
[Name] => Name A
[Subcats] => Array
(
[11111] => Array
(
[Name] => Name A.1
)
[11112] => Array
(
[Name] => Name A.2
)
)
)
[2222] => Array
(
[Name] => Name B
[Subcats] => Array
(
[22221] => Array
(
[Name] => Name B.1
)
[22222] => Array
(
[Name] => Name B.2
)
)
)
)
... so it looks similar to the below:
$array_after
Array
(
[1111] => Array
(
[Id] => 1
[Name] => Name A
[Subcats] => Array
(
[11111] => Array
(
[Id] => 1
[Name] => Name A.1
)
[11112] => Array
(
[Id] => 2
[Name] => Name A.2
)
[11113] => Array
(
[Id] => 3
[Name] => Name A.2
)
)
)
[2222] => Array
(
[Id] => 2
[Name] => Name B
[Subcats] => Array
(
[22221] => Array
(
[Id] => 1
[Name] => Name B.1
)
[22222] => Array
(
[Id] => 2
[Name] => Name B.2
)
)
)
)
Could someone point me in the right direction?
Thanks,
LG
Try this:
$id = 0;
array_walk($array,function(&$a) use (&$id) {$a['id'] = ++$id;});
This will modify the originl array to add the IDs, rather than create a new one.
Related
I have this Array from a json that represent a form. I have to collect all the [components] in a new array
Array
(
[components] => Array
(
[0] => Array
(
[key] => number1
[type] => number
[input] => 1
[label] => Test Number Field
)
[1] => Array
(
[key] => testRadioFiled
[type] => radio
[label] => Test Radio Filed
[values] => Array
(
[0] => Array
(
[label] => First
[value] => first
[shortcut] =>
)
)
)
[2] => Array
(
[key] => testSelectBoxField
[type] => selectboxes
[input] => 1
[label] => Test Select Box Field
[values] => Array
(
[0] => Array
(
[label] => First
[value] => first
[shortcut] =>
)
)
)
[3] => Array
(
[key] => testSelectField
[data] => Array
(
[values] => Array
(
[0] => Array
(
[label] => First
[value] => first
)
)
)
[type] => select
[input] => 1
[label] => Test Select Single Field
)
[4] => Array
(
[key] => testPanel
[type] => panel
[input] =>
[label] => Panel
[title] => Test Panel
[components] => Array
(
[0] => Array
(
[key] => testSelectMultipleField
[data] => Array
(
[values] => Array
(
[0] => Array
(
[label] => First
[value] => first
)
[1] => Array
(
[label] => Second
[value] => second
)
[2] => Array
(
[label] => Third
[value] => third
)
)
)
[mask] =>
[sort] =>
[type] => select
[input] => 1
[label] => Test Select Multiple Field
)
[1] => Array
(
[key] => testColumns
[type] => columns
[label] => Test Columns
[columns] => Array
(
[0] => Array
(
[key] => column
[type] => column
[label] => Column
[components] => Array
(
[0] => Array
(
[key] => radio
[type] => radio
[input] => 1
[label] => Radio
[values] => Array
(
[0] => Array
(
[label] => first
[value] => first
[shortcut] =>
)
[1] => Array
(
[label] => second
[value] => second
[shortcut] =>
)
[2] => Array
(
[label] => third
[value] => third
[shortcut] =>
)
)
)
)
)
[1] => Array
(
[key] => column
[type] => column
[label] => Column
[components] => Array
(
[0] => Array
(
[key] => testSelectField2
[tab] => 1
[data] => Array
(
[values] => Array
(
[0] => Array
(
[label] => First
[value] => first
)
[1] => Array
(
[label] => Second
[value] => second
)
[2] => Array
(
[label] => Third
[value] => third
)
)
)
[type] => select
[input] => 1
[label] => Test Select Single Field
)
)
)
)
)
)
[collapsible] =>
)
[5] => Array
(
[key] => tabs2
[mask] =>
[type] => tabs
[input] =>
[label] => Tabs
[components] => Array
(
[0] => Array
(
[key] => tab2
[label] => Tab 1
[components] => Array
(
[0] => Array
(
[key] => columns2
[tab] => 0
[mask] =>
[type] => columns
[input] =>
[label] => Columns
[logic] => Array
(
)
[columns] => Array
(
[0] => Array
(
[key] => column
[type] => column
[input] =>
[label] => Column
[components] => Array
(
[0] => Array
(
[key] => number4
[type] => number
[input] => 1
[label] => Test Number Field in Column
)
)
)
[1] => Array
(
[key] => column
[type] => column
[input] =>
[label] => Column
[components] => Array
(
[0] => Array
(
[key] => number3
[type] => number
[input] => 1
[label] => Test Number Field in Column
)
)
)
)
)
)
)
[1] => Array
(
[key] => tab2
[label] => Tab 2
[components] => Array
(
[0] => Array
(
[key] => testSelectField1
[tab] => 1
[data] => Array
(
[values] => Array
(
[0] => Array
(
[label] => First
[value] => first
)
[1] => Array
(
[label] => Second
[value] => second
)
[2] => Array
(
[label] => Third
[value] => third
)
)
)
[type] => select
[input] => 1
[label] => Test Select Single Field
)
)
)
)
)
)
)
I solved making a serious of foreach and in each loop I used logic:
If not exist an array "components" -> check if is type columns
if yes check if exist "components" and saved in new array
if not save in new array
If exist again check apply the same condition of point 1 this for 3 or 4 sublevel.
This is the final array that I need
Array
(
[0] => Array
(
[key] => number1
[type] => number
[input] => 1
[label] => Test Number Field
)
[1] => Array
(
[key] => testRadioFiled
[type] => radio
[label] => Test Radio Filed
[values] => Array
(
[0] => Array
(
[label] => First
[value] => first
[shortcut] =>
)
)
)
......
[2] => Array
(
[key] => testSelectMultipleField
[data] => Array
(
[values] => Array
(
[0] => Array
(
[label] => First
[value] => first
)
[1] => Array
(
[label] => Second
[value] => second
)
[2] => Array
(
[label] => Third
[value] => third
)
)
)
[mask] =>
[sort] =>
[type] => select
[input] => 1
[label] => Test Select Multiple Field
)
}
This code make the job but can't be the final solution.
$test_array = Array();
foreach($data['components'] AS $comp_1){
if(!isset($comp_1['components'])){
if($comp_1['type'] != 'columns'){
$test_array[] = $comp_1;
} else {
foreach($comp_1['columns'] as $col_1){
if(!isset($col_1['components'])){
$test_array[] = $col_1;
} else {
foreach($col_1['components'] AS $col_comp_1){
if(!isset($col_comp_1['components'])){
$test_array[] = $col_comp_1;
}
}
}
}
}
} else {
foreach($comp_1['components'] AS $comp_2){
if(!isset($comp_2['components'])){
if($comp_2['type'] != 'columns'){
$test_array[] = $comp_2;
} else {
foreach($comp_2['columns'] as $col_2){
if(!isset($col_2['components'])){
$test_array[] = $col_2;
} else {
foreach($col_2['components'] AS $col_comp_2){
if(!isset($col_comp_2['components'])){
$test_array[] = $col_comp_2;
}
}
}
}
}
} else {
foreach($comp_2['components'] AS $comp_3){
if(!isset($comp_3['components'])){
if($comp_3['type'] != 'columns'){
$test_array[] = $comp_3;
} else {
foreach($comp_3['columns'] as $col_3){
if(!isset($col_3['components'])){
$test_array[] = $col_3;
} else {
foreach($col_3['components'] AS $col_comp_3){
if(!isset($col_comp_3['components'])){
$test_array[] = $col_comp_3;
}
}
}
}
}
}
}
}
}
}
}
My question is there is a way for extract all the "components" array that of coarse take care if not contain other "components" array ?
If I understood you correctly recursion can help:
function extractComponents($arrayWithComponents)
{
$components = [];
if (!isset($arrayWithComponents['components']) || !is_array($arrayWithComponents['components'])) {
return $components;
}
foreach ($arrayWithComponents['components'] as $component) {
if (isset($component['columns'])) {
foreach ($component['columns'] as $column) {
$components = array_merge($components, extractComponents($column));
}
} else {
$components = array_merge($components, extractComponents($component));
unset($component['components']);
$components[] = $component;
}
}
return $components;
}
var_dump(extractComponents($data));
Example.
I want to generate a nested associative array from the result of SQL query from the table which should look like this:
[0] => Array
(
[name] => RODENTIA
[children_no] => 3
[children] => Array
(
[0] => Array
(
[name] => Sciuridae
[children_no] => 1
[children] => Array
(
[0] => Array
(
[name] => Marmota
[children] => Array
(
[0] => Array
(
[name] => Marmota himalayan
[children] => Himalayan Marmot
)
)
)
)
),
[1] => Array
(
[name] => Spalacidae
[children_no] => 1
[children] => Array
(
[0] => Array
(
[name] => Cannomys
[children] => Array
(
[0] => Array
(
[name] => Cannomys badius
[children] => Lesser Bamboo Rat
)
)
)
)
),
[2] => Array
(
[name] => Spalacidae
[children_no] => 1
[children] => Array
(
[0] => Array
(
[name] => Cannomys
[children] => Array
(
[0] => Array
(
[name] => Cannomys badius
[children] => Lesser Bamboo Rat
)
)
)
)
),
[3] => Array
(
[name] => Cricetidae
[children_no] => 1
[children] => Array
(
[0] => Array
(
[name] => Alticola
[children] => Array
(
[0] => Array
(
[name] => Alticola roylei
[children] => Royle's Mountain V
)
)
)
)
)
)
)
Problem: I am unable to figure it out how to nest array if there are same subsequent particular column values, for example, Rodentia has 3 different families (according to the table) it should nest inside children array and similarly for other fields as shown in the above array. My code is as follows:
$sql = "SELECT * FROM `mamallia_table`";
$result = $conn->query($sql);
$genus = array();
if ($result->num_rows > 0) {
foreach($result as $row){
$data[] = classification_order($row);
}
echo "<pre>";
print_r($data);
} else {
echo "0 results";
}
}
function classification_order($row){
$species[] = array("name"=>$row['species'],"children"=>$row['common_name']);
$genus[] = array("name"=>$row['genus'],"children"=>$species);
$family_count[] = array("name"=>$row['family'],"children_no"=>$row['no_of_species'],"children"=>$genus);
$data = ["name"=>$row['order'],"children_no"=>$row['no_of_family'],"children"=>$family_count];
return $data;
}
Output: Which gives me the resulting array:
Array
(
[0] => Array
(
[name] => RODENTIA
[children_no] => 3
[children] => Array
(
[0] => Array
(
[name] => Sciuridae
[children_no] => 1
[children] => Array
(
[0] => Array
(
[name] => Marmota
[children] => Array
(
[0] => Array
(
[name] => Marmota himalayan
[children] => Himalayan Marmot
)
)
)
)
)
)
)
[1] => Array
(
[name] => RODENTIA
[children_no] => 3
[children] => Array
(
[0] => Array
(
[name] => Spalacidae
[children_no] => 1
[children] => Array
(
[0] => Array
(
[name] => Cannomys
[children] => Array
(
[0] => Array
(
[name] => Cannomys badius
[children] => Lesser Bamboo Rat
)
)
)
)
)
)
)
[2] => Array
(
[name] => RODENTIA
[children_no] => 3
[children] => Array
(
[0] => Array
(
[name] => Cricetidae
[children_no] => 1
[children] => Array
(
[0] => Array
(
[name] => Alticola
[children] => Array
(
[0] => Array
(
[name] => Alticola roylei
[children] => Royle's Mountain V
)
)
)
)
)
)
)
)
Updated: I have two separate arrays which hold some data returned from search() function of SolrPhpClient. The arrays contain data in form of Apache_Sole_Document objects. These objects in turn contain the actual fields and values. I merge these two arrays to get a single array holding all items using array_merge() of PHP
The array will have some duplicate items which needs to be removed.
I am not sure how to achieve it in this structure.
The array structure is as such:
Array ( [0] => Apache_Solr_Document Object (
[_documentBoost:protected] =>
[_fields:protected] => Array ( [id] => 111 [name] => ABCD )
[_fieldBoosts:protected] => Array ( [id] => [name] => )
)
[1] => Apache_Solr_Document Object (
[_documentBoost:protected] =>
[_fields:protected] => Array ( [id] => 222 [name] => DEFG )
[_fieldBoosts:protected] => Array ( [id] => [name] => )
)
[2] => Apache_Solr_Document Object (
[_documentBoost:protected] =>
[_fields:protected] => Array ( [id] => 333 [name] => LMNO )
[_fieldBoosts:protected] => Array ( [id] => [name] => )
)
[3] => Apache_Solr_Document Object (
[_documentBoost:protected] =>
[_fields:protected] => Array ( [id] => 111 [name] => ABCD )
[_fieldBoosts:protected] => Array ( [id] => [name] => )
)
[4] => Apache_Solr_Document Object (
[_documentBoost:protected] =>
[_fields:protected] => Array ( [id] => 444 [name] => PQRS )
[_fieldBoosts:protected] => Array ( [id] => [name] => )
)
[5] => Apache_Solr_Document Object (
[_documentBoost:protected] =>
[_fields:protected] => Array ( [id] => 222 [name] => DEFG )
[_fieldBoosts:protected] => Array ( [id] => [name] => )
)
)
As you can see there is a [id] field and a [name] field.
I would like to remove duplicates from the array comparing the [id] field.
The final array after removing duplicates should look like this:
Array ( [0] => Apache_Solr_Document Object (
[_documentBoost:protected] =>
[_fields:protected] => Array ( [id] => 111 [name] => ABCD )
[_fieldBoosts:protected] => Array ( [id] => [name] => )
)
[1] => Apache_Solr_Document Object (
[_documentBoost:protected] =>
[_fields:protected] => Array ( [id] => 222 [name] => DEFG )
[_fieldBoosts:protected] => Array ( [id] => [name] => )
)
[2] => Apache_Solr_Document Object (
[_documentBoost:protected] =>
[_fields:protected] => Array ( [id] => 333 [name] => LMNO )
[_fieldBoosts:protected] => Array ( [id] => [name] => )
)
[3] => Apache_Solr_Document Object (
[_documentBoost:protected] =>
[_fields:protected] => Array ( [id] => 444 [name] => PQRS )
[_fieldBoosts:protected] => Array ( [id] => [name] => )
)
)
How can I achieve this? somebody please help!
I guess you could iterate over the array and remove the duplicates, but that doesn't seem like a neat solution to me.
I'm not familiar with the SolrPhpClient, but Solr does support grouping on fields. You can group on the id by adding this part to your request:
group=true&group.field=id
The documents returned will be grouped on the id. For more details check this page.
Update:
I looked at the SolrPhpClient documentation and see that you can add additional parameters to your request like this:
$additionalParameters = array(
'fq' => 'a filtering query',
'facet' => 'true',
'facet.field' => array(
'field_1',
'field_2'
)
);
$results = $solr->search($query, $start, $rows, $additionalParameters);
I assume you can add the grouping parameters to this:
$additionalParameters = array(
'group' => 'true',
'group.field' => 'id'
)
);
$results = $solr->search($query, $start, $rows, $additionalParameters);
For more details on this, check this page
im using cakephp 1.3. Im trying to sort an array using Set::sort() function but is not working.. any idea on how to do this? below is the array im using.
Array (
[0] => Array
(
[Group] => Array
(
[name] => Team A
)
[Members] => Array
(
[0] => Array
(
[name] => George
[Code] => Array
(
[name] => C
)
)
[1] => Array
(
[name] => Hall
[Code] => Array
(
[name] => A
)
)
[2] => Array
(
[name] => Mike
[Code] => Array
(
[name] => B
)
)
)
)
im sorting the array using this :
$data = Set::sort($data, '{n}.Members.{n}.Code.name', 'asc');
im expecting an output like this:
Array
(
[0] => Array
(
[Group] => Array
(
[name] => Team A
)
[Members] => Array
(
[0] => Array
(
[name] => Hall
[Code] => Array
(
[name] => A
)
)
[1] => Array
(
[name] => Mike
[Code] => Array
(
[name] => B
)
)
[2] => Array
(
[name] => George
[Code] => Array
(
[name] => C
)
)
)
)
The sorting does not take in effect.how can i do this? any idea?
Using only Set::sort() its no doable. You can you this:
$result = array();
foreach($a as $arr) {
$res = Set::sort($arr['Member'], '{n}.Code.name', 'asc');
$result[] = array(
'Group' => $arr['Group'],
'Member' => $res
);
}
pr($result);
Quotes object:
Array
(
[0] => Array
(
[sitecaptions] => Array
(
[id] => 2
[caption] => Great camera deals!
[linkurl] => http://www.99hotdeals.com/cat/Cameras
and Camcorders
)
)
)
The Posts Object:
Array
(
[0] => Array
(
[Post] => Array
(
[id] => 2797
[post_title] => xx1
[item_desc] => xx desc
[dateadded] => 2009-12-22 11:10:15
)
[Category] => Array
(
[0] => Array
(
[id] => 99
[name] => Others
)
)
)
[1] => Array
(
[Post] => Array
(
[id] => 2798
[post_title] => xx2
[item_desc] => xx2 desc
[dateadded] => 2009-12-22 11:10:45
)
[Category] => Array
(
[0] => Array
(
[id] => 99
[name] => Others
)
)
)
)
As you can see, the Posts Object contains two elements, [Post] and
[Category] for each record [0],[1] etc. I want to insert the
[sitecaptions] element into that Posts Object so that in effect it
looks like:
Array
(
[0] => Array
(
[Post] => Array
(
[id] => 2797
[post_title] => xx1
[item_desc] => xx desc
[dateadded] => 2009-12-22 11:10:15
)
[Category] => Array
(
[0] => Array
(
[id] => 99
[name] => Others
)
)
[sitecaptions] => Array
(
[id] => 2
[caption] => Great camera deals!
[linkurl] => http://www.99hotdeals.com/cat/Cameras
and Camcorders
)
)
)
How do I combine two objects like that? Or how do I insert elements
into an existing object? Hope I'm clear about what I'm asking. Thanks
for your time...
lets call these objects $Quotes and $Posts respectively.
Quotes object:
Array (
[0] => Array (
[sitecaptions] => Array (
[id] => 2
[caption] => Great camera deals!
[linkurl] => http://www.99hotdeals.com/cat/Cameras and Camcorders )
)
)
)
The Posts Object:
Array (
[0] => Array (
[Post] => Array (
[id] => 2797
[post_title] => xx1
[item_desc] => xx desc
[dateadded] => 2009-12-22 11:10:15
)
[Category] => Array (
[0] => Array (
[id] => 99
[name] => Others
)
)
)
[1] => Array (
[Post] => Array (
[id] => 2798
[post_title] => xx2
[item_desc] => xx2 desc
[dateadded] => 2009-12-22 11:10:45
)
[Category] => Array (
[0] => Array (
[id] => 99
[name] => Others
)
)
)
)
do you want the [sitecaptions] from the $quotes to be in both of your $posts elements? or just the one with the same key?
as to say if you have just $quotes[0] only $posts[0] will be affected. OR both $posts[0] and $posts[1] will be affected.
if you want $quotes[0] to be in both $post elements you can do this:
foreach ($posts as $key=>$post) {
$posts[$key]['sitecaptions'] = $quotes[0]['sitecaptions'];
}
if you want only the elements from $quotes that have the same index as the elements in $posts you can do this:
$posts = array_merge_recursive($posts,$quotes);
doing this second one would have $posts[1] being without a ['sitecaptions'] element.
end result:
Array (
[0] => Array (
[Post] => Array (
[id] => 2797
[post_title] => xx1
[item_desc] => xx desc
[dateadded] => 2009-12-22 11:10:15
)
[Category] => Array (
[0] => Array (
[id] => 99
[name] => Others
)
)
[sitecaptions] => Array (
[id] => 2
[caption] => Great camera deals!
[linkurl] => http://www.99hotdeals.com/cat/Cameras and Camcorders
)
)
)
Hope it helps!