I have a multidimensional array where I would like to remove a specific layer of the data. Basically, I would like to remove all the labels that are numeric aka the [0] => Array, [1] => Array, [2] => Array, and [3] => Array.
Here's the array I have currently:
Array
(
[0] => Array
(
[CN=Abraham Lincoln,CN=Users,DC=test,DC=io] => Array
(
[objectclass] => Array
(
[0] => top
[1] => person
[2] => organizationalPerson
[3] => user
)
[cn] => Abraham Lincoln
)
)
[1] => Array
(
[CN=Administrator,CN=Users,DC=test,DC=io] => Array
(
[objectclass] => Array
(
[0] => top
[1] => person
[2] => organizationalPerson
[3] => user
)
[distinguishedname] => CN=Administrator,CN=Users,DC=test,DC=io
)
)
[2] => Array
(
[CN=CloudNCUsers,CN=Users,DC=test,DC=io] => Array
(
[objectclass] => Array
(
[0] => top
[1] => group
)
[distinguishedname] => CN=CloudNCUsers,CN=Users,DC=test,DC=io
)
)
[3] => Array
(
[CN=Jill Dope,CN=Users,DC=test,DC=io] => Array
(
[objectclass] => Array
(
[0] => top
[1] => person
[2] => organizationalPerson
[3] => user
)
[distinguishedname] => CN=Jill Dope,CN=Users,DC=test,DC=io
)
)
)
Here's the array I need:
Array
(
[CN=Abraham Lincoln,CN=Users,DC=test,DC=io] => Array
(
[objectclass] => Array
(
[0] => top
[1] => person
[2] => organizationalPerson
[3] => user
)
[cn] => Abraham Lincoln
)
)
[CN=Administrator,CN=Users,DC=test,DC=io] => Array
(
[objectclass] => Array
(
[0] => top
[1] => person
[2] => organizationalPerson
[3] => user
)
[distinguishedname] => CN=Administrator,CN=Users,DC=test,DC=io
)
[CN=CloudNCUsers,CN=Users,DC=test,DC=io] => Array
(
[objectclass] => Array
(
[0] => top
[1] => group
)
)
[CN=Jill Dope,CN=Users,DC=test,DC=io] => Array
(
[objectclass] => Array
(
[0] => top
[1] => person
[2] => organizationalPerson
[3] => user
)
[distinguishedname] => CN=Jill Dope,CN=Users,DC=test,DC=io
)
)
Any help would be appreciated, thanks!
You can remove a level from the array using key and reset and saving to a new array:
$result = array();
foreach ($entries as $entry) {
$result[key($entry)] = reset($entry);
}
Demo on 3v4l.org
You should be able to do the same thing when you initially load the array to save this extra step:
$entry = ldap_get_entries($ldapconnection, $result);
$array[key($entry)] = reset($entry);
Note that this assumes that all of the key values (CN=Abraham Lincoln,CN=Users,DC=test,DC=io etc.) are unique. If they are not, later values in the array will overwrite earlier ones in the output. Note also that if ldap_get_entries returns more than one value, this will remove all but the first. However based on your existing code and the sample data you have provided, it would appear that this is not the case.
I would like your help removing records from an array using an array to define the records that need to be removed, please see the example below
Array 1
$exclusion = array("ABC-DEF","GHI-JKL");
Array 2
Array
(
[0] => Array
(
[Name] => ABC-DEF
[Stack] => Dev
)
[1] => Array
(
[Name] => KML-XWZ
[Stack] => Test
)
[2] => Array
(
[Name] => GHI-JKL
[Stack] => Mock
)
[3] => Array
(
[Name] => MNO-PQR
[Stack] => String
)
)
Expected Output
Array
(
[1] => Array
(
[Name] => KML-XWZ
[Stack] => Test
)
[2] => Array
(
[Name] => MNO-PQR
[Stack] => String
)
)
I did some testing, and surely you can try looping through the $target array and set a condition to unset elements using the in_array() php function for checking each item against the $excusion array you have.
I have array like this in PHP :
Array
(
[0] => Array
(
[item] => value
[item1] => value
)
[1] => Array
(
[item] => value
[item1] => value
)
[2] => Array
(
[item] => value
[item1] => value
)
[3] => Array
(
[item] => value
[item1] => value
)
[4] => Array
(
[item] => value
[item1] => value
)
[5] => Array
(
[item] => value
[item1] => value
)
)
and I Have to make it like this to show two item per page
Array
(
[0] => Array
(
[0] => Array
(
[item] => value
[item1] => value
)
[1] => Array
(
[item] => value
[item1] => value
)
)
[1] => Array
(
[2] => Array
(
[item] => value
[item1] => value
)
[3] => Array
(
[item] => value
[item1] => value
)
)
[2] => Array
(
[4] => Array
(
[item] => value
[item1] => value
)
[5] => Array
(
[item] => value
[item1] => value
)
)
)
I have try using array_merge_recursive and few other way but not able to built logic to get above output
Can anyone Help me with this please.
You can use
array_chunk($array, 2);
It should solve your problem
array_chunk — Split an array into chunks
I have an multidimensional array but need to make it smaller.
This is an easy question I believe. I need to remove 1 array in
jsonresult array, the first one but preserve the other in the next array. I have tried array_splice but it only keeps one.
Array
(
[searchword] => search word
[jsonresult] => Array
(
[0] => Array // THIS ONE, KEEP ITS CHILDREN MOVE UP
(
[0] => Array
(
[id] => 14889770
)
[1] => Array
(
[id] => 14389720
)
[2] => Array
(
[id] => 14869723
)
)
[1] => Array // THIS ONE, KEEP ITS CHILDREN MOVE UP
(
[0] => Array
(
[id] => 14889722
)
[1] => Array
(
[id] => 14389711
)
[2] => Array
(
[id] => 14869329
)
)
)
)
Would like to get:
Array
(
[searchword] => search word
[jsonresult] => Array
(
[0] => Array
(
[id] => 14889770
)
[1] => Array
(
[id] => 14389720
)
[2] => Array
(
[id] => 14869723
)
[3] => Array
(
[id] => 14889722
)
[4] => Array
(
[id] => 14389711
)
[5] => Array
(
[id] => 14869329
)
)
)
Try this code. This may not be the correct method but it gives what you need. (As I understand from your question)
//creating a sample array similar to one you given in question.
$arr_test['searchword'] = 'search word';
$arr_test['jsonresult'] = array(array(array('id'=>14889770),array('id'=>14889720)),array(array('id'=>14889780),array('id'=>14889790)));
//creating new array
$arr_new = array();
//formatting array as you needed it
foreach($arr_test['jsonresult'] as $arr_jsonresult){
foreach($arr_jsonresult as $jsonresult){
$arr_new['jsonresult'][] = $jsonresult;
}
}
//overwriting the specific array key
$arr_test['jsonresult'] = $arr_new['jsonresult'];
//checking output
echo '<pre>';
print_r($arr_test);
This code produces the following output
Array
(
[searchword] => search word
[jsonresult] => Array
(
[0] => Array
(
[id] => 14889770
)
[1] => Array
(
[id] => 14889720
)
[2] => Array
(
[id] => 14889780
)
[3] => Array
(
[id] => 14889790
)
)
)
i have big problem, because i don't know how get values from this array where value is be key into new array. This is my source array
Array
(
[0] => Array
(
[ID] => 250602
[NAME] => qwe
)
[1] => Array
(
[ID] => 250603
[NAME] => wer
)
[2] => Array
(
[ID] => 250629
[NAME] => sdf
)
[3] => Array
(
[ID] => 250629
[NAME] => xcv
)
[4] => Array
(
[ID] => 250629
[NAME] => fghfgh
)
[5] => Array
(
[ID] => 250601
[NAME] => pggd
)
[6] => Array
(
[ID] => 250601
[NAME] => dfgdfg
)
[7] => Array
(
[ID] => 250606
[NAME] => dfgdfg
)
)
When id is the same it will be created a new table that will look like for id = 250629
[NAME] => Array
(
[0] => sdf
[1] => xcv
[2] => fghfgh
)
How about foreach loop like this?
<?php
$final_array=array();
foreach($arrays as $sub_arr){ //it will traverse loop for all sub-arrays
$final_array[$sub_arr['ID']][]=$sub_arr['NAME'];
}
print_r($final_array); //you should see expected output.
?>
It will product below output for your given data:
Array
(
[250602] => Array
(
[0] => qwe
)
[250603] => Array
(
[0] => wer
)
[250629] => Array
(
[0] => sdf
[1] => xcv
[2] => fghfgh
)
[250601] => Array
(
[0] => pggd
[1] => dfgdfg
)
[250606] => Array
(
[0] => dfgdfg
)
)
Working Demo
Like this
$by_name = array();
foreach($your_array as $item)
$by_name[$item['ID']] []= $item['name'];
This makes use of php's lazy array initialization ([]= creates a new array implicitly).
If you get your array from mysql, you might also consider GROUP_CONCAT.