Remove level of an array

Hi experts,

I need to change an array from this
array to alter
to this:

Array
(
     [KCL P] => Kent county League Premier division
     [IRC 1] => Bill Manklow Inter Regional Challenge Cup 1st round
     [IRC 2] => Bill Manklow Inter Regional Challenge Cup 2nd round
)

Any help appreciated.

Col
LVL 3
colinspursAsked:
Who is Participating?
 
Julian HansenConnect With a Mentor Commented:
This should do what you want
<?php
$x = array(
	array('comp_code' => 'KCL P', 'comp_desc' => 'Kent county league premier division'),
	array('comp_code' => 'IRC 1', 'comp_desc' => 'Bill Manklow Inter Regional Challenge Cup 1st round'),
	array('comp_code' => 'IRC 2', 'comp_desc' => 'Bill Manklow Inter Regional Challenge Cup 2nd round')
);

$n = array();

foreach($x as $i) {
	$n[$i['comp_code']] = $i['comp_desc'];
}
echo "<pre>" . print_r($n, true) . "</pre>";

Open in new window

1
 
Pawan KumarDatabase ExpertCommented:
Please try this -

$1DimensionalArray = array_map('current', $2Dimensionalarray);

Open in new window


or

 $1DimensionalArray  = array_reduce($2Dimensionalarray, 'array_merge', array())

Open in new window

0
 
colinspursAuthor Commented:
After I removed the initial numbers from the array names,
$1DimensionalArray = array_map('current', $2Dimensionalarray);
gives
Array
(
    [0] => KCL P
    [1] => IRC 1
    [2] => IRC 2
)

$1DimensionalArray  = array_reduce($2Dimensionalarray, 'array_merge', array())
gives
Array
(
    [comp_code] => Other
    [comp_desc] => Other
)

Other is the last option in each case.
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
colinspursAuthor Commented:
It does - thanks Julian.
0
 
Julian HansenCommented:
You are welcome.
0
 
colinspursAuthor Commented:
There might be a few more array questions coming up, I'm really struggling
0
 
Julian HansenCommented:
Post away and then PM me
0
 
colinspursAuthor Commented:
Will do, I think I've made some progress though.

Cheers
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.