Need help on PHP array filtering - php

I have this array in php, i want to select all casenumber those have dob and then select all links those not have any dob. How can I do that in php
Array
(
[links] => Array
(
[0] => inquiryDetail.jis?caseId=0101SP085622015&loc=3&detailLoc=DV
[1] => inquiryDetail.jis?caseId=0101SP096462015&loc=3&detailLoc=DV
[2] => inquiryDetail.jis?caseId=050200173642014&loc=20&detailLoc=DSCIVIL
[3] => inquiryDetail.jis?caseId=CAL1432003&loc=65&detailLoc=PGV
)
[case_number] => Array
(
[0] => 0101SP085622015
[1] => 0101SP096462015
[2] => 050200173642014
[3] => CAL1432003
)
[persons] => Array
(
[0] => Walker, Rosemary
[1] => Walker, Rosemary
[2] => Walker, Rosemary
[3] => Walker, Rosemary
)
[dob] => Array
(
[0] => 11/1961
[1] => 11/1961
)
[Party_Type] => Array
(
[0] => Defendant
[1] => Defendant
[2] => Defendant
[3] => Defendant
)
[Court] => Array
(
[0] => Baltimore City District Court 1400 North Ave.
[1] => Baltimore City District Court 1400 North Ave.
[2] => Upper Marlboro District Court
[3] => Prince George\'s County Circuit Court
)
[Case_Type] => Array
(
[0] => Domestic Violence
[1] => Domestic Violence
[2] => CONT
[3] => CIVIL
)
[Case_Status] => Array
(
[0] => CLOSE
[1] => CLOSE
[2] => ACTIVE
[3] => ACTIVE
)
[Filing_Date] => Array
(
[0] => 09/04/2015
[1] => 11/25/2015
[2] => 07/24/2014
[3] => 11/18/2014
)
)
Here all are inter connected with keys to each other. please help

This solutuon works if indexes of each subarrays match. If not, you should clarify your question with associations between subarrays.
$array - your input data.
You'll get $result array which contains searched values.
$result = array(
'links_without_dob' => array(),
'case_number_with_dob' => array()
);
foreach($array['dob'] as $k => $v) {
$result['case_number_with_dob'][] = $array['case_number'][$k];
}
foreach($array['links'] as $k => $v) {
if (array_key_exists($k, $array['dob'])) continue;
$result['links_without_dob'][] = $v;
}
I'll be glad to see any suggestions for improving this solution.

Related

Reading the response of Google Analytics php Api

I am trying to get the page views from each country.
I have following code for this purpose.
function getResults($analytics, $profileId,$url) {
// Calls the Core Reporting API and queries for the number of sessions
// for the last seven days.
$optParams = array(
'dimensions' => 'ga:country',
'filters' => 'ga:pagePath=#/605-2/'
);
return $analytics->data_ga->get(
'ga:' . $profileId,
'2017-11-01',
'today',
'ga:pageViews',
$optParams);
}
The code is working fine. The problem is I am not sure how to print the results as a table of form
Country | Views
USA | 52
Australia | 50
Below is the response I managed to filter out
Array ( [0] => Array ( [0] => (not set) [1] => 1 ) [1] => Array ( [0] => Australia [1] => 11 ) [2] => Array ( [0] => Bahrain [1] => 2 ) [3] => Array ( [0] => Belgium [1] => 1 ) [4] => Array ( [0] => Canada [1] => 5 ) [5] => Array ( [0] => Denmark [1] => 1 ) [6] => Array ( [0] => Finland [1] => 1 ) [7] => Array ( [0] => France [1] => 1 ) [8] => Array ( [0] => Germany [1] => 10 ) [9] => Array ( [0] => Hungary [1] => 1 ) [10] => Array ( [0] => Italy [1] => 1 ) [11] => Array ( [0] => Japan [1] => 4 ) [12] => Array ( [0] => Mali [1] => 1 ) [13] => Array ( [0] => Norway [1] => 1 ) [14] => Array ( [0] => Pakistan [1] => 589 ) [15] => Array ( [0] => Peru [1] => 1 ) [16] => Array ( [0] => Saudi Arabia [1] => 13 ) [17] => Array ( [0] => Singapore [1] => 1 ) [18] => Array ( [0] => South Africa [1] => 1 ) [19] => Array ( [0] => South Korea [1] => 2 ) [20] => Array ( [0] => Ukraine [1] => 1 ) [21] => Array ( [0] => United Arab Emirates [1] => 7 ) [22] => Array ( [0] => United Kingdom [1] => 7 ) [23] => Array ( [0] => United States [1] => 10 ) )

