Get values from multiple arrays in PHP - php

This is my array which is coming from a foreach loop:
Array
(
[count] => 1
[0] => Array
(
[distinguishedname] => Array
(
[count] => 1
[0] => CN=Administratie,OU=Test,DC=stefan,DC=nl
)
[0] => distinguishedname
[count] => 1
[dn] => CN=Administratie,OU=Test,DC=stefan,DC=nl
)
)
Array
(
[count] => 1
[0] => Array
(
[distinguishedname] => Array
(
[count] => 1
[0] => CN=Anderemailgroep,OU=Test,DC=stefan,DC=nl
)
[0] => distinguishedname
[count] => 1
[dn] => CN=Anderemailgroep,OU=Test,DC=stefan,DC=nl
)
)
Array
(
[count] => 1
[0] => Array
(
[distinguishedname] => Array
(
[count] => 1
[0] => CN=Beheergroep,OU=Test,DC=stefan,DC=nl
)
[0] => distinguishedname
[count] => 1
[dn] => CN=Beheergroep,OU=Test,DC=stefan,DC=nl
)
)
Array
(
[count] => 1
[0] => Array
(
[distinguishedname] => Array
(
[count] => 1
[0] => CN=Belangrijke Groep,OU=Test,DC=stefan,DC=nl
)
[0] => distinguishedname
[count] => 1
[dn] => CN=Belangrijke Groep,OU=Test,DC=stefan,DC=nl
)
)
Array
(
[count] => 1
[0] => Array
(
[distinguishedname] => Array
(
[count] => 1
[0] => CN=Hoofdgroep,OU=Test,DC=stefan,DC=nl
)
[0] => distinguishedname
[count] => 1
[dn] => CN=Hoofdgroep,OU=Test,DC=stefan,DC=nl
)
)
Array
(
[count] => 1
[0] => Array
(
[distinguishedname] => Array
(
[count] => 1
[0] => CN=Mailgroep,OU=Test,DC=stefan,DC=nl
)
[0] => distinguishedname
[count] => 1
[dn] => CN=Mailgroep,OU=Test,DC=stefan,DC=nl
)
)
Array
(
[count] => 1
[0] => Array
(
[distinguishedname] => Array
(
[count] => 1
[0] => CN=Testgroep2,OU=Test,DC=stefan,DC=nl
)
[0] => distinguishedname
[count] => 1
[dn] => CN=Testgroep2,OU=Test,DC=stefan,DC=nl
)
)
The question is, how do I get all the CN="GroupName" values from all the arrays in a list or something? It only needs to grab that value from every single array and display it in a list.
For example:
I only want this value from every array.
And the output should be like this:
Administratie
Anderemailgroep
Beheergroep
Belangrijke Groep
Hoofdgroep
Mailgroep
Testgroep2
EDIT
The array is coming from this piece of code:
$result = $adldap->user()->groups('test.user');
for ($i=0;$i<count($result);$i++) {
sort($result);
}
print_r($result);
foreach ($result as $key => $value) {
$check = $adldap->group()->info($value, array(
'distinguishedname'
));
if (strpos($check[0]['distinguishedname'][0], 'OU=Test') !== false) {
unset($result[$key]);
print_r($check);
}
}

Lets say your array is $arrs which contains of your array values given.
You can do like this.
<?php
foreach($arrs as $arr) {
foreach($arr as $ar) {
$sep = explode(',',$ar['dn']);
echo explode('=',$sep[0])[1];
}
}
?>

Related

php - Issue with extracting array values

I'm getting all the HTML post values in the $postdata variable.
$postdata = $this->input->post();
unset($postdata['submit']);
unset($postdata['valve_no']);
Output of $postdata:
Array (
[1] => Array (
[0] => BH123
[1] => H89
)
[2] => Array (
[0] => BH123
[1] => H89
)
[3] => Array (
[0] => BH123
[1] => H89
)
[4] => Array (
[0] => BH123
)
)
$valve_no=$this->input->post('valve_no');
Output of $valve_no:
Array (
[0] => 1
[1] => 2
[2] => 3
[3] => 4
)
Next is I'm trying to merge both arrays
foreach($postdata as $key => $val)
{
$dataSet[] = array ('valve_no'=>$valve_no[$key-1],$postdata[$key]);
}
print_r($dataSet);
Output of $dataSet:
Array (
[0] => Array (
[valve_no] => 1
[0] => Array (
[0] => BH123
[1] => H89
)
)
[1] => Array (
[valve_no] => 2
[0] => Array (
[0] => BH123
[1] => H89
)
)
[2] => Array (
[valve_no] => 3
[0] => Array (
[0] => BH123
[1] => H89
)
)
[3] => Array (
[valve_no] => 4
[0] => Array (
[0] => BH123
)
)
)
The output I'm expecting is below:
Array (
[0] => Array (
[valve_no] => 1
[1] => BH123
[2] => H89
)
[1] => Array (
[valve_no] => 2
[1] => BH123
[2] => H89
)
[2] => Array (
[valve_no] => 3
[1] => BH123
[2] => H89
)
[3] => Array (
[valve_no] => 4
[1] => BH123
)
)
)
As you can see in the expected output I want to extract the sub-array and need to start with [1] instead of [0].
Thanks in advance.
Just change foreach() code like this: (As per your comment)
foreach($postdata as $key => $val)
{
$dataSet[$key-1]['valve_no'] = $valve_no[$key-1];
foreach($val as $k=>$v){
$dataSet[$key-1][$k+1] =$v;
}
}
print_r($dataSet);
Output: https://3v4l.org/P8NKs
Note: In case $postdata sub-array indexes not start with 0,1,2... and still you want them to start with 1,2,... in your result, then do like below:
foreach($postdata as $key => $val)
{
$dataSet[$key-1]['valve_no'] = $valve_no[$key-1];
$srNo = 1;
foreach($val as $v){
$dataSet[$key-1][$srNo] =$v;
$srNo++;
}
}
Output: https://3v4l.org/sLVv5

