Converting Array type in PHP - php

Please Help in conversion of an array from one form to another I Have This Array
Array (
[mpr_last_month] => 376431
[mpr_month] => 03
[total_boys_all_6m_36m] => 5550225
[total_girls_all_6m_36m] => 5215529
[total_boys_all_36m_72m] => 4209639
[total_girls_all_36m_72m] => 4149613
[total_pse_boys_36m_72m] => 4442301
[total_pse_all_girls_36m_72m] => 4413446
[total_pregnanting] => 2209158 )
Array (
[mpr_last_month] => 448216
[mpr_month] => 04
[total_boys_all_6m_36m] => 7153209
[total_girls_all_6m_36m] => 6798913
[total_boys_all_36m_72m] => 5175846
[total_girls_all_36m_72m] => 5105460
[total_pse_boys_36m_72m] => 5290617
[total_pse_all_girls_36m_72m] => 5263340
[total_pregnanting] => 2944612
)
Array (
[mpr_last_month] => 448253
[mpr_month] => 05
[total_boys_all_6m_36m] => 11742417
[total_girls_all_6m_36m] => 6362815
[total_boys_all_36m_72m] => 4879252
[total_girls_all_36m_72m] => 4756805
[total_pse_boys_36m_72m] => 5344042
[total_pse_all_girls_36m_72m] => 5095155
[total_pregnanting] => 2852864
)
Array (
[mpr_last_month] => 470848
[mpr_month] => 06
[total_boys_all_6m_36m] => 6552523
[total_girls_all_6m_36m] => 6217771
[total_boys_all_36m_72m] => 4613019
[total_girls_all_36m_72m] => 4551685
[total_pse_boys_36m_72m] => 5182666
[total_pse_all_girls_36m_72m] => 5165730
[total_pregnanting] => 2746293
)
Array (
[mpr_last_month] => 465489
[mpr_month] => 07
[total_boys_all_6m_36m] => 6638749
[total_girls_all_6m_36m] => 6310676
[total_boys_all_36m_72m] => 4801665
[total_girls_all_36m_72m] => 4657764
[total_pse_boys_36m_72m] => 5020964
[total_pse_all_girls_36m_72m] => 5051785
[total_pregnanting] => 2815773
)
I Want This
name: 'mpr_last_month',
data: [43934, 52503, 57177, 69658, 97031, 119931, 137133, 154175,123,123,123,123]
.
.
.
.
.
.
.
.

Simplest approach would be foreach loop.
$newData = [];
foreach ($yourArray as $innerArray) {
foreach ($innerArray as $key => $value) {
$newData[$key][] = $value;
}
}
It loops over the first array (the big one, containg all others), the runs over each inner array and store the value in the correct place.

You can use array_column function. But be aware that this function works only for PHP >= 5.5.0. Try this:
//example data
$array = [
['a' => 2, 'b'=>3, 'c'=>6],
['a' => 12, 'b'=>13, 'c'=>16],
['a' => 22, 'b'=>23, 'c'=>26],
];
$result = [];
//get indexes from first element of array
$indexes = array_keys($array[0]);
foreach($indexes as $index) {
$result[$index] = array_column($array, $index);
}
And result is:
Array
(
[a] => Array
(
[0] => 2
[1] => 12
[2] => 22
)
[b] => Array
(
[0] => 3
[1] => 13
[2] => 23
)
[c] => Array
(
[0] => 6
[1] => 16
[2] => 26
)
)

Related

Convert 2d array by specified 2d format

I need to convert the below 2d array in to specified 2d array format. Array contains multiple parent and multiple child array. Also, have tried to convert the code, but am not getting the expected output.
This is the code what i have tried,
$a1 = array(
'0' =>
array(
'banner_details' =>
array(
'id' => 2,
'section_id' => 24
),
'slide_details' =>
array(
0 => array(
'id' => 74,
'name' => 'Ads1'
),
1 => array(
'id' => 2,
'name' => 'Ads2'
)
)
),
'1' =>
array(
'banner_details' =>
array(
'id' => 106,
'section_id' => 92
),
'slide_details' =>
array(
0 => array(
'id' => 2001,
'name' => 'Adv1'
),
1 => array(
'id' => 2002,
'name' => 'Adv2'
)
)
)
);
$s = [];
for($i = 0; $i<2; $i++) {
foreach($a1[$i]['slide_details'] as $vs){
$s[] = $vs;
}
}
My output:
Array
(
[0] => Array
(
[id] => 74
[name] => Ads1
)
[1] => Array
(
[id] => 2
[name] => Ads2
)
[2] => Array
(
[id] => 2001
[name] => Adv1
)
[3] => Array
(
[id] => 2002
[name] => Adv2
)
)
Expected output:
Array
(
[24] => Array
(
[0] => 74
[1] => 2
)
[92] => Array
(
[0] => 2001
[1] => 2002
)
)
please check the above code and let me know.
Thanks,
You can apply next simple foreach loop with help of isset() function:
foreach($a1 as $data){
if (isset($data['banner_details']['section_id'])){
$s[$data['banner_details']['section_id']] = [];
if (isset($data['slide_details'])){
foreach($data['slide_details'] as $row){
$s[$data['banner_details']['section_id']][] = $row['id'];
}
}
}
}
Demo
If you know that indexes like banner_details or slide_details or section_id will be there always then you can skip isset() in if statements.
You can use array_column function for simple solution:
$result = [];
foreach ($a1 as $item)
{
$result[$item['banner_details']['section_id']] = array_column($item['slide_details'], 'id');
}
var_dump($result);