How can I traverse a 2D array in having sparse keys to access inner arrays? [duplicate]

This question already has answers here:
How to loop through the following php array?
(2 answers)
Closed 5 years ago.
I've got this 2D array $userDesignatedCategory.
Array (
[1] => Array (
[0] => CEO
[1] => Assistant Art Director
[2] => Assistant Choreographer
[3] => Assistant Creative Director
[4] => Assistant Director
[5] => Assistant Editor
[6] => Assistant Equipment Engineer
[7] => Assistant Hair Dresser
[8] => Assistant Lighting Director
[9] => Assistant Make Up Artist
)
[2] => Array (
[0] => Senior Developer
)
[3] => Array (
[0] => CEO
[1] => Script Supervisor
[2] => Creative Director
[3] => Anchor
[4] => Executive Producer
[5] => Director
[6] => Actor
)
[7] => Array (
[0] => Director
[1] => Executive Producer
[2] => Journalist
[3] => Producer
)
[10] => Array (
[0] => Head of Division
[1] => Vice President
)
[11] => Array (
[0] => Anchor
[1] => Chairman
[2] => Co Founder
[3] => Creative Director
[4] => Director
)
[13] => Array (
[0] => Associate Producer
)
[16] => Array (
[0] => Accounts Manager
)
[20] => Array (
[0] => Adventure Cameraperson
[1] => Cameraperson
[2] => Director Of Photography
[3] => Underwater Cameraperson
)
[21] => Array (
[0] => Director
[1] => Screenplay Writer
[2] => Writer
)
[28] => Array (
[0] => Director
)
[50] => Array (
[0] => Cameraperson
)
[73] => Array (
[0] => Accounts Manager
[1] => Actor
[2] => Aerial Cameraperson
[3] => Anchor
)
[78] => Array (
[0] => Accounts Manager
[1] => Aerial Cameraperson
[2] => Animator
)
[79] => Array (
[0] => Actor
[1] => Anchor
[2] => Adventure Cameraperson
[3] => Aerial Cameraperson
[4] => Animation Director
[5] => Animator
[6] => Assistant Make Up Artist
[7] => Assistant Manager
)
[82] => Array (
[0] => Adventure Cameraperson
[1] => Cameraperson
[2] => Director Of Photography
[3] => Associate Producer
)
[86] => Array (
[0] => Director
[1] => Producer
[2] => Writer
)
[87] => Array (
[0] => Co Founder
[1] => Vice President
)
)
I have sparse keys so I cant use for($i=0;i 'less than' count;$i++)
I've used foreach($userDesignatedCatogery as $key => $value) but it didnt help either.
How can I traverse this array to access inner arrays?
The simple traverse of your array.
<?php
foreach($userDesignatedCatogery as $index => $pull) {
echo '#' . $index . '<br>';
foreach($pull as $id => $position)
echo $id . ' => ' . $position . '<br>';
}
you have two ways:
first one:
$count = count($userDesignatedCategory);
for ($i=0; $i < $count; $i++) {
foreach ($userDesignatedCategory[$i] as $item) {
//some codes here
}
}
second one:
foreach ($userDesignatedCategory as $item) {
foreach ($item as $subitem) {
// some code here
}
}

Replace numeric array keys with associative keys from array