How to unset specific array in nested foreach?

I have an output array like this:
Array
(
[0] => Array
(
[item] => null
[count] => 0
[child] => Array
(
[Dagadu Bocah] => Array
(
[item] => Dagadu Bocah
[count] => 47
[child] => Array
(
[HirukPikuk] => Array
(
[item] => HirukPikuk
[count] => 5
[child] => Array
(
[DGD] => Array
(
[item] => DGD
[count] => 1
[child] =>
)
)
)
[DGD] => Array
(
[item] => DGD
[count] => 5
[child] => Array
(
[Malioboroman] => Array
(
[item] => Malioboroman
[count] => 1
[child] =>
)
)
)
[Malioboroman] => Array
(
[item] => Malioboroman
[count] => 2
[child] =>
)
)
)
)
)
)
in my expectations I can use the loop as I asked earlier in this question by doing repetitions in such a way as to delete certain arrays to eliminate different parts of the array which is an array that is above the array which has three items namely 'item', 'count' and 'child' and how to produce arrays like this from the array above?
Array
(
[0] => Array
(
[item] => null
[count] => 0
[child] => Array
(
[0] => Array
(
[item] => Dagadu Bocah
[count] => 47
[child] => Array
(
[0] => Array
(
[item] => HirukPikuk
[count] => 5
[child] => Array
(
[0] => Array
(
[item] => DGD
[count] => 1
[child] =>
)
)
)
[1] => Array
(
[item] => DGD
[count] => 5
[child] => Array
(
[Malioboroman] => Array
(
[item] => Malioboroman
[count] => 1
[child] =>
)
)
)
[2] => Array
(
[item] => Malioboroman
[count] => 2
[child] =>
)
)
)
)
)
)
It seems you want to convert the child arrays from associative arrays to indexed arrays.
Assuming your data is called $data, here is a recursive function you could use:
function convert(&$data) {
foreach ($data as $row) {
if(isset($row["child"])) {
$row["child"] = array_values($row["child"]);
convert($row["child"]);
}
}
}
// call it for each row
foreach($data as $row) convert($row);

Compare values of two multidimentional array and insert if not exits