empty value of a key if the array has duplicates

i have this array
Array
(
[0] => Array
(
[user_id] => 78
[post_id] => 3
[post_user_added_id] => 2
)
[1] => Array
(
[user_id] => 76
[post_id] => 3
[post_user_added_id] => 16
)
[2] => Array
(
[user_id] => 78
[post_id] => 3
[post_user_added_id] => 12
)
[3] => Array
(
[user_id] => 76
[post_id] => 9
[post_user_added_id] => 15
)
[4] => Array
(
[user_id] => 77
[post_id] => 9
[post_user_added_id] => 15
)
what i want to do is if the key post_id is repeated i just want to empty it and keep one so my final array will look like this
Array
(
[0] => Array
(
[user_id] => 78
[post_id] => 3
[post_user_added_id] => 2
)
[1] => Array
(
[user_id] => 76
[post_id] =>
[post_user_added_id] => 16
)
[2] => Array
(
[user_id] => 78
[post_id] =>
[post_user_added_id] => 12
)
[3] => Array
(
[user_id] => 76
[post_id] => 9
[post_user_added_id] => 15
)
[4] => Array
(
[user_id] => 77
[post_id] =>
[post_user_added_id] => 15
)
i have tried this code but it doesn't seem to work it deletes the whole array
foreach($arry as $k => $v)
{
foreach($arry as $key => $value)
{
if($k != $key && $v['post_id'] == $value['post_id'])
{
unset($arry [$k]);
}
}
}
print_r($arry);
You can perform foreach with ternary operator
$last = null;//this will keep the previous post_id
foreach($arr as &$v){
($last && $last == $v['post_id']) ? ($v['post_id'] = '') : ($last = $v['post_id']);
}
print_r($arr);
Working example :- https://3v4l.org/RiU9J
here try this.
$tempArr = [];
$resArr = [];
foreach($orignalArr as $key=>$obj){
if(in_array($obj['post_id'], $tempArr)){
$obj['post_id'] = '';
$resArr[] = $obj;
}else{
$tempArr[] = $obj['post_id'];
$resArr[] = $obj;
}
}
print_r($resArr);
try this :
// example code
$arrayResult = array();
$arry = [
1 =>
[
'user_id' => 78,
'post_id' => 3,
'post_user_added_id' => 2
],
2=>
[
'user_id' => 76,
'post_id' => 3,
'post_user_added_id' => 16
],
3 =>
[
'user_id' => 78,
'post_id' => 3,
'post_user_added_id' => 12
],
4 =>
[
'user_id' => 76,
'post_id' => 9,
'post_user_added_id' => 15
],
5 =>
[
'user_id' => 77,
'post_id' => 9,
'post_user_added_id' => 15
]];
$final = array();
foreach($arry as $a)
{
if (in_array($a['post_id'], $arrayResult)) {
$a['post_id'] = 0;
}else{
$arrayResult[] = $a['post_id'];
}
$final[] = $a;
}
var_dump($final);
Maybe if you use a stack for comparison?
edit: rewritten the code...
$stack=[];
foreach ($array as $index => $subarray){
if(in_array($subarray['post_id'], $stack)) {
$array[$index]['post_id'] = null;
}
else $stack[]=$subarray['post_id'];
}
print_r($array);
example: https://3v4l.org/FsPUG
Hope this helps!
use of your array value in instead of $arr and try to filter array like the following code :
$arr = array(
array(
'user_id'=>15,
'post_id'=>3,
'post_user_added_id'=>2
),
array(
'user_id'=>16,
'post_id'=>3,
'post_user_added_id'=>2
),
array(
'user_id'=>18,
'post_id'=>3,
'post_user_added_id'=>2
),
array(
'user_id'=>18,
'post_id'=>3,
'post_user_added_id'=>2
),
array(
'user_id'=>16,
'post_id'=>3,
'post_user_added_id'=>2
),
);
$postids = array();
foreach($arr as $key=>$val){
if(in_array($val['post_id'], $postids)){
$arr[$key]['post_id'] = '';
}else{
$postids[] = $val['post_id'];
}
}
echo "<pre>";print_r($arr);exit;
Output :
Array
(
[0] => Array
(
[user_id] => 15
[post_id] => 3
[post_user_added_id] => 2
)
[1] => Array
(
[user_id] => 16
[post_id] =>
[post_user_added_id] => 2
)
[2] => Array
(
[user_id] => 18
[post_id] =>
[post_user_added_id] => 2
)
[3] => Array
(
[user_id] => 18
[post_id] =>
[post_user_added_id] => 2
)
[4] => Array
(
[user_id] => 16
[post_id] =>
[post_user_added_id] => 2
)
)

Merge 2 Arrays on second level with different keys

I need to merge two arrays on the second level with different key names but the same amount of keys.
usera=102
userb=103
My desired output would be array 3.
I have tried a lots of variations to get this together.
My best attempt was something like the following but it destroyed my key names:
$results = array();
foreach($arr1 as $key => $array)
{
foreach($array as $user => $value)
{
$results[$user]['name'] = $value;
}
}
foreach($arr2 as $key => $array)
{
foreach($array as $user => $value)
{
$results[$user]['name2'] = $value;
}
}
Array 1 :
Array
(
[usera] => Array
(
[0] => Array
(
[user] => usera
[duration_s] => 15
)
[1] => Array
(
[user] => usera
[duration_s] => 9
)
)
[userb] => Array
(
[2] => Array
(
[user] => userb
[duration_s] => 21
)
[3] => Array
(
[user] => userb
[duration_s] => 19
)
)
)
Array 2:
Array
(
[102] => Array
(
[0] => Array
(
[user] => 102
[duration_s2] => 54
)
[1] => Array
(
[user] => 102
[duration_s2] => 378
)
)
[103] => Array
(
[2] => Array
(
[usernr] => 103
[duration_s2] => 299
)
[3] => Array
(
[usernr] => 103
[duration_s2] => 110
)
)
)
Array 3:
Array
(
[usera] => Array
(
[0] => Array
(
[user] => usera
[duration_s] => 15
[usernr] => 102
[duration_s2] => 54
)
[1] => Array
(
[user] => usera
[duration_s] => 9
[usernr] => 102
[duration_s2] => 378
)
)
[userb] => Array
(
[2] => Array
(
[user] => userb
[duration_s] => 21
[usernr] => 103
[duration_s2] => 299
)
[3] => Array
(
[user] => userb
[duration_s] => 19
[usernr => 103
[duration_s2] => 110
)
)
)
Try the following code using array_values() :
<?php
$array1 = [
'usera' => [
0=> ['user' => 'usera','duration_s' => 15],
1=> ['user' => 'usera','duration_s' => 9],
],
'userb' => [
2=> ['user' => 'usera','duration_s' => 15],
3=> ['user' => 'usera','duration_s' => 9],
],
];
$array2 = [
102 => [
0=> ['usernr' => 102,'duration_s2' => 54],
1=> ['usernr' => 102,'duration_s2' => 378]
],
103 => [
2=> ['usernr' => 103,'duration_s2' => 299],
3=> ['usernr' => 103,'duration_s2' => 110]
],
];
$array2 = array_values($array2);
$array1 = array_values($array1);
foreach($array1 as $index=>$ar1){
foreach ($ar1 as $index2=>$ar2){
$array1[$index][$index2] = array_merge($ar2,$array2[$index][$index2]);
}
}
print_r($array1);
To merge the array you can also do the following :
$index = 0;
foreach ($arr2 as $key => $value) { // changing the indexing of second array
$arr3[$index] = $value;
$index++;
}
$results = array();
$count = 0;
foreach ($arr1 as $index => $value) { // merging two array
$total = 0;
foreach ($value as $key => $val) {
$merge_value = array_merge($val,$arr3[$count][$total]);
$results[$index][$total] = $merge_value;
$total++;
}
$count++;
}
print_r($results);
In this result your indexing will not lost.

foreach loop is not working after print array

I've tried to loop of following array.
Array
(
[mech_info] => Array
(
[make] => Amaka
[0] => Array
(
[year] => 2001
[model] => Array
(
[0] => Test one
[1] => test fix
[2] => Hamour
[3] => Imagica
)
)
[1] => Array
(
[year] => 2002
[model] => Array
(
[0] => Test Two
)
)
[2] => Array
(
[year] => 2014
[model] => Array
(
[0] => Test three
)
)
[3] => Array
(
[year] => 2015
[model] => Array
(
[0] => test four
)
)
)
)
Array
(
[mech_info] => Array
(
[make] => PRI
[0] => Array
(
[year] => 2005
[model] => Array
(
[0] => PRIMODE
[1] => Temp Pri
[2] => primode
[3] => yyy
)
)
)
)
I want to do it with foreach loop. I have tried by following code but it is not show anything except
`print_r($_POST['mech_show']);`.
$all_make_model= $_POST['mech_show'];
$all_make_model_data = $all_make_model['mech_info'];
foreach ($all_make_model_data as $key => $mec_value) {
echo "string";
echo $meta_value['make'];
}
echo "<pre>";
print_r($_POST['mech_show']);
exit();
also not able to go under foreach and data not print in loop.
given me error
Notice: Undefined index: mech_info
Warning: Invalid argument supplied for foreach() in
i also trie this way but
$all_make_model= $_POST['mech_show'];
$all_make_model_data = $_POST['mech_info'];
foreach ($all_make_model as $key => $mec_value) {
echo "<pre>";
print_r($mec_value['make']);
echo "</pre>";
}
echo "<pre>";
print_r($all_make_model['mech_info']);
but it's showing Warning: Illegal string offset 'mech_info' in ..
I don't know if my code is wrong or I'm missing something anyone pls help me.
Thank You
Some change your foreach loop. it $meta_value['make'] should be $mec_value['make']
So,
$all_make_model= $_POST['mech_show'];
//$all_make_model_data = $all_make_model['mech_info'];
foreach ($all_make_model as $key => $mec_value) {
echo "<pre>";
print_r($mec_value['make']);
echo "</pre>";
}
try
$all_array=array("mech_info"=>array("make"=>"Amaka",array("year"=>2001,"model"=>array("one","two","three")),array("year"=>2002,"model"=>array("one","two","three")),array("year"=>2003,"model"=>array("one","two","three")),array("year"=>2004,"model"=>array("one","two","three"))),array("mech_info"=>array("make"=>"PRI",array("year"=>2001,"model"=>array("one","two","three")))));
$all_make_model= $all_array;
//$all_make_model_data = $all_make_model['mech_info'];
//print_r($all_make_model['mech_info']);
foreach ($all_make_model['mech_info'] as $key => $mec_value) {
if(is_numeric($key)) continue;
echo $mec_value; // output Amaka
}
exit();
This Code just work.
To iterate on multiple mech_info, i added a leave in the array, because otherwise you are tring to create multiple object with the same index.
$p = Array('mech_show' => Array(
0 => Array(
'mech_info' => Array(
'make' => 'Amaka',
'0' => Array(
'year' => 2001,
'model' => Array(
0 => 'Test one',
1 => 'test fix',
2 => 'Hamour',
3 => 'Imagica'
)
),
'1' => Array(
'year' => 2002,
'model' => Array(
0 => 'Test Two'
)
),
'2' => Array(
'year' => 2014,
'model' => Array(
0 => 'Test three'
)
),
'3' => Array(
'year' => 2015,
'model' => Array
(
0 => 'test four'
)
)
)
),
1=>Array(
'mech_info' => Array(
'make' => 'PRI',
'0' => Array(
'year' => 2005,
'model' => Array(
0 => 'PRIMODE',
1 => 'Temp Pri',
2 => 'primode',
3 => 'yyy'
)
)
)
)
)
);
$all_make_model= $p['mech_show'];
foreach($all_make_model as $all_make_model_data){
foreach($all_make_model_data as $mech_info)
var_dump($mech_info['make']);
}
where you have to replace $p with $_POST

Create new array depending on key

My array is like that:
Array
(
[0] => Array
(
[des_id] => 1
[des_name] => bagan
[tran_id] => 1
[tran_name] => private
[tran_image] => 1251961905A1.jpg
[type] => car
[troute_id] => 10
)
[1] => Array
(
[des_id] => 1
[des_name] => bagan
[tran_id] => 2
[tran_name] => express
[tran_image] => bus3.jpg
[type] => car
[troute_id] => 13
)
[2] => Array
(
[des_id] => 1
[des_name] => bagan
[tran_id] => 3
[tran_name] => MyanmarTrain
[tran_image] => Burma-Gorteikviaduct.jpg
[type] => train
[troute_id] => 16
)
[3] => Array
(
[des_id] => 1
[des_name] => bagan
[tran_id] => 4
[tran_name] => Ayeyarwaddy Cruise
[tran_image] => boat-ChutzpahToo1.jpg
[type] => cruise
[troute_id] => 22
)
)
I want to change that array like that depending on key['type']. If array key['type'] are same, I want to change array like that:
Array
(
[car] => Array(
[0]=>Array
(
[des_id] => 1
[des_name] => bagan
[tran_id] => 1
[tran_name] => private
[tran_image] => 1251961905A1.jpg
[type] => car
[troute_id] => 10
),
[1] => Array
(
[des_id] => 1
[des_name] => bagan
[tran_id] => 2
[tran_name] => express
[tran_image] => bus3.jpg
[type] => car
[troute_id] => 13
)
),
[train]=>Array(
[0] => Array
(
[des_id] => 1
[des_name] => bagan
[tran_id] => 3
[tran_name] => MyanmarTrain
[tran_image] => Burma-Gorteikviaduct.jpg
[type] => train
[troute_id] => 16
)
[cruise]=>Array(
[0] => Array
(
[des_id] => 1
[des_name] => bagan
[tran_id] => 4
[tran_name] => Ayeyarwaddy Cruise
[tran_image] => boat-ChutzpahToo1.jpg
[type] => cruise
[troute_id] => 22
)
)
)
)
what I mean is that if key['type'] is car, I want to create car array or if the type is train I want to create train array or if the type is cruise I want to create cruise array. I don't know how to loop the array. Anyone please help me. Thanks a lot!
Here's a simple way to do it: loop over the data, and just append to the subarray matching the type value:
// starting data
$starting_array = array (
0 => array (
'des_id' => 1,
'des_name' => 'bagan',
'tran_id' => 1,
'tran_name' => 'private',
'tran_image' => '1251961905A1.jpg',
'type' => 'car',
'troute_id' => 10
),
1 => array (
'des_id' => 1,
'des_name' => 'bagan',
'tran_id' => 2,
'tran_name' => 'express',
'tran_image' => 'bus3.jpg',
'type' => 'car',
'troute_id' => 13
),
2 => array (
'des_id' => 1,
'des_name' => 'bagan',
'tran_id' => 3,
'tran_name' => 'MyanmarTrain',
'tran_image' => 'Burma-Gorteikviaduct.jpg',
'type' => 'train',
'troute_id' => 16
),
3 => array (
'des_id' => 1,
'des_name' => 'bagan',
'tran_id' => 4,
'tran_name' => 'Ayeyarwaddy Cruise',
'tran_image' => 'boat-ChutzpahToo1.jpg',
'type' => 'cruise',
'troute_id' => 22
)
);
// initialize the result array
$result = array();
// loop over the starting array
foreach($starting_array as $entry) {
// make sure the result array has a key matching this item's type
if(!array_key_exists($entry['type'], $result)) {
$result[ $entry['type'] ] = array();
}
// add this item to the result array
$result[ $entry['type'] ][] = $entry;
}
// this is just for testing, so you can verify the output matches your desired result
echo "<pre>";
var_dump($result);
echo "</pre>";
Try this:
<?php
$tempArr = Array
(
Array(
"des_id" => 1,
"des_name" => "bagan",
"tran_id" => 1,
"tran_name" => "private",
"tran_image" => "1251961905A1.jpg",
"type" => "car",
"troute_id" => 10
),
Array
(
"des_id" => 1,
"des_name" => "bagan",
"tran_id" => 2,
"tran_name" => "express",
"tran_image" => "bus3.jpg",
"type" => "car",
"troute_id" => 13
),
Array
(
"des_id" => 1,
"des_name" => "bagan",
"tran_id" => 3,
"tran_name" => "MyanmarTrain",
"tran_image" => "Burma-Gorteikviaduct.jpg",
"type" => "train",
"troute_id" => 16
),
Array
(
"des_id" => 1,
"des_name" => "bagan",
"tran_id" => 4,
"tran_name" => "Ayeyarwaddy Cruise",
"tran_image" => "boat-ChutzpahToo1.jpg",
"type" => "cruise",
"troute_id" => 22
)
);
$resultArr = array();
foreach($tempArr as $tempKey=>$temp)
{
if(!array_key_exists($temp['type'], $resultArr))
{
$resultArr[$temp['type']] = array();
}
$resultArr[$temp['type']][] = $temp;
}
echo '<pre>';
print_r($resultArr);
?>
This is working fine .....

Categories