My array is below i need to arrange like array2 (without use $aa['caption1'] like names directly)
arrray1 is
Array
(
[0] => Array
(
[caption1] => Array
(
[0] => gfdhgfjhg
[1] => dfhfgjghk
)
[caption2] => Array
(
[0] => shgjgh
[1] => dhfgkgjl
)
[banner_image] => Array
(
[0] => assets/images/page_content/img_namT7.jpg
[1] => assets/images/page_content/img_R8mzP.jpg
)
)
[1] => Array
(
[heading] => Array
(
[0] =>
)
[pragraph] => Array
(
[0] =>
)
)
)
arrray2 is(Required format )
Array
(
[0] => Array
(
array('caption1'=>'caption1','caption2'=>'shgjgh','banner_image'=>'assets/images/page_content/img_namT7.jpg'),
array('caption1'=>'dfhfgjghk','caption2'=>'dhfgkgjl','banner_image'=>'page_content/img_R8mzP.jpg')
)
[1] => Array
(
array('heading'=>'','pragraph'=>''),
array('heading'=>'fgh','pragraph'=>'ghgh'),
)
)
please any one help me.
The solution using array_keys, array_map and array_combine functions:
// $arr is your initial array
$result = [];
foreach($arr as $v){
$keys = array_keys($v);
$data = call_user_func_array('array_map', [null] + $v);
$result[] = array_map(function($item) use($keys){
return array_combine($keys, $item);
}, $data);
}
print_r($result);
The output:
Array
(
[0] => Array
(
[0] => Array
(
[caption1] => gfdhgfjhg
[caption2] => shgjgh
[banner_image] => assets/images/page_content/img_namT7.jpg
)
[1] => Array
(
[caption1] => dfhfgjghk
[caption2] => dhfgkgjl
[banner_image] => assets/images/page_content/img_R8mzP.jpg
)
)
[1] => Array
(
[0] => Array
(
[heading] =>
[pragraph] =>
)
)
)
Related
I have an array that has some keys that are repeating multiple times. Would like to combine them so that I can have them in one array, within the same array.
I have an array of this type;
Array
(
[Shoes] => Array
(
[POLO] => Array
(
[0] => Size5
)
)
)
Array
(
[Shoes] => Array
(
[FUBU] => Array
(
[0] => size6
)
)
)
Array
(
[Bag] => Array
(
[HPBAG] => Array
(
[0] => Black
)
)
)
Array
(
[Bag] => Array
(
[HPBAG] => Array
(
[0] => White
)
)
)
I would like an output of the following kind;
Array
(
[Shoes] => Array
(
[POLO] => Array
(
[0] => size5
)
[FUBU] => Array
(
[0] => size6
)
)
[Bag] => Array
(
[HPBAG] => Array
(
[0] => Black
[1] => White
)
)
)
I have tried array_merge, array_merge_recursive but all are not working.
foreach ($county as $value) { print_r(array_merge($value)); } //$value contains the array
foreach ($county as $value) { print_r(array_merge_recursive($value)); } //$value contains the array
Kindly assist if you have an idea on how to solve this in PHP.
You could do it with a few nested array_walk() calls:
$arr=array(array("Shoes" => array("POLO" => array("Size5"))),
array("Shoes" => array("FUBU" => array("size6"))),
array("Bag" => array("HPBAG" => array("Black"))),
array("Bag" => array("HPBAG" => array("White"))));
$res=[];
array_walk($arr,function($a) use (&$res){
array_walk($a, function($ar,$type) use (&$res){
array_walk($ar, function ($arr,$brand) use (&$res,$type){
$res[$type][$brand]=array_merge($res[$type][$brand]??[],$arr);
});
});
});
print_r($res);
See the demo here: https://rextester.com/RFLQ18142
It produces this result:
Array
(
[Shoes] => Array
(
[POLO] => Array
(
[0] => Size5
)
[FUBU] => Array
(
[0] => size6
)
)
[Bag] => Array
(
[HPBAG] => Array
(
[0] => Black
[1] => White
)
)
)
I am having below two arrays
$firstArray = Array
(
[1] => Array
(
[MemberList] => Array
(
[0] => 100
[1] => 5d6
[2] => 5d7
)
)
[3] => Array
(
[MemberList] => Array
(
[0] => 5d8
[1] => 200
)
)
)
$secondArray = Array
(
[0] => 100
[1] => 200
)
my question is if suppose $secondArray array values matched with $firstArray array then i have to remove the values from $firstArray
my expected output
$firstArray = Array
(
[1] => Array
(
[MemberList] => Array
(
[0] => 5d6
[1] => 5d7
)
)
[3] => Array
(
[MemberList] => Array
(
[0] => 5d8
)
)
)
$newArray = array_map(
function ($v) use ($secondArray) {
return ['MemberList' => array_diff($v['MemberList'], $secondArray)];
},
$firstArray
);
Fiddle here.
This is probably not so hard, but I have searched for a while with no luck so any help will be appreciated.
I'm working in PHP with two arrays which contain a number of multidimensional arrays each with a set of two values. The 2 arrays looks something like this:
Array
(
[0] => Array
(
[0] => 3206.63
[1] => 815.47
)
[1] => Array
(
[0] => 5024.71
[1] => 803.73
)
[2] => Array
(
[0] => 3290.36
[1] => 625.02
)
//...
)
Array
(
[0] => Array
(
[0] => 3138.87
[1] => 819.8
)
[1] => Array
(
[0] => 5000.24
[1] => 810.87
)
[2] => Array
(
[0] => 3221.15
[1] => 668.58
)
//...
)
And I need to achieve this:
Array
(
[0] => Array
(
[0] => 6345.5
[1] => 1635.27
)
[1] => Array
(
[0] => 10024.95
[1] => 1614.6
)
[2] => Array
(
[0] => 6511.51
[1] => 1293.6
)
//...
)
This should work for you:
(Here i loop though each innerArray with foreach and use array_sum() to get the sum of all values of each innerArray)
<?php
$arr = array(
array(
3206.63,
815.47
),
array(
5024.71,
803.73
),
array(
3290.36,
625.02
)
);
$result = array();
foreach($arr as $v)
$result[] = array_sum($v);
print_r($result);
?>
Output:
Array ( [0] => 4022.1 [1] => 5828.44 [2] => 3915.38 )
If you have 2 Arrays you can use this:
$result = array();
foreach($arr1 as $k => $v)
$result[] = array_sum($arr1[$k]) + array_sum($arr2[$k]);
print_r($result);
EDIT:
From your updated question, this should work for you:
<?php
$arr1 = array(
array(
3206.63,
815.47
),
array(
5024.71,
803.73
),
array(
3290.36,
625.02
)
);
$arr2 = array(
array(
3138.87,
819.8
),
array(
5000.24,
810.87
),
array(
3221.15,
668.58
)
);
$sums = array();
foreach ($arr1 as $key => $value) {
$sums[$key][] = $arr1[$key][0] + $arr2[$key][0];
$sums[$key][] = $arr1[$key][1] + $arr2[$key][1];
}
print_r($sums);
?>
Output:
Array
(
[0] => Array
(
[0] => 6345.5
[1] => 1635.27
)
[1] => Array
(
[0] => 10024.95
[1] => 1614.6
)
[2] => Array
(
[0] => 6511.51
[1] => 1293.6
)
)
Is there a way to delete a dimension in a array (only if it's empty), it's pretty dificult to explain with words, so that's what i want to do :
I have an array that returns :
(
[region1] => Array
(
[] => Array
(
[0] => citie1
[1] => citie2
)
[region2] => Array
(
[] => Array
(
[0] => citie1
[1] => citie2
[2] => citie3
)
)
)
I want it to be :
(
[region1] => Array
(
[0] => citie1
[1] => citie2
)
[region2] => Array
(
[0] => citie1
[1] => citie2
[2] => citie3
)
)
foreach($array as $key => $value) {
$array[$key] = reset($value);
}
That will replace each value in the outer array with the first element of that value.
How would I go about flipping an array and establishing relationships between all the values and keys? For example:
I am trying to turn this:
Array (
[11913] => Array (
[0] => 4242
[1] => 3981
)
[9878] => Array (
[0] => 2901
[1] => 3981
)
[11506] => Array (
[0] => 3981
[1] => 2901
)
)
Into this:
Array (
[3981] => Array (
[0] => 11506
[1] => 9878
[2] => 11913
)
[2901] => Array (
[0] => 11506
[1] => 9878
)
[4242] => Array (
[0] => 11913
)
)
Are there any PHP functions that will already do this automatically? If not what would be a way of going about this? Can't seem to wrap my head around it.
Here you go.
$final_array = array();
foreach($initial_array as $key => $val){
foreach($val as $v){
$final_array[$v][] = $key;
}
}