I have two array $array1 and $array2 which I get dynamically and look like
$array1 = Array
(
[0] => Array
(
[hour] => 10
[activity] => Array
(
[0] => Array
(
[activity_id] => 1
[cnt] => 2
)
[1] => Array
(
[activity_id] => 2
[cnt] => 1
)
)
)
[1] => Array
(
[hour] => 11
[activity] => Array
(
)
)
[2] => Array
(
[hour] => 12
[percentage] => 0
[activity] => Array
(
[0] => Array
(
[activity_id] => 2
[cnt] => 5
)
[1] => Array
(
[activity_id] => 3
[cnt] => 2
)
)
)
);
$array2 = Array
(
[0] => Array
(
[id] => 1
[name] => Phone Calls
[readable] => 1
[status] => active
)
[1] => Array
(
[id] => 2
[name] => Meeting With Customer
[readable] => 1
[status] => active
)
[2] => Array
(
[id] => 3
[name] => Others Works
[readable] => 1
[status] => active
)
);
which i need to compare.
if $array2['id'] is not in $array1["activity"](i.e"activity_id") add array ['activity_id'=>$array2['id'],'cnt'=>0] to $array1['activity'].
My result must be like
$result = Array
(
[0] => Array
(
[hour] => 10
[activity] => Array
(
[0] => Array
(
[activity_id] => 1
[cnt] => 2
)
[1] => Array
(
[activity_id] => 2
[cnt] => 1
)
[2] => Array
(
[activity_id] => 3
[cnt] => 0
)
)
)
[1] => Array
(
[hour] => 11
[activity] => Array
(
[0] => Array
(
[activity_id] => 1
[cnt] => 0
)
[1] => Array
(
[activity_id] => 2
[cnt] => 0
)
[2] => Array
(
[activity_id] => 3
[cnt] => 0
)
)
)
[2] => Array
(
[hour] => 12
[percentage] => 0
[activity] => Array
(
[0] => Array
(
[activity_id] => 1
[cnt] => 0
)
[1] => Array
(
[activity_id] => 2
[cnt] => 5
)
[2] => Array
(
[activity_id] => 3
[cnt] => 2
)
)
)
);
What i have tried is
$finalArray = array();
foreach($array1 as $arr1) {
foreach($array2 as $arr2) {
if(!in_array($arr2['id'], $arr1['activity'])) {
$array = ['activity_id'=>$arr2['id'], 'cnt'=>0];
}
array_push($arr1['activity'], $array);
unset($array);
}
array_push($finalArray, $result);
}
print_r($finalArray);
in_array() function is not working as I excepted or I am trying to do it in the wrong way. Can someone helps me with this?
Sorry,finally i get what i did wrong.May be someone get helped.
everything is ok just change the line
if(!in_array($arr2['id'], $arr1['activity'])) {
into
if(!in_array( $readActivity['id'], array_column($result['activity'],'activity_id'))){

Arrange an array by weeks

I have an array in which I have field called date and what I need is to separate all these arrays into weeks. Is it posible to do so? Here is my code:
function getWeeks($query){
$postdate = $query['response']['posts']['date'];
return $posts;
}
Here is part of my array:
Array ( [date] => 07/30/12 [message] => test [post_id] => 1 [impressions] => Array ( [0] => 9638 ) [consumptions] => Array ( [0] => 38 ) [storytellers] => Array ( [0] => 6 ) [engaged_users] => Array ( [0] => 31 ) [story_adds] => Array ( [0] => 6 ) [impressions_unique] => Array ( [0] => 4700 ) [comment] => Array ( [0] => 1 ) [like] => Array ( [0] => 5 ) [share] => Array ( [0] => 0 ) [virality] => Array ( [0] => 0 ) [lifetime] => Array ( [0] => 0 ) [affinity] => Array ( [0] => 0 ) )
Array ( [date] => 07/30/12 [message] => test2 [post_id] => 2 [impressions] => Array ( [0] => 10552 ) [consumptions] => Array ( [0] => 47 ) [storytellers] => Array ( [0] => 5 ) [engaged_users] => Array ( [0] => 44 ) [story_adds] => Array ( [0] => 5 ) [impressions_unique] => Array ( [0] => 4982 ) [comment] => Array ( [0] => 0 ) [like] => Array ( [0] => 4 ) [share] => Array ( [0] => 1 ) [virality] => Array ( [0] => 0 ) [lifetime] => Array ( [0] => 0 ) [affinity] => Array ( [0] => 0 ) )
This will loop through each of your post items and group them together if they are in the same week.
View an Example
<?php
$posts = array(
array('date' => '7/30/10', 'title' => 'july post'),
array('date' => '7/19/10', 'title' => 'another post in july'),
array('date' => '7/22/10', 'title' => 'sup, this will be with another post')
);
$grouped_posts = array();
foreach( $posts as $post ) {
// #see http://us2.php.net/manual/en/function.date.php
$week = date('W', strtotime($post['date']));
// create new empty array if it hasn't been created yet
if( !isset($grouped_posts[$week]) ) {
$grouped_posts[$week] = array();
}
// append the post to the array
$grouped_posts[$week][] = $post;
}
print_r($grouped_posts);

array difference

I have this array lets call it array 1
Array
(
[0] => Array
(
[Machine] => Array
(
[id] => 7
[name] => XYZ
[priority] => 1
)
[Software] => Array
(
[id] => 472
)
)
[1] => Array
(
[Machine] => Array
(
[id] => 6
[name] => ABC
[priority] => 0
)
[Software] => Array
(
[id] => 470
)
)
[2] => Array
(
[Machine] => Array
(
[id] => 1
[name] => IEU
[priority] => 3
)
[Software] => Array
(
[id] => 471
)
)
)
Then I have another array lets call it array 2
Array
(
[0] => 7
[1] => 5
[2] => 4
[3] => 3
[4] => 6
)
If array 2 doesnt have [Machine][id] then I want it to be removed from array 1. Like in above example 1 will removed
[2] => Array
(
[Machine] => Array
(
[id] => 1
[name] => IEU
[priority] => 3
)
[Software] => Array
(
[id] => 471
)
)
any idea on how to achieve that. Thanks
Perhaps..
foreach ($array1 AS $key => $array) {
if (!in_array($array['Machine']['id'], $array2))
unset($array1[$key]);
}
try something like :
$new_array = array();
foreach ($array1 as $platform)
{
if (in_array($platform["Machine"]["id"], $array2))
{
$new_array[] = $platform;
}
}
return $new_array;

Categories