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);
Related
Below is my array,
Array
(
[0] => Array
(
[topic_title] => gff
[title] => gff
)
[1] => Array
(
[topic_title] => new toipic
[title] => new toipic
)
[2] => Array
(
[topic_title] => Welcome!
[title] =>Welcome!
)
[3] => Array
(
[entry_title] => sdasdad
[title] => sdasdad
)
[4] => Array
(
[event_title] => red
[title] => red
)
[5] => Array
(
[event_title] => sa
[title] => sa
)
[6] => Array
(
[event_title] => sadasd
[title] => sadasd
)
[7] => Array
(
[event_title] => Test Event
[title] => Test Event
)
)
I want the output like below
Array(
[0] => Array
(
[event_title] => Test Event
[title] => Test Event
)
[1] => Array
(
[event_title] => Welcome!
[title] => Welcome!
)
[2] => Array
(
[event_title] => sa
[title] => sa
)
[3] => Array
(
[entry_title] => sdasdad
[title] => sdasdad
)
[4] => Array
(
[event_title] => sadasd
[title] => sadasd
)
[5] => Array
(
[event_title] => red
[title] => red
)
[6] => Array
(
[topic_title] => new toipic
[title] => new toipic
)
[7] => Array
(
[topic_title] => gff
[title] => gff
)
)
This is the print code from PHP 5.6.
I uses usort() function but didn't get required one. I am not able to sort array in assenting or descending order by their title key.
This array I am getting from different queries an d after that I merge it in one array but not able to sort it by title value
Try this :
$array = Array ( Array("topic_title" => "aatoipic","title" => "new toipic"),Array("event_title" => "Test Event","title" => "Test Event"),Array("topic_title" => "Welcome!", "title" => "Welcome!"),Array("entry_title" => "sdasdad","title" => "p",),Array("event_title" => "red","title" => "k",),Array("event_title" => "sa","title" => "sa"),Array("event_title" => "sadasd","title" => "d"),Array("topic_title" => "gff", "title" => "gff"),Array( "topic_title" => "new toipic","title" => "h"));
$arr = $array;
$sort = array();
foreach($arr as $k=>$v) {
$sort['title'][$k] = $v['title'];
}
array_multisort($sort['title'], SORT_ASC, $arr);
echo "<pre>";
print_r($arr);
Output will come :
Array ( [0] => Array ( [event_title] => Test Event [title] => Test Event ) [1] => Array ( [topic_title] => Welcome! [title] => Welcome! ) [2] => Array ( [event_title] => sadasd [title] => d ) [3] => Array ( [topic_title] => gff [title] => gff ) [4] => Array ( [topic_title] => new toipic [title] => h ) [5] => Array ( [event_title] => red [title] => k ) [6] => Array ( [topic_title] => aatoipic [title] => new toipic ) [7] => Array ( [entry_title] => sdasdad [title] => p ) [8] => Array ( [event_title] => sa [title] => sa ) )
You can use array_multisort() to achieve the desired result.
E.g
$title = array();
foreach ($post as $key => $row)
{
$title[$key] = $row['title'];
}
array_multisort($title, SORT_DESC, $post);
You need to use usort with a strcasecmp:
function querySort ($x, $y) {
return strcasecmp($x['title'], $y['title']);
}
usort($myArray, 'querySort');
usort will do the trick:
usort($array, function($x, $y) {
return strcasecmp($x['title'], $y['title']);
});
I have this array with information, which I want to create a new associative array with. Each key in the associative array should be the "name" from the old one. And in each new key, I want the corresponding information to be collected.
Array
(
[0] => Array
(
[id] => 1
[counter] => 21478813
[serie] => 2607171234
[name] => Ben
)
[1] => Array
(
[id] => 2
[counter] => 21478858
[serie] => 2607177151
[name] => Evan
)
[2] => Array
(
[id] => 3
[counter] => 21478817
[serie] => 2607171341
[name] => Steve
)
[3] => Array
(
[id] => 4
[counter] => 21471798
[serie] => 2607178561
[name] => Ben
)
[4] => Array
(
[id] => 5
[counter] => 21478811
[serie] => 2607171347
[name] => Ben
)
)
This is the array I'm trying to create:
Array
(
["Ben"] => Array
(
[0] => Array
(
[id] => 1
[counter] => 21478813
[serie] => 2607171234
[name] => Ben
)
[1] => Array
(
[id] => 4
[counter] => 21471798
[serie] => 2607178561
[name] => Ben
)
[2] => Array
(
[id] => 5
[counter] => 21478811
[serie] => 2607171347
[name] => Ben
)
)
["Evan"] => Array
(
[0] => Array
(
[id] => 2
[counter] => 21478858
[serie] => 2607177151
[name] => Evan
)
)
["Steve"] => Array
(
[0] => Array
(
[id] => 3
[counter] => 21478817
[serie] => 2607171341
[name] => Steve
)
)
)
$newArr = array();
foreach($myArr as $value) {
$name = $value['name'];
if (isset($newArr[$name])) {
$newArr[$name][] = $value;
}
else {
$newArr[$name] = array($value);
}
}
Use a foreach loop to create a new array:
$newArr = [];
foreach($myArr as $key => $value){
$newArr[$myArr[$key][$value['name']]][] = $myArr[$key];
}
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.
Array
(
[0] => Array
(
[id] => 123-456-000-000
[name] => john
)
[1] => Array
(
[id] => 123-456-789-014
[name] => james
)
)
Array
(
[0] => Array
(
[id] => 123-456-000-000
[address] => Japan
)
[1] => Array
(
[tin] => 123-456-789-014
[address] => Spain
)
)
I have 2 array above and I want to combine them so i used array-merge. But I didnt get the result i want. What it gave me is
Array
(
[0] => Array
(
[id] => 123-456-000-000
[name] => john
)
[1] => Array
(
[id] => 123-456-789-014
[name] => james
)
[2] => Array
(
[tin] => 123-456-000-000
[address] => Japan
)
[3] => Array
(
[tin] => 123-456-789-014
[address] => Spain
)
)
What i wanted is
Array
(
[0] => Array
(
[id] => 123-456-000-000
[name] => john
[address] => Japan
)
[1] => Array
(
[id] => 123-456-789-014
[name] => james
[address] => Spain
)
)
How to achieve this kind of array merge
Assume both array have the same number of elements and in the same order, then one foreach loop can do the job
$arr1 = Array (
0 => Array (
"id" => "123-456-000-000",
"name" => "john"
),
1 => Array (
"id" => "123-456-789-014",
"name" => "james"
)
);
$arr2 = Array (
0 => Array (
"tin" => "123-456-000-000",
"address" => "Japan"
),
1 => Array (
"tin" => "123-456-789-014",
"address" => "Spain"
)
);
foreach ($arr2 as $key => $inner) {
$arr1[$key]["address"] = $inner["address"];
}
output: $arr1
Array
(
[0] => Array
(
[id] => 123-456-000-000
[name] => john
[address] => Japan
)
[1] => Array
(
[id] => 123-456-789-014
[name] => james
[address] => Spain
)
)
Supposing that your arrays are at the same size and that your IDs are ordered int he same way.
Then you can try:
$arrayReturn = array();
foreach( $array1 as $key1 => $result1 ){
$mergeArray = [$result1['id'], $result1['name'], $array2[$key1]['name']];
array_push($arrayReturn, $mergeArray);
}
Where $array1 and $array2 are both of my initial arrays. $arrayReturn is my result.
Is there no other ways for you to directly merge the arrays at their creation? It would be much more elegant and will avoid all that.
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);