0

I am looking for a way to create a new array based on array contents.

so i've got:

Array ( [0] => 1 
        [type] => first_value_i_need 
        [some_id] => 2 
        [hits] => 88 
        [some_other_id] => second_value_i_need 
      ) 

and i would like to get

Array ( [0] => 1 
        [app_name] => "first_value_i_need-second_value_i_need" 
        [hits] => "88" 
      ) 

I know that i need some sort of foreach function, but i'm right now lost.

2
  • if that's simple you don't need any foreach Commented Jun 5, 2011 at 15:31
  • How about Array[app_name] = Array[type].Array[some_other_id] Commented Jun 5, 2011 at 15:36

2 Answers 2

1

No, you don't need any loops as long as you know which keys you need.

$old = array(
    0 => 1, 
    'type' => 'first_value_i_need',
    'some_id' => 2, 
    'hits' => 88, 
    'some_other_id' => 'second_value_i_need'
);

$new = array(
    0 => $old[0],
    'app_name' => $old['type'].'-'.$old['some_other_id'],
    'hits' => $old['hits'],
);
Sign up to request clarification or add additional context in comments.

Comments

1

So basically do you wnat to get rid of the app_table_id key ?

You can do unset($array['app_table_id']);

And if you need to change some value you can do :

$array['app_name'] = $array['some_other_id'];

//> Note i posted this before your edit.

1 Comment

thanks man this will also come in handy later today, thanks in advance, you saved me another slightly dumb question ..! + 10 internets to you..!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.