I am new in PHP and love to learn it.
I want to merge two or more arrays having same keys only. And neglect the arrays whose keys are not in both the Arrays. Like
Here is the First Array :
Array
(
[1] => Array
(
[111] => 36265
)
[2] => Array
(
[222] => 36265
)
[3] => Array
(
[333] => 36265
)
)
and Second Array as :
Array
(
[1] => Array
(
[444] => 36265
)
[2] => Array
(
[555] => 36265
)
[4] => Array
(
[666] => 36265
)
)
And i want my result to be as :
Array
(
[1] => Array
(
[111] => 36265
[444] => 36265
)
[2] => Array
(
[222] => 36265
[555] => 36265
)
)
Neglecting the rest Array with key [3] & [4]....
So Please anyone tell me how to get this. I try out "array_merge_recursive()" but this one displays all keys.
Thanks in advance.
You'd have to loop over one of the arrays, check for the existence of the current key in the other array, if it exists, merge them, eg:
$output = array();
foreach ($array1 as $key => $value) {
if (array_key_exists($key, $array2)) {
$output[$key] = $value + $array2[$key];
}
}
Here's a demo
You're probably looking for array_intersect_key.
You could also use a foreacah loop and create a new one. While in this loop you can use one of the arrays keys to match them both. Consider this example:
$array1 = array( 1 => array(111 => 36265), 2 => array(222 => 36265), 3 => array(333 => 36265), ); $array2 = array( 1 => array(444 => 36265), 2 => array(555 => 36265), 4 => array(666 => 36265), );
$new_array = array();
foreach($array1 as $key => $value) {
if(isset($array2[$key])) {
$new_array[$key][key($array1[$key])] = reset($array1[$key]);
$new_array[$key][key($array2[$key])] = reset($array2[$key]);
}
}
echo '<pre>';
print_r($new_array);
echo '</pre>';
Should yield something like this:
Array
(
[1] => Array
(
[111] => 36265
[444] => 36265
)
[2] => Array
(
[222] => 36265
[555] => 36265
)
)
Related
I have a multidimensional associative array which has a set of array. I want to change my array index value from some array value.
I already tried some array functions but my array also contains some null array so laravel function keyBy not give me wanted result.
$arr1=array(0 =>array(),1=>array(0=>array('quan'=>10,'handle' => 'baroque'),1 =>array('quan'=>20,'handle' => 'baroque')),
2 =>array (0 =>array('quan' => 5,'handle' => 'adidas')));
My expected result array must be like this
$arr2=array(0 =>array(),'baroque'=>array(0=>array('quan'=>10,'handle' => 'baroque'),1 =>array('quan'=>20,'handle' => 'baroque')),
'adidas' =>array (0 =>array('quan' => 5,'handle' => 'adidas')));
You can use the classic foreach. Check if the handle on element 0 exists using isset, if it does, use that as the key.
$arr1 = //...
$result = array();
foreach($arr1 as $key => $val) {
if (is_array($val) && isset($val[0]["handle"])) $result[ $val[0]["handle"] ] = $val;
else $result[$key] = $val;
}
$result will be:
Array
(
[0] => Array
(
)
[baroque] => Array
(
[0] => Array
(
[quan] => 10
[handle] => baroque
)
[1] => Array
(
[quan] => 20
[handle] => baroque
)
)
[adidas] => Array
(
[0] => Array
(
[quan] => 5
[handle] => adidas
)
)
)
You can use without condition by grouping at the handle as key directly.
$result = [];
foreach ($arr as $key => $value) {
if (!empty($value)) {
foreach ($value as $key1 => $value1) {
$result[$value1['handle']][] = $value1;
}
} else {
$result[] = $value;
}
}
Demo
Output:-
Array
(
[0] => Array
(
)
[baroque] => Array
(
[0] => Array
(
[quan] => 10
[handle] => baroque
)
[1] => Array
(
[quan] => 20
[handle] => baroque
)
)
[adidas] => Array
(
[0] => Array
(
[quan] => 5
[handle] => adidas
)
)
)
Try this..
$res = [];
foreach($x as $key => $value)
{
if(empty($value))
{
$res[] = $value;
}
else
{
foreach($value as $v => $k)
{
if(array_key_exists($k['handle'],$res))
{
$res[$k['handle']][] = ['quan' => $k['quan'],'handle' => $k['handle']];
}
else
{
$res[$k['handle']][0] = ['quan' => $k['quan'],'handle' => $k['handle']];
}
}
}
}
The result is going to be like this.
Array
(
[0] => Array
(
)
[baroque] => Array
(
[0] => Array
(
[quan] => 10
[handle] => baroque
)
[1] => Array
(
[quan] => 20
[handle] => baroque
)
)
[adidas] => Array
(
[0] => Array
(
[quan] => 5
[handle] => adidas
)
)
)
I'm having two arrays like this
$whole_orders
Array
(
[2] => Array
(
[0] => Array
(
[id] => 3
[food_id] => 1
)
)
[1] => Array
(
[0] => Array
(
[id] => 2
[food_id] => 2
)
[1] => Array
(
[id] => 1
[food_id] => 1
)
)
)
And $array
Array
(
[2] => Array
(
[0] => Array
(
[count] => 1
[subtotal] => 103.42
[tax] => 18.42
)
)
[1] => Array
(
[0] => Array
(
[count] => 2
[subtotal] => 303.42
[tax] => 38.42
)
)
)
Here I'm having two arrays such as $whole_orders & $array from which I need to merge the $array values into the $whole_orders..
And the $whole_orders having nested values which are dynamic..
Finally My array should be like this..
Array
(
[2] => Array
(
[0] => Array
(
[id] => 3
[food_id] => 1
)
[1] => Array
(
[count] => 1
[subtotal] => 103.42
[tax] => 18.42
)
)
[1] => Array
(
[0] => Array
(
[id] => 2
[food_id] => 2
)
[1] => Array
(
[id] => 1
[food_id] => 1
)
[2] => Array
(
[count] => 2
[subtotal] => 303.42
[tax] => 38.42
)
)
)
It should append nested values of $whole_orders array's..
If you think that my title is not correct please change it..
Thanks in advance..
Use foreach and iterate your $array and assigned to $whole_orders
<?php
// if $array is always single dimension array
foreach($array as $array_key=>$array_val)
{
$whole_orders [$array_key][]=$val[0];
}
// or if $array is multi dimension array
foreach($array as $array_key=>$array_val)
{
foreach($array_val as $key=>$val)
{
$whole_orders [$array_key][]=$val;
}
}
?>
Just do this it will achieve you desire output ,but when if the count is same for both array
foreach($array as $arrayKey => $arrayValue){
foreach($arrayValue as $key => $value){
$whole_orders[$arrayKey][] = $value;
}
}
print_r($whole_orders);
You can try below approach..
$arr3 = array();
foreach($arr1 as $key => $value) :
$arr3[$key] = $value;
if(isset($arr2[$key])) :
foreach($arr2[$key] as $k=>$val) :
$arr3[$key][] = $val;
endforeach;
endif;
endforeach;
print_r($arr3);
I am generating dynamic textbox for save and add more functionality and i have to store that data in database i got the array but i dont know how to put that array in to loop so i can get my data.
Array looks like this based on this prepare loop so i can access every element of array:
Array
(
[0] => Array
(
[0] => Array
(
[0] => Array
(
[prem_type] => 1
)
[1] => Array
(
[phase_name] => a1
)
[2] => Array
(
[counter] => 2
)
[3] => Array
(
[block] => A
)
[4] => Array
(
[block] => B
)
)
)
[1] => Array
(
[0] => Array
(
[0] => Array
(
[prem_type] => 1
)
[1] => Array
(
[phase_name] => a2
)
[2] => Array
(
[counter] => 2
)
[3] => Array
(
[block] => A
)
[4] => Array
(
[block] => B
)
)
)
)
Thanks
try this
$array = //your array
foreach($array as $value){
foreach($value as $value2){
foreach($value2 as $value3){
foreach($value3 as $key3 => $value3){
//$key3 is rem_type, phase_nameā¦
//$value3 is required values
}
}
}
}
To get to the data you can use something like:
foreach ($array AS $row) {
$prem_type = $row[0][0]['prem_type'];
$phase_name = $row[0][1]['phase_name'];
$counter = $row[0][2]['counter'];
$block1 = $row[0][3]['block'];
$block2 = $row[0][4]['block'];
}
An alternative is to restructure the array into something more tidy:
$result = array();
foreach ($array AS $rowId => $row) {
$result[$rowId] = array(
'prem_type' => $row[0][0]['prem_type'],
'phase_name' => $row[0][1]['phase_name'],
'counter' => $row[0][2]['counter'],
'block1' => $row[0][3]['block'],
'block2' => $row[0][4]['block']
);
}
This results in:
Array
(
[0] => Array
(
[prem_type] => 1,
[phase_name] => a1,
[counter] => 2,
[block1] => A,
[block2] => B
)
...
)
Here is the answer:
for($i=0;$i<count($data1);$i++){
for($j=0;$j<count($data1[$i]);$j++){
$prem_type =$data1[$i][$j][0]['prem_type'];
$prem_name= $data1[$i][$j][1]['phase_name'];
$no_of_phase= $data1[$i][$j][2]['counter'];
echo $prem_type." ".$prem_name." ".$no_of_phase."<br>";
for($k=3;$k<count($data1[$i][$j]);$k++){
echo $data1[$i][$j][$k]['unitname']."<br>";
}
}
}
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;
}
}
I have a multi-dimensional array and what i want is in each second degree of array values limit should be set to 4 only. Like i have a array of type:
Array
(
[test2] => Array
(
[0] => Array
(
[application_id] => 405275016
)
[1] => Array
(
[application_id] => 405275016
)
[2] => Array
(
[application_id] => 303198288
)
[3] => Array
(
[application_id] => 303841592
)
)
[test3] => Array
(
[0] => Array
(
[application_id] => 289267216
)
[1] => Array
(
[application_id] => 303198216
)
[2] => Array
(
[application_id] => 405275016
)
[3] => Array
(
[application_id] => 303198288
)
[4] => Array
(
[application_id] => 303841592
)
[5] => Array
(
[application_id] => 311430400
)
[6] => Array
(
[application_id] => 318096216
)
[7] => Array
(
[application_id] => 320256352
)
)
)
and what i want that if the inner arrays value exceed 5 count it should not add any further values to it. i have to format this above array to something like:
Array
(
[test2] => Array
(
[0] => Array
(
[application_id] => 405275016
)
[1] => Array
(
[application_id] => 405275016
)
[2] => Array
(
[application_id] => 303198288
)
[3] => Array
(
[application_id] => 303841592
)
)
[test3] => Array
(
[0] => Array
(
[application_id] => 289267216
)
[1] => Array
(
[application_id] => 303198216
)
[2] => Array
(
[application_id] => 405275016
)
[3] => Array
(
[application_id] => 303198288
)
[4] => Array
(
[application_id] => 303841592
)
)
)
here in second array array last 3 arrays were truncated in order to take only 5 count as a value. I have tried many a methods but none achieved this.
Any idea on how can i achieve this will be highly appreciated ??
or you can use the array_slice function like this:
foreach ($array as $key=>$value)
{
$array[$key] = array_slice($value, 0, 5);
}
$array = array_map(function ($arr) { return array_slice($arr, 0, 5); }, $array);
Note that this uses PHP 5.3+ syntax.
If you actually want an array (or something like it) that cannot hold more than 5 elements, you'll need to implement a custom class (possibly by extending ArrayObject) and/or play around with SplFixedArray.
foreach ($array as $k => $v) { // Loop outer array
for ($i = 5; isset($array[$k][$i]); $i++) { // Loop inner array, starting at element #5
unset($array[$k][$i]); // unset all elements >=5
}
}
PHP has no ready functionality for this behavior, you'll have to make your own function for adding values to the array to achieve this.
Ex:
function array_add_value($array, $value) {
if(count($array) < 5) {
$array[] = $value;
}
}
If you wish to manipulate an existing array that already has too many elements, you will have to make a similar function for that
Ex:
function fix_array($array) {
if(count($array) > 5) {
for($i=4;$i<count($array);$i++) {
unset($array[$i]);
}
}
}