Bind array with same ID - php

I have an PHP array like this:
Array
(
[0] => Array
(
[option_id] => 21
[header_image] => logo.png
)
[1] => Array
(
[option_id] => 21
[menu1] => About
)
[2] => Array
(
[option_id] => 22
[menu2] => Speaker
)
[3] => Array
(
[option_id] => 22
[menu3] => Agenda
)
[4] => Array
(
[option_id] => 22
[menu4] => Venue
)
[5] => Array
(
[option_id] => 23
[menu5] => Hotel
)
[6] => Array
(
[option_id] => 23
[menu6] => Sponsors
)
)
I want array like this:
Array
(
[0] => Array
(
[option_id] => 21
[header_image] => logo.png
[menu1] => About
)
[1] => Array
(
[option_id] => 22
[menu2] => Speaker
[menu3] => Agenda
[menu4] => Venue
)
[2] => Array
(
[option_id] => 23
[menu5] => Hotel
[menu6] => Sponsors
)
)
I want to bind array having same option_id.
How can I achieve this?

You need to loop over the array with foreach
<?php
$output = array();
if (! empty($arr)) {
foreach ($arr as $elem) {
if (! empty($elem)) {
foreach ($elem as $k => $v) {
$output[$elem['option_id']][$k] = $v;
}
}
}
}

Related

how to make incremental value in hierarchal array in Php?

Hi guys I was wondering how can I add a incremental all elements? Because as of now I am not sure where can I include the "inc" in all elements
Ex:
MY_ARRAY = (
[id] => 4
[children] => Array
(
[0] => Array
(
[id] => 18
[children] => Array
(
[0] => Array
(
[id] => 21
)
[1] => Array
(
[id] => 22
)
)
)
[1] => Array
(
[id] => 19
)
[2] => Array
(
[id] => 20
[children] => Array
(
[0] => Array
(
[id] => 26
)
)
)
)
)
Using these code:
$in = MY_ARRAY
function generateArray($in, $parent = 0){
foreach ($in as $key => $value) {
if(is_numeric($key)){
$in = $value;
$out[$key] = $this->generateArray($in, $parent);
}else{
$out[$key]=$value;
if($key=="id"){
$out['p_id'] = $parent;
$parent=$value;
}elseif($key=="children"){
$in = $value;
$out[$key] = $this->generateArray($in, $parent);
}
}
}
return $out;
}
Will give me this output, not including the [inc].
[id] => 4
[P_id] => 0
[inc] => 1
[children] => Array
(
[0] => Array
(
[id] => 18
[P_id] => 4
[inc] => 2
[children] => Array
(
[0] => Array
(
[id] => 21
[P_id] => 18
[inc] => 3
)
[1] => Array
(
[id] => 22
[P_id] => 18
[inc] => 4
)
)
)
[1] => Array
(
[id] => 19
[P_id] => 4
[inc] => 5
)
[2] => Array
(
[id] => 20
[P_id] => 4
[inc] => 6
[children] => Array
(
[0] => Array
(
[id] => 26
[P_id] => 20
[inc] => 7
)
)
)
)
)
Now I'm not sure where and how can I include the [inc] or the incremental value of each element in the array using my code above.
Need really help here guys...

Sort array based on count of any value

