PHP merge 2 array with same key - php

Hi everyone how I can merge/push one array to another to specific keys? Here is my arrays:
// Array 1
Array
(
[1] => Array
(
[name] => test
)
)
// Array 2
Array
(
[1] => Array
(
[age] => 25
)
)
I want this result:
Array
(
[1] => Array
(
[name] => test
[age] => 25
)
)
I use PHP and will be very grateful if someone help me. Thanks in advance!

$arr = [ 1 => [ "name" => "Test" ] ];
$arr2 = [ 1 => [ "age" => 25 ] ];
foreach ($arr as $key => $value) {
if (isset($arr2[$key])) {
$arr[$key] = array_merge($value,$arr2[$key]);
}
}
print_r($arr);
Check the output at https://eval.in/602680

Just add them together:
<?php
$array1 = array('name' => 'test');
$array2 = array('age' => 21);
var_dump($array1 + $array2);

Related

how to get array from given array in php

I've array like this in php:
Array
(
[0] => Array
(
[offer_id] => 62122
[quantity] => 1
)
[1] => Array
(
[offer_id] => 62123
[quantity] => 2
)
[2] => Array
(
[offer_id] => 62124
[quantity] => 2
)
)
I want to create new array from the above array like this:
Array
(
[62122] => 1
[62123] => 2
[62124] => 2
)
and so on. Any help will be appreciated. Thanks in advance.
There is a PHP function that can do this for you: array_column().
The first argument is the array.
The second argument is the key you want as value.
The third (optional) argument is which key should be used as index
$newArray = array_column($array, 'quantity', 'offer_id');
Here's a demo: https://3v4l.org/AANUO
$resultArr = [];
foreach ($org_array as $one){
$resultArr[ $one["offer_id"] ] = $one["quantity"];
}
// $resultArr now contains your desired structure
<?php
$originalArray = [
[
"offer_id" => 62122,
"quantity" => 1
], [
"offer_id" => 62123,
"quantity" => 2
], [
"offer_id" => 62124,
"quantity" => 2
]
];
$newArray = array_column($originalArray, 'quantity', 'offer_id');
print_r($newArray);
// Output :
// Array
// (
// [62122] => 1
// [62123] => 2
// [62124] => 2
// )

Problem with a specific task using associative arrays in PHP