I have a dataset similar to this in which I am trying to replace the numeric key values within DATA to the corresponding values in COLUMNS. I can do this in a loop but I don't think I'm doing it in the most efficient way possible. Can anyone suggest any nice functions that I haven't considered to accomplish this?
Existing Style
stdClass Object
(
[COLUMNS] => Array
(
[0] => MATCHID
[1] => SEASON
[2] => COMPETITION
[3] => ROUNDID
[4] => ROUNDSORT
[5] => ROUNDNAME
)
[DATA] => Array
(
[0] => Array
(
[0] => 141627
[1] => 2013/2014
[2] => The Scottish Cup
[3] => 18
[4] => 11
[5] => Final
)
[1] => Array
(
[0] => 140895
[1] => 2013/2014
[2] => The Scottish Cup
[3] => 16
[4] => 10
[5] => Semi-Final
)
)
)
Desired Style
stdClass Object
(
[COLUMNS] => Array
(
[0] => MATCHID
[1] => SEASON
[2] => COMPETITION
[3] => ROUNDID
[4] => ROUNDSORT
[5] => ROUNDNAME
)
[DATA] => Array
(
[0] => Array
(
[MATCHID] => 141627
[SEASON] => 2013/2014
[COMPETITION] => The Scottish Cup
[ROUNDID] => 18
[ROUNDSORT] => 11
[ROUNDNAME] => Final
)
[1] => Array
(
[MATCHID] => 140895
[SEASON] => 2013/2014
[COMPETITION] => The Scottish Cup
[ROUNDID] => 16
[ROUNDSORT] => 10
[ROUNDNAME] => Semi-Final
)
)
)
foreach ($data->DATA as $key => $array) {
$data->DATA[$key] = array_combine($data->COLUMNS, $array);
}
$data is the object you showed.
Loop trough the data and combine the keys with the data, see array_combine
$data->DATA = array_map(function (array $entry) use ($data) {
return array_combine($data->COLUMNS, $entry);
}, $data->DATA);

PHP : transform a multidimensional array

I search a solution to transform an array to another array. I have this one :
Array
(
[Germany] => Array
(
[0] => Munich
[1] => Frankfurt
)
[France] => Array
(
[0] => Paris
[1] => Marseille
[2] => Lille
[3] => Starsbourg
[4] => Lyon
[5] => Bordeaux
[6] => Toulouse
)
[Spain] => Array
(
[0] => Madrid
[1] => Barcelona
[2] => Valencia
)
)
What is the best way (with array_filter for example, or any other PHP function) to transform it to this please :
Array
(
[0] => Array
(
[value] => Germany
[cities] => Array
(
[0] => Munich
[1] => Frankfurt
)
)
[1] => Array
(
[value] => France
[cities] => Array
(
[0] => Paris
[1] => Marseille
[2] => Lille
[3] => Starsbourg
[4] => Lyon
[5] => Bordeaux
[6] => Toulouse
)
)
[0] => Array
(
[value] => Spain
[cities] => Array
(
[0] => Madrid
[1] => Barcelona
[2] => Valencia
)
)
)
$new_array = array();
foreach ($old_array as $country => $cities)
{
$new_array[] = array(
'value' => $country,
'cities' => $cities
);
}
Should do the job.
You could use array_map:
$newArray = array_map(function($key, $val){
return array(
'value' => $key,
'cities' => $val
);
}, array_keys($oldArray), $oldArray);

Changing key of Multidimensional array in PHP

This is my first array
Array
(
[0] => Array
(
[0] => 1
[1] => Elite
[2] => Air-Con Bus
[3] => Monday
)
[1] => Array
(
[0] => 4
[1] => KBZ
[2] => Airplane
[3] => Wednesday
)
[2] => Array
(
[0] => 5
[1] => Yoma
[2] => Cruise
[3] => Tuesday
)
)
I want to be inner array[0] to the outer array key. Like the following array: Can I or not? Please suggest me.
Array(
[1] => Array
(
[0] => 1
[1] => Elite
[2] => Air-Con Bus
[3] => Monday
)
[4] => Array
(
[0] => 4
[1] => KBZ
[2] => Airplane
[3] => Wednesday
)
[5] => Array
(
[0] => 5
[1] => Yoma
[2] => Cruise
[3] => Tuesday
)
)
$new_array = array();
foreach ($old_array as $el) {
$new_array[$el[0]] = $el;
}
one way:
foreach ($array as $a){
$new[$a[0]]=$a;
}

Categories