tags:

views:

117

answers:

6
$result = array_merge($arr1,$arr2);

I want to exclude numerical values of $arr2,is there an option for this?

Edit after comment:

$arr1 = array('key' => 1);
$arr2 = array('test',1 => 'test', 'key2' => 2);

after processing I need the result to be:

array('key' => 1,'key2' => 2);

Excluding numerical keys

+1  A: 

It seems that you want to array_filter your $arr2's keys, first:

function not_numeric( $object ) {
   return !is_numeric( $object );
}


$no_numeric_keys  = array_filter( array_keys( $arr2 ), not_numeric );
$no_numeric_array = array_intersect_keys( $arr2, $no_numeric_keys );
$result           = array_merge( $arr1, $no_numeric_array );
xtofl
Is this the most convenient solution?
Does `array_filter` filter by keys ? I would have said it could only filter by values (and, here, the OP wants to remove the items for which the **key** is a numeric).
Pascal MARTIN
@Pascal MARTIN ,you got me right:)
@Pascal: right. I created the answer before the example data was there...
xtofl
@user198729: convenience is in the eye of the beholder :) I find it using all PHP array functions, and understandable. But yes: I'm writing the solution, so...
xtofl
+1  A: 

I'm guessing that this would work, after using $result = array_merge($arr1,$arr2);:

foreach ($result as $key => $value) {
  if (is_numeric($key)) {
    unset($result[$key]);
  }
}

Edit: In as few lines as possible (1) – as requested in the new title:

foreach ($result as $key => $value) { if (is_numeric($key)) { unset($result[$key]); } }
Eikern
I want an solution that **looks** elegant..
@user198729: Eikern has spared some time of his to provide you a solution, for which you need to be thankful. Then *request* for a better looking solution. Before you learn PHP learn some netiquettes.
gameover
Can someone help me delete my question?
A: 

Just loop through each array and test if keys are strings:

$output = array();
foreach($arr1 as $key => $value) {
    if(is_string($key)) {
        $output[$key] = $value;
    }
}
foreach($arr2 as $key => $value) {
    if(is_string($key)) {
        $output[$key] = $value;
    }
}

Edit: Since you said elegant...

function merge_arrays_string_keys()
{
    $output = array();

    foreach(func_get_args() as $arr)
    {
        if(is_array($arr))
        {
            foreach($arr as $key => $value) {
                if(is_string($key) {
                    $output[$key] = $value;
                }
            }
        }
    }

    return $output;
}
smack0007
See my updated title...
A: 

elegant, huh?

$test = array('test',  1 => 'test', 'key2' => 2, 33, 3 => 33, 'foo' => 'bar');
$test_non_num = array_intersect_key(
   $test,
   array_flip(
      array_diff(
         array_keys($test),
         array_filter(array_keys($test), 'is_int'))));

print_r($test_non_num); // key2=>2, foo=>bar
stereofrog
`array_filter` filters by value,not key...
@user198729: did you notice he uses `array_filter` on the keys?
xtofl
A: 

Use this code , it will also do the require thing.

<?php

$result = array ( 1,"pavunkumar","bks", 123 , "3" ) ;
array_walk($result,'test_print');
print_r ( $result ) ;

function test_print( $val , $key )
{
global $result;
if ( gettype ( $val ) == 'integer' )
{
unset ( $result[$key] ) ;
}
}
pavun_cool
A: 
array_diff_ukey($m=$arr2+$arr1,$m,function($k){return is_string($k);})
salathe