I've been stuck on this for the better part of the day and I'm out of ideas. I have an array like this:
Array
(
[rank] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
[name] => Array
(
[0] => 'Hideki'
[1] => 'Rory'
[2] => 'Sam'
[money] => Array
(
[0] => '$100'
[1] => '$200'
[2] => '$500'
)
)
and I have the task to create an array with the following format from it:
Array
(
[Hideki] => Array
(
[rank] => 1
[money] => '$100'
)
[Rory] => Array
(
[rank] => 2
[money] => '$200'
[Sam] => Array
(
[rank] => 3
[money] => '$500'
)
)
The catch is that 'rank' and 'money' have to be dynamic names
It should be simple as that:
$new = [];
foreach($array['name'] as $key => $name) {
$new[$name] = [
'rank' => $array['rank'][$key],
'money' => $array['money'][$key]
];
}
Little late but here my answear. My approach was to use the array_walk() function.
$array = [
'rank' => [1,2,3],
'name' => ['Hideki', 'Rory', 'Sam'],
'money' => ['$100', '$200', '$500'],
];
$i = 0;
$newArray = [];
array_walk($array['name'], function($name) use (&$i, $array, &$newArray) {
$newArray[$name] = ['rank'=> $array['rank'][$i], 'money' => $array['money'][$i]];
$i++;
});
print_r($newArray);
Run your first array through a foreach loop referencing only the "name" key and using key=>value pairs. Then reference the other keys from the first array when you build the new array, setting the value as the key to the second array.
You will need to first get the keys using array_keys() and use a nested foreach to loop through all the keys.
Example:
$keys1 = array_keys($array1);
foreach ($array1['name'] as $key => $value) {
$val2 = array();
foreach ($keys1 as $k){
if ($k != 'name') $val2[$k] = $array1[$k][$key];
}
$array2[$value] = $val2;
}

How to use array_column or foreach to get result

This is my array,with the use of array_column or any loop I want to replace keys as value of next element .I don't want to change parent array index
Array
(
[0] => Array
(
[id] => 11
[total] => 100000
[content] => abc
)
[1] => Array
(
[id] => 22
[total] => 200000
[content] => def
)
)
This is the array I would like to have.
Array
(
[0] => Array
(
[11] => 100000
[content] => abc
)
[1] => Array
(
[22] => 200000
[content] => def
)
)
It is very simple Try this:-
$array = array(
'0'=>array('id'=> 10,'total'=> 100000,'content' => 'abc'),
'1'=>array('id'=> 11,'total'=> 200000,'content' => 'def')
);
foreach($array as $key => $val){
$array[$key][$val['id']] = $val['total'];
unset($array[$key]['total']);
unset($array[$key]['id']);
}
echo "<pre>"; print_r($array); die; // print array data here
Hope it helps!
Using 'foreach' to modify existing array works perfect, but 'array_walk' is just more expressive, because it was designed for this task.
<?php
$array = [
[
'id' => 11,
'total' => 10000,
'content' => 'abc'
],
[
'id' => 22,
'total' => 200000,
'content' => 'def'
]
];
array_walk($array, function(&$row) {
$row[$row['id']] = $row['total'];
unset($row['id'], $row['total']);
});
var_dump($array);
It can be done with array_map function
$new = array_map(function($x) {
return [ $x['id']=> $x['total'], 'content' => $x['content']]; },
$array);
demo

merging mutidimensional arrays

I have an array that looks like this:
getting array need to convert array as same key value as 0
foreach($array as $key=>$id){
$consumer_data[]=$this->App_model->get_session($id);
}
print_r($consumer_data);
Array
(
[0] => Array
(
[0] => Array
(
[ConsumerID] => 1
[name] => asdfd
)
[1] => Array
(
[ConsumerID] => 5
[name] => test
)
[2] => Array
(
[ConsumerID] => 3
[name] => test1
)
)
[1] => Array
(
[0] => Array
(
[ConsumerID] => 4
[name] => test4
)
)
i want to implement array like this in same key value as 0
Array
(
[0] => Array
(
[0] => Array
(
[ConsumerID] => 1
[name] => asdfd
)
[1] => Array
(
[ConsumerID] => 5
[name] => test
)
[2] => Array
(
[ConsumerID] => 3
[name] => test1
)
[3] => Array
(
[ConsumerID] => 4
[name] => test4
)
)
I am using PHP. Can anyone point me to a good starting point as to how I should go about doing this?
You can use array_merge():
$new_array[0] = array_merge($array[0], $array[1]);
Where $array is the first array.
SEE DEMO
OR for a more dynamic approach:
$new_array = array(0 => array());
foreach($array as $a) {
$new_array[0] = array_merge($new_array[0], $a);
}
SEE DEMO 2
The simpliest solution is to do it with:
$input = array(
array(
array('ConsumerID' => 1, 'name' => 'asdfd'),
array('ConsumerID' => 5, 'name' => 'test'),
array('ConsumerID' => 4, 'name' => 'test1'),
),
array(
array('ConsumerID' => 4, 'name' => 'test4'),
),
);
$output = array(
array()
);
foreach ($input as $data) {
$output[0] = array_merge($output[0], $data);
}
Try this->
$newArray = array();
foreach($values as $key=>$val){
$newArray [0][$key]=$val;
}
print_r($newArray);
Check this:
<?php
$arr[0] = array(0 => array("ConsumerID" => 1, "name" => "Ni"), 1 => array("ConsumerID" => 2, "name" => "Ab"));
$arr[1] = array(1 => array("ConsumerID" =>5, "name" => "GE"), 1 => array("ConsumerID" => 6, "name" => "DB"));
$new = array();
foreach($arr as $key => $value) {
foreach($value as $innerkey => $innervalue) {
$new[0][] = $innervalue;
}
}
print_r($new);
?>

changing the key description in php array issue

I have an array like the one below
Array
(
[0] => Array
(
[name] => Alex
[age] => 30
[place] => Texas
)
[1] => Array
(
[name] => Larry
[age] => 28
[place] => Memphis
)
)
How would I change the key names? Like "name" to "firstname", "age" to "years", "place" to "address"?
Use a foreach loop to iterate over your array, and then use array_combine in conjunction with array_values() to create the new array:
$keys = array('firstname', 'years', 'address');
foreach ($array as & $subarr) {
$subarr = array_combine($keys, array_values($subarr));
}
print_r($array);
Output:
Array
(
[0] => Array
(
[firstname] => Alex
[years] => 30
[address] => Texas
)
[1] => Array
(
[firstname] => Larry
[years] => 28
[address] => Memphis
)
)
Online demo
array_map is your friend,
$users = array_map(function($user) {
return array(
'firstname' => $user['name'],
'years' => $user['age'],
'location' => $user['place']
);
}, $users);
DEMO.
I believe that the only way to do this is to create a new array and assign each value with old key to value with new key.
<?php
//$originalArray is array from the question.
for($i=0; $i<=count($originalArray); $i++){
$originalArray[$i] = rekeyArray($originalArray[$i]);
}
function rekeyArray($a){
$result = array();
if(isset($a['name']))
$result['firstname'] = $a['name'];
if(isset($a['age']))
$result['years'] = $a['age'];
if(isset($a['place']))
$result['address'] = $a['place'];
return $result;
}
?>

Categories