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.
Related
How to insert new array to a specific position in multi dimensional array below is the code, how it will be done using php
$aExistingArray = Array
(
[pages] => Array
(
[0] => Array
(
[name] => page1
[elements] => Array
(
[0] => Array
(
[type] => text
[name] => question2
[title] => PSTN
)
[1] => Array
(
[type] => radiogroup
[name] => question3
[title] => Are you satisfied to our services
[choices] => Array
(
[0] => Array
(
[value] => item1
[text] => yes
)
[1] => Array
(
[value] => item2
[text] => no
)
)
)
)
)
))
Below is the new array which I want to insert to elements position at 0 position, how it will be done using array technique
$aToBeInserted = Array
(
[0] => Array
(
[type] => text
[name] => name
[title] => name
)
[1] => Array
(
[type] => text
[name] => question1
[title] => Test
)
)
Below is the solution, first find the specific index of array, merge the both arrays and then override with original array.
$elementPostition = $arr['pages'][0]['elements'];
$aMergeArray = array_merge($sTobeInsert,$elementPostition );
$arr['pages'][0]['elements'] = $aMergeArray;
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 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.
Here is my problem how can i get the array[1]
i have an foreach on my view i can't get the value of [1] => Array?
Array
(
[0] => Array
(
[details] => Array
(
[product_id] => 1
[0] => Array
(
[field_name] => 犬種
[field] => ペã‚ニーズ
)
[1] => Array
(
[field_name] => 生年月日
[field] => 2013年12月13日
)
[2] => Array
(
[field_name] => 性別
[field] => メス
)
[3] => Array
(
[field_name] => ä¾¡æ ¼
[field] => ¥98,000
)
)
[image] => Array
(
[0] => Array
(
[filename] => 1394462124.jpg
[0] => 1394462124.jpg
)
)
)
[1] => Array
(
[details] => Array
(
[product_id] => 2
[0] => Array
(
[field_name] => 犬種
[field] => ペã‚ニーズ
)
[1] => Array
(
[field_name] => 生年月日
[field] => 2013年12月13日
)
[2] => Array
(
[field_name] => 性別
[field] => オス
)
[3] => Array
(
[field_name] => ä¾¡æ ¼
[field] => ¥88,000
)
)
[image] => Array
(
[0] => Array
(
[filename] => 1394462181.jpg
[0] => 1394462181.jpg
)
)
)
i will fetch the [1] => Array inside the
i can't get the [1] => Array using foreach on my view what is can i get the value on array[1]
If you just want to loop through and deal with each dataset, the following code will do that:
foreach ($arr as $key => $data) {
// Output data
// The first row will be $arr[0], and the second $arr[1], etc…
}
-- Original answer
Assuming you mean the second array within the top-level array and that you only want to access the second element in that array (index 1), you should just be able to access it as follows:
$arr[1]
eg:
$second_photo = $arr[1];
Where $arr is the variable name you've assigned to the array you've var_dump()ed above.
That said, your question is ambiguous and not very clear. Can you please provide more information as to what you are actually trying to achieve? (Loop though and display all instances, for example?)