I have an array which looks like
Array
(
[0] => Array
(
[id] => 39662
[points] => 24
[subject] => 112
)
[1] => Array
(
[id] => 39609
[points] => 24
[subject] => 87
)
[2] => Array
(
[id] => 39610
[points] => 23
[subject] => 77
)
[3] => Array
(
[id] => 39608
[points] => 23
[subject] => 87
)
[4] => Array
(
[id] => 39606
[points] => 22
[subject] => 60
)
[5] => Array
(
[id] => 39604
[points] => 19
[subject] => 75
)
[6] => Array
(
[id] => 39595
[points] => 18
[subject] => 60
)
[7] => Array
(
[id] => 39605
[points] => 18
[subject] => 47
)
[8] => Array
(
[id] => 39650
[points] => 17
[subject] => 87
)
[9] => Array
(
[id] => 39660
[points] => 17
[subject] => 55
)
)
Now I want to sort then based on count of key subject. You can see that subjuet = 87 have 3 records and subject = 60 has two records, so all three records of 87 should display first , after this records of 60 , then others.
I tried array_multisort but its not giving expected result.
Thanks
As per your desired output, you just need to array_map() with array_multisort(),
Example:
<?php
// Test Array
$array = array(
array('id'=>39662,'points'=>'24','subject'=>112),
array('id'=>39609,'points'=>'24','subject'=>87),
array('id'=>39610,'points'=>'23','subject'=>77),
array('id'=>39608,'points'=>'23','subject'=>87),
array('id'=>39606,'points'=>'22','subject'=>60),
array('id'=>39604,'points'=>'19','subject'=>75),
array('id'=>39595,'points'=>'18','subject'=>60),
array('id'=>39605,'points'=>'18','subject'=>47),
array('id'=>39650,'points'=>'17','subject'=>87),
array('id'=>39660,'points'=>'17','subject'=>55),
);
$newArr = array(); // initialize the new Array
foreach ($array as $key => $value)
{
$newArr[$value['subject']][] = $value;
}
array_multisort(array_map('count', $newArr), SORT_DESC, $newArr); // using array_multisort() and Sort as DESC order by using array_map()
echo "<pre>";
print_r($newArr);
?>
Result:
Array
(
[0] => Array
(
[0] => Array
(
[id] => 39609
[points] => 24
[subject] => 87
)
[1] => Array
(
[id] => 39608
[points] => 23
[subject] => 87
)
[2] => Array
(
[id] => 39650
[points] => 17
[subject] => 87
)
)
[1] => Array
(
[0] => Array
(
[id] => 39606
[points] => 22
[subject] => 60
)
[1] => Array
(
[id] => 39595
[points] => 18
[subject] => 60
)
)
[2] => Array
(
[0] => Array
(
[id] => 39604
[points] => 19
[subject] => 75
)
)
[3] => Array
(
[0] => Array
(
[id] => 39605
[points] => 18
[subject] => 47
)
)
[4] => Array
(
[0] => Array
(
[id] => 39610
[points] => 23
[subject] => 77
)
)
[5] => Array
(
[0] => Array
(
[id] => 39660
[points] => 17
[subject] => 55
)
)
[6] => Array
(
[0] => Array
(
[id] => 39662
[points] => 24
[subject] => 112
)
)
)
Try the following method for your case
$data[] = array('points' => 67, 'subject' => 2);
$data[] = array('points' => 86, 'subject' => 1);
$data[] = array('points' => 85, 'subject' => 6);
$data[] = array('points' => 98, 'subject' => 2);
$data[] = array('points' => 86, 'subject' => 6);
$data[] = array('points' => 67, 'subject' => 7);
// Obtain a list of columns
foreach ($data as $key => $row) {
$subject[$key] = $row['subject'];
}
// Sort the data with volume descending, edition ascending
// Add $data as the last parameter, to sort by the common key
array_multisort($subject, SORT_DESC, $data);
Example usage with my array from the php manual.
Output of the above was
Array
(
[0] => Array
(
[points] => 67
[subject] => 7
)
[1] => Array
(
[points] => 85
[subject] => 6
)
[2] => Array
(
[points] => 86
[subject] => 6
)
[3] => Array
(
[points] => 67
[subject] => 2
)
[4] => Array
(
[points] => 98
[subject] => 2
)
[5] => Array
(
[points] => 86
[subject] => 1
)
)
function array_sort($array, $key){
$new_array = array();
$sortable_array = array();
if (count($array) > 0) {
foreach ($array as $k => $v) {
if (is_array($v)) {
foreach ($v as $k2 => $v2) {
if ($k2 == $key) {
$sortable_array[$k] = $v2;
}
}
} else {
$sortable_array[$k] = $v;
}
}
asort($sortable_array);
}
foreach ($sortable_array as $k => $v) {
$new_array[$k] = $array[$k];
}
}
return $new_array;
}
array_sort($a, 'subjects');
//$a is array which want to sort

How to fetch array in correct order

