I need to get two arrays to merge into one while keeping all the keys in place and listing the values in an array like in this example:
$array1 = array('car' => '3', 'bus' => '2');
$array2 = array('dog' => '1', 'car' => '2', 'bird' => '9');
$merged = array(
'car' => array('3','2'),
'bus' => array('2',null),
'dog' => array(null,'1'),
'bird' => (null,'9')
);
function merge_common_keys(){
$arr = func_get_args();
$num = func_num_args();
$keys = array();
$i = 0;
for ($i=0; $i<$num; ++$i){
$keys = array_merge($keys, array_keys($arr[$i]));
}
$keys = array_unique($keys);
$merged = array();
foreach ($keys as $key){
$merged[$key] = array();
for($i=0; $i<$num; ++$i){
$merged[$key][] = isset($arr[$i][$key]) ? $arr[$i][$key] : null;
}
}
return $merged;
}
Usage:
$merged = merge_common_keys($array1,$array2);
PS. It can work with more than two arrays, just pass as many as you want as next arguments.
Something like this? http://php.net/array_merge_recursive
Related
$arr1 = [1,2,3];
$arr2 = [1,2,3,4];
$arr3 = [1,2,3,4,5];
echo max( count($arr1), count($arr2), count($arr3) ); // returns 5
with max I do get the count, but don't know which array is larger. How do I get the larger array's reference ($arr3 in this case)?
You should use multidimensional array to store your all arrays then, loop through the multidimensional array and find your largest array,
$lgArraySize = 0; // used for comparing the size of array
$lgArray = array(); // used to store reference of largest array
foreach($arraylist as $array) {
if(count($array) > $largeArraySize) {
$lgArray = &$array;
$lgArraySize = count($array);
}
}
print_r($largeArray);
Here is the one liner code.(that you want)
function findMax( $row ){
return count($row);
}
$maxArrayPos = array_search(max( array_map("findMax", $multi )), array_map("findMax", $multi ));
print_r($multi[$maxArrayPos]);
You can make this as one liner.
It's better to use indexes for such arrays, so that traversal is easy.
You can get reference to the larger array as follows -
$arr;
$arr[0] = [6,2,1,12,32,11];
$arr[1] = [1,2,3,4];
$arr[2] = [1,2,3,4,4,4,1,1,1,1];
$count = -1;//will hold max count
$big;//larger array will be stored here
foreach ($arr as $key => $value) {
$curCount = count($arr[$key]);//curCount holds current array size
//checking for biggest array count
if($curCount>$count){
$count = $curCount;
$big = &$arr[$key];
}
}
var_dump($big);
Maybe the logic is too bulky though!
Is this what you need?
$arr1 = [1,2,3];
$arr2 = [1,2,3,4];
$arr3 = [1,2,3,4,5];
$countmax = [
"arr1" => $arr1,
"arr2" => $arr2,
"arr3" => $arr3,
];
$value = max($countmax);
print_r($value);
Oputput:
Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 )
Try this :
function laregr(){
$numargs = func_num_args();
$large_array = array();
if($numargs){
$arg_list = func_get_args();
$max = 0;
foreach($arg_list as $arg){
if(is_array($arg)){
$big = count($arg);
if($big >= $max ){
$max = $big;
$large_array = $arg;
}
}
}
}
return $large_array;
}
$arr1 = [1,2,3,4,5,6];
$arr2 = [1,2,3,4,5];
$arr4 = [1,2,3,4, 5,6,7];
$max_array = laregr($arr1, $arr2, $arr4); // large($arr1, ...)
print_r($max_array);
You may like to store all arrays inside an array and would like to do a foreach I think.
function max_length($array) {
$max = 0;
foreach($array as $child) {
if(count($child) > $max) {
$max = count($child);
}
}
return $max;
}
check this why it is better approach .
Original array
Array ( [1147183647] => 3 [2147483242] => 1 )
expected out put
Array (1147183647,1147183647,1147183647,2147483242)
how can i expand array key base on their value
slightly sexier than those using 2 loops is to use the built in array_fill function
<?php
$array = array(1147183647 => 3, 2147483242 => 1);
$new=array();
foreach ($array as $key=>$var){
$new= array_merge($new,array_fill(0,$var,$key));
}
print_r($new);
demo: http://codepad.viper-7.com/NTcDhC
Loop over each key value pair and add the key to the output array for the value number of repitions:
$output = array();
$arr = array(1147183647 => 3, 2147483242 => 1);
foreach($arr as $v => $rep) {
for($i = 0; $i < $rep; $i++) {
$output[] = $v;
}
}
Here is a simple snippet.
<?php
//initialize array
$array = array(
"1147183647" => 3, "2147483242" => 1
);
//new array to hold the values
$new =array();
foreach($array as $k=>$v){
//echo $k."-".$v."<br>";
for($i=0; $i<$v; $i++){
$new[] = $k;
}
}
//check the output
print_r($new);
?>
Try this.
$array = array(1147183647 => 3, 2147483242 => 1);
$result=array();
foreach ($array as $key=>$var){
$result= array_merge($result,array_fill(0,$var,$key));
}
var_dump($result);
I have the following php code,
<?php
$first = array(1,2,3);
$last = array(4,5,6);
$k = 10;
echo json_encode(array('id' =>$k, 'name' => array( 'first' => $first, 'last' => $last)));
?>
I only get this result
{"id":10,"name":{"first":[1,2,3],"last":[4,5,6]}}
but I want the following format, so that I can access the data in javascript like name.first and name.last
{"id":10,"name":[{"first":1,"last":4},{"first":2,"last":5},{"first":3,"last":6}] }
can anyone help me out?
Thanks,
Pat
Use array_map to operate in the same time on the multiple arrays:
$array = array_map(function ($first, $last) { return array("first" => $first, "last" => $last); }, $first, $last);
echo json_encode(array('id' =>$k, 'name' => $array));
Sample code for a brute force solution for this:
$first = array(1,2,3);
$last = array(4,5,6);
$name = array();
for( $i = 0; $i < count($first); $i++) {
$name[$i]['first'] = $first[$i];
$name[$i]['last'] = $last[$i];
}
echo json_encode(array('id' =>$k, 'name' =>$name));
if ( $_GET['_value'] == 'moto' )
{
$array[] = array('1' => 'Yamaha');
$array[] = array('2' => 'Suzuki');
$array[] = array('3' => 'Triumph');
$array[] = array('4' => 'KTM');
$array[] = array('5' => 'Honda');
$array[] = array('6' => 'Harley Davidson');
$array[] = array('7' => 'Buell');
$array[] = array('8' => 'MV Agusta');
$array[] = array('9' => 'Ducati');
$array[] = array('10' => 'Other');
}
$array = sort($array);
echo json_encode( $array );
that is the code i have and its pulled by a chained dropdown.
I want it to return the values sorted alphabetically but based on the code you see it returns an empty array. what could be the mistake I am making /
Your code fails because you have an array of arrays here.
You should either search for "sort php array by sub-array key"
Or you can try something like:
$array[1] = 'Yamaha';
$array[2] = 'Suzuki';
// ...
sort($array);
echo json_encode($array);
Your array contains arrays, hence cannot be sorted, try:
$array[1] = 'Yamaha';
$array[2] = 'Suzuki';
then sort($array)
You can use uasort() function
like:
function cmp($a, $b) {
$a = reset($a);
$b = reset($b);
if ($a == $b) {
return 0;
}
return ($a < $b) ? -1 : 1;
}
uasort($array, 'cmp')
It's not at all pretty but this does the job.
Unless you're constrained otherwise you should really use some of the other suggestions.
<?
if ( $_GET['_value'] == 'moto' ) {
$array[] = array('1' => 'Yamaha');
$array[] = array('2' => 'Suzuki');
$array[] = array('3' => 'Triumph');
$array[] = array('4' => 'KTM');
$array[] = array('5' => 'Honda');
$array[] = array('6' => 'Harley Davidson');
$array[] = array('7' => 'Buell');
$array[] = array('8' => 'MV Agusta');
$array[] = array('9' => 'Ducati');
$array[] = array('10' => 'Other');
foreach($array as $i => $v)
{
$v = array_values($v);
$sort[] = $v[0];
}
sort($sort);
$c = 0;
foreach($sort as $i => $v)
{
$c++;
$sorted[] = array($c=>$v);
}
echo json_encode($sorted);
}
?>
I have to create a function that copy the array(that i pass to the function) with only uneven keys. Example:
$a = array(
'0' => '0',
'one => 'one',
'1' => '1',
'two' => 'two'
)
I have to get:
$result = array(
'one => 'one',
'two' => 'two'
)
I have created the follow function, it works, but maybe I need to optimize it(maybe exists a function that does this job). Advice?
private clean($values){
$vv = array();
$keys = array_keys($values);
for($i=1; $i < count($values); $i+=2) $vv[$keys[$i]] = $values[$keys[$i]];
return $vv;
}
Thanks
$even = range(0, count($array), 2);
source
UPDATE:
for ($i = 0, $c = count($array); $i <= $c; $i = $i + 2) {
$even = array_push($even, $array[$i]);
}
Try
$outputArray = array();
$keyToAdd = false;
foreach( $inputArray as $key => $value ) {
if( $keyToAdd ) {
$outputArray[$key] = $value;
}
$keyToAdd = !$keyToAdd;
}