I have an array:
array(
[0] => Array
(
[d1] => Array
(
................
)
[d2] => Array
(
................
)
)
[1] => Array
(
[d1] => Array
(
................
)
[d2] => Array
(
................
)
)
)
How to create a new array to merge it, so only d1 and d2, remove the index 0 and 1.
For PHP > 5.5.0 you can simply use array_column like as
$result['d1'] = call_user_func_array('array_merge',array_column($your_array,'d1'));
$result['d2'] = call_user_func_array('array_merge',array_column($your_array,'d2'));
print_r($result);
Demo
Let us say your array name is $a, so we can have:
$result = [];
$result['d1'] = [];
$result['d2'] = [];
foreach ($a as $v) {
$result['d1'] = array_merge($result['d1'], $v['d1'])
$result['d2'] = array_merge($result['d2'], $v['d2'])
}
Now you have what you want in $result.
here is your solution visit here
Suppose your array $menus and after filter new array will be $filteredMenu . Your final result $filteredMenu
<?php
$menus = array(
0 =>array(
"d1" => array (
"id"=> "----",
),
"d2" => array (
"id"=> "----",
)
),
1 =>array(
"d1" => array (
"id"=> "----",
),
"d2" => array (
"id"=> "----",
)
)
);
$filteredMenu = [];
$filteredMenu['d1'] = [];
$filteredMenu['d2'] = [];
foreach ($menus as $item) {
$filteredMenu['d1'] = array_merge($filteredMenu['d1'], $item['d1']);
$filteredMenu['d2'] = array_merge($filteredMenu['d2'], $item['d2']);
}
print_r($filteredMenu);
?>
Related
I have 3 arrays as below.
$array1 = Array
(
[0] => 05/01
[1] => 05/02
)
$array2 =Array
(
[0] => ED
[1] => P
)
$array3 =Array
(
[0] => Mon
[1] => Tue
)
I want to merge these 3 arrays as below $result_array. I have written a code as below. But It gave a empty array.
$result_array =Array
(
[0] => Array
(
[0] => 05/01
[1] => ED
[2] => Mon
)
[1] => Array
(
[0] => 05/02
[1] => P
[2] => Tue
)
)
Code:
for($z=0; $z<count($array1); $z++){
$all_array[$z][] = array_merge($array1[$z],$array2[$z] );
$all_array2[$z] = array_merge($all_array[$z],$array3[$z] );
}
Please help me to do this.
Simply foreach over the first array and use the index as the key to the other arrays.
foreach ( $array1 as $idx => $val ) {
$all_array[] = [ $val, $array2[$idx], $array3[$idx] ];
}
Remember this will only work if all 3 arrays are the same length, you might like to check that first
You can simply walk through the first array with a foreach loop then access the corresponding arrays and combine the results into one big array. This will only work if the arrays are the same length so it's worth checking that before to stop any errors. Something like this:
<?php
$arr1 = ['05/01', '05/02'];
$arr2 = ['ED', 'P'];
$arr3 = ['Mon', 'Tue'];
$combined = [];
if (count($arr1) != count($arr2) || count($arr1) != count($arr3))
die("Array lengths do not match!");
foreach ($arr1 as $key => $val) {
$combined[] = [$val, $arr2[$key], $arr3[$key]];
}
var_dump($combined);
You can see an eval.in of this working here - https://eval.in/833893
Create an sample array and push to each array value with respective key
$sample = array();
for($z=0; $z<count($array1); $z++){
$sample[]=array($array1[$z],$array2[$z],$array3[$z]);
}
print_r($sample);
Out put is
Array ( [0] => Array (
[0] => 05/01
[1] => ED
[2] => Mon
)
[1] => Array (
[0] => 05/02
[1] => P
[2] => Tue
)
)
this work for n of arrays and dynamic length of array
function mergeArrays(...$arrays)
{
$length = count($arrays[0]);
$result = [];
for ($i=0;$i<$length;$i++)
{
$temp = [];
foreach ($arrays as $array)
$temp[] = $array[$i];
$result[] = $temp;
}
return $result;
}
$x = mergeArrays(['05/01' , '05/02'] , ['ED' , 'P'] , ['Mon' , 'Tus']);
$y = mergeArrays(['05/01' , '05/02' , 'X'] , ['ED' , 'P' , 'Y'] , ['Mon' , 'Tus' , 'Z'] , ['A' , 'B' , 'C']);
var_dump($x);
var_dump($y);
function merge($file_name, $titles, $description)
{
$result = array();
foreach($file_name as $key=>$name )
{
$result[] = array( 'file_name' => $name, 'title' => $titles[$key],
'description' => $description[ $key ] );
}
return $result;
}
$array1 = Array
(
'05/01',
'05/02'
);
$array2 = Array
(
'ED',
'P'
);
$array3 =Array
(
'Mon',
'Tue'
);
$result_array = array();
foreach ($array1 as $key=>$val)
{
$result_array[$key] = array($array1[$key],$array2[$key],$array3[$key]);
}
echo "<PRE>"; print_r($result_array);
i have 2 arrays i want to display the final array as what are the array element in $displayArray only be displayed from the $firstArray
$firstArray = Array
(
[0] => Array
(
[Dis_id] => Dl-Dis1
[Dis_Desc] => Discount
[Dis_Per] => 7.500
[Dis_val] => 26.25
)
[1] => Array
(
[Dis_id] => Dl-Dis2
[Dis_Desc] => Discount
[Dis_Per] => 2.500
[Dis_val] => 8.13
)
)
$displayArray = Array
(
[0] => Array
(
[0] => Dis_id
[1] => Dis_val
)
)
i want the final output will be
$resultArray = Array
(
[0] => Array
(
[Dis_id] => Dl-Dis1
[Dis_val] => 26.25
)
[1] => Array
(
[Dis_id] => Dl-Dis2
[Dis_val] => 8.13
)
)
Both the $firstArray and the $DisplayArray are dynamic but the $displayArray should be one.
i dont know how to do give me any suggestion
First up, if $displayArray will never have more than one array, the answer is pretty simple. Start by popping the inner array, to get to the actual keys you will need:
$displayArray = array_pop($displayArray);//get keys
$resultArray = array();//this is the output array
foreach ($firstArray as $data)
{
$item = array();
foreach ($displayArray as $key)
$item[$key] = isset($data[$key]) ? $data[$key] : null;//make sure the key exists!
$resultArray[] = $item;
}
var_dump($resultArray);
This gives you what you need.
However, if $displayArray contains more than 1 sub-array, you'll need an additional loop
$resultArray = array();
foreach ($displayArray as $k => $keys)
{
$resultArray[$k] = array();//array for this particular sub-array
foreach ($firstArray as $data)
{
$item = array();
foreach ($keys as $key)
$item[$key] = isset($data[$key]) ? $data[$key] : null;
$resultArray[$k][] = $item;//add data-item
}
}
var_dump($resultArray);
the latter version can handle a display array like:
$displayArray = array(
array(
'Dis_id',
'Dis_val'
),
array(
'Dis_id',
'Dis_desc'
)
);
And it'll churn out a $resultArray that looks like this:
array(
array(
array(
'Dis_id' => 'foo',
'Dis_val' => 123
)
),
array(
array(
'Dis_id' => 'foo',
'Dis_desc' => 'foobar'
)
)
)
Job done
I'm pulling an array from the database and it looks like so:
Array
(
[0] => Array
(
[tracker_id] => 28
[tracking_numbers] => hdkshwuy47937892hd
)
[1] => Array
(
[tracker_id] => 28
[tracking_numbers] => 797825464411
)
)
I need to reformat it to look like this:
Array
(
[0] => Array
(
[tracker_id] => 28
[tracking_numbers] => Array
(
[0] => hdkshwuy47937892hd
[1] => 797825464411
)
)
)
I can't seem find the right search in the array or keys to create an array of numbers for the single tracker id.
Use array_column() for < php V5.5
<?php
$a=array
( array
('tracker_id' => 28,
'tracking_numbers'=> "hdkshwuy47937892hd"
),
array('tracker_id' => 28,
'tracking_numbers' => "797825464411",
) );
$a[0]['tracking_numbers']=array_column($a,"tracking_numbers");
unset($a[1]);
print_r($a);
Demo
try this
$arr_output = array();
foreach($arr_input as $arr)
{
$tracker_id = $arr['tracker_id'];
$tracking_numbers = $arr['tracking_numbers'];
$arr_output[$traker_id][] = $tracking_numbers;
}
print_r($arr_output);
UPDATE 2:
$arr_output = array();
$arr_output1 = array();
foreach($arr_input as $arr)
{
$tracker_id = $arr['tracker_id'];
$tracking_numbers = $arr['tracking_numbers'];
$arr_output[$traker_id][] = $tracking_numbers;
}
$i=0;
foreach($arr_output as $key=>$value)
{
$arr_output1[$i]['tracker_id']=$key
$arr_output1[$i]['tracking_numbers']=$value
$i+=1;
}
print_r($arr_output1);
I would like create a list of values within a new array based on the same keys from the previous array. Basically, I would like to turn this array:
$old_array = Array (
[segment1] => Array (
[subsegment] => Array (
[number1] => 1413
[number2] => 306
)
)
[segment2] => Array (
[subsegment] => Array (
[number1] => 717
[number2] => 291
)
)
)
...into this array:
$new_array = Array (
[segment] => Array (
[subsegment] => Array (
[number1] => Array (
[0] => 1413
[1] => 717
)
[number2] => Array (
[0] => 306
[1] => 291
)
)
)
)
I tried the following:
$new_array = array ();
foreach ($old_array["segment"]["subsegment"] as $value) {
$new_array["segment"]["subsegment"][] = $value;
}
Unfortunately, this doesn't work. What do I need to do? Thanks.
This is very specific to your example $old_array:
$index = 1;
$new_array = array();
do {
if (!isset($old_array["segment" . $index]["subsegment"]))
break;
foreach ($old_array["segment" . $index]["subsegment"] as $key => $value) {
$new_array["segment"]["subsegment"][$key][] = $value;
}
$index++;
} while (true);
I understand you want all number1's in the same key, then all number 2's, and so on. try this:
$numberCount = count($old_array['segment1']['subsegment']);
foreach ($old_array as $segment)
{for ($i=1;$i<=$numberCount;$i++)
{$new_array['segment']['subsegment']['number' . $i][] = $segment['subsegment']['number' . $i];}}
this is assuming all subsegment have the same number of [numberx] keys
I want to create flat array from nested array, like this one:
[0]=>Array(
"id"=>1,
"positions">Array(
[0]=>Array(
"id"=>2
),
[1]=>Array(
"id"=>3
"positions"=>Array(
[0]=>Array(
"id"=>4
)
)
)
to something like this:
[0]=>Array(
"id"=>1,
"parent_id"=>0
),
[1]=>Array(
"id"=>2,
"parent_id"=>1
),
[2]=>Array(
"id"=>3,
"parent_id"=>1
),
[3]=>Array(
"id"=>4,
"parent_id"=>3
)
I don't have parent_id in nested structure, so all the trick is to "ride" trough the nested array, and add 'parent_id', base on id from parent node. I know how to flat the array, but I need parent_id information.
Try below code : i hope useful it...
<?php
$array = array(array(
"id"=>1,
"positions" =>
array(
array(
"id"=>2
),
array(
"id"=>3,
"positions"=>
array(
array(
"id"=>4
)
)
)
)
));
echo "<pre>";
print_r(getArray($array));
echo "</pre>";
exit;
function getArray($array,$parent_id = 0)
{
$result = array();
foreach ($array as $value)
{
$tmp = array();
$tmp['id'] = $value['id'];
$tmp['parent_id'] = $parent_id;
$result[] = $tmp;
if(!empty($value['positions']))
{
$result= array_merge($result,getArray($value['positions'],$value['id']));
}
}
return $result;
}
?>
OUTPUT :
Array
(
[0] => Array
(
[id] => 1
[parent_id] => 0
)
[1] => Array
(
[id] => 2
[parent_id] => 1
)
[2] => Array
(
[id] => 3
[parent_id] => 1
)
[3] => Array
(
[id] => 4
[parent_id] => 3
)
)
Use this code
$result = array();
function generateArray($array,$parent=0){
foreach ($array as $key=>$val){
$tmp = array();
if(!empty($val['id'])){
$tmp['id'] = $val['id'];
$tmp['parent_id'] = $parent;
$result[] = $tmp;
}
if(!empty($val['positions'])){
$result=array_merge($result,generateArray($val['positions'],$val['id']));
}
}
return $result;
}
Your array must be of this structure
$data = array(0=>array("id"=>1,"positions"=>array(0=>array("id"=>2),1=>array("id"=>3,"positions"=>array(0=>array("id"=>4))))));
Then call the function generateArray(),
var_dump(generateArray($data));