How to fetch array in correct order ?
I am printing a array but it is not fetching in correct order.
I want that each track value contain array of date and array of
number respectively. please give a solution .Any help would be
highly appreciated.
My code for fetch array are:
<?php
foreach($posts as $post)
{
$array['track_value'][] = $post->track_value;
$array['track_value']['date'][] = $post->date;
$array['track_value']['num'][] = $post->num;
}
?>
From this i am getting wrong value like these:
<?php
Array (
[track_value] => Array
(
[0] => mobile
[date] => Array
(
[0] => 2015-08-23
[1] => 2015-08-24
[2] => 2015-08-23
[3] => 2015-08-24
[4] => 2015-08-22
[5] => 2015-08-23
[6] => 2015-08-24
)
[num] => Array
(
[0] => 1
[1] => 1
[2] => 1
[3] => 2
[4] => 1
[5] => 1
[6] => 1
)
[1] => mobile
[2] => laptop
[3] => laptop
[4] => pc
[5] => pc
[6] => pc
)
)
?>
The output should be like:
<?php
Array (
[track_value] => Array
(
[0] => mobile
Array
(
[date] => Array
(
[0] => 2015-08-23
[1] => 2015-08-24
)
[num] => Array
(
[0] => 1
[1] => 1
)
)
[1] => laptop
Array
(
[date] => Array
(
[0] => 2015-08-23
[1] => 2015-08-24
)
[num] => Array
(
[0] => 1
[1] => 2
)
)
[2] => pc
Array
(
[date] => Array
(
[0] => 2015-08-23
[1] => 2015-08-24
[2] => 2015-08-23
)
[num] => Array
(
[0] => 1
[1] => 1
[2] => 1
)
)
)
)
?>
I'm not sure what you are trying to do, but maybe this will help you on the way:
foreach($posts as $post)
{
$array['track_value'][$post->track_value]['date'][] = $post->date;
$array['track_value'][$post->track_value]['num'][] = $post->num;
}
This will give you an output like:
Array(
'track_value' => Array(
'mobile' => Array(
'date' => Array(
0 => '2015-08-25',
1 => '2015'08'26',
),
'num' => Array(
0 => 1337,
1 => 13337,
),
),
'pc' => ...
),
)
Try this:
$arr = [];
foreach($posts as $key => $val)
$arr['track_value'][$val->track_value][] = ['date' => $val->date, 'num' => $val->num];
}
print_r($arr);
Try this:
$tracks = [];
foreach($posts as $k1 as $v1) {
if (is_array($value)){
foreach ($value as $k2 => $v2) {
$tracks[$k1][$k2] = $v2;
}
}
$tracks[$k1] = $v1;
}

array search for a key=>value

I have an array,
$arr=(
[0] => Array
(
[groupid] => 1
[groupname] => Red
[members] => Array
(
[0] => Array
(
[mid] => 9
[name] => Anith
)
[1] => Array
(
[mid] => 11
[name] => Aravind
)
[2] => Array
(
[mid] => 10
[name] => Lekshmi
)
)
)
[1] => Array
(
[groupid] => 2
[groupname] => Blue
[members] => Array
(
[0] => Array
(
[mid] => 6
[name] => Yamuna
)
[1] => Array
(
[mid] => 2
[name] => Kamala K
)
[2] => Array
(
[mid] => 13
[name] => Sooraj K
)
)
)
I want to check [mid] => 2 is in the array..If it exists
I want to delete it(ie. unset the array )-----
[1] => Array
(
[mid] => 2
[name] => Kamala K
)
;;;
eg:--unset($arr[1]['members'][2];
This should do the trick
foreach ($arr as $group => $subarray) {
foreach ($subarray['members'] as $k => $v) {
if ($v['mid'] == 2) {
unset($arr[$group]['members'][$k]);
break;
}
}
}
var_dump($arr);
If you feel like getting crafty, you could do something like this:
// note: requires PHP >= 5.3
foreach ($arr as $key => &$value) {
$value['members'] = array_filter(
$value['members'],
function($member) {
return $member['mid'] != 2;
}
);
}
var_dump($arr);

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