Link to home
Start Free TrialLog in
Avatar of ClintonK
ClintonKFlag for United Kingdom of Great Britain and Northern Ireland

asked on

array-search giving unexpected result

I have an array that looks like this:
    [281] => Array
        (
            [postID] => 4412
            [registration] => G-BLCT
            [type] => DR220
            [serial_number] =>
            [owner] =>
            [email] =>
            [web1] =>
            [web2] =>
            [web3] =>
            [based] =>
            [notes] =>
        )

and when I run this code
	reset($aircraft_data);
	$arraykey = array_search('G-BLCT', array_column($aircraft_data,"registration"));
	echo $arraykey , ' ', $aircraft_data[$arraykey]['registration'];

Open in new window

I would expect $arraykey to have a value of 281 but instead it has a value of 8 which corresponds to aircraft registration AKR-006
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

We need to see the full array data - there are several things that could be going on here.

For instance, array_search()
Searches the array for a given value and returns the first corresponding key if successful
Which means if there is another element with the same value before 281 that will be displayed.

1. Please post your full data using var_export() to dump your array before posting it.
2. Please use CODE TAGS for your code to do so
a) Highlight your code (your array)
b) Click the CODE button in the tool bar
User generated image
Avatar of ClintonK

ASKER

To make this more manageable I have used an extract of the data to demonstrate the issue.
The code I run to test the value after the array-search is
	reset($aircraft_data);
	$arraykey = array_search('05FM', array_column($aircraft_data,"registration"));
	echo 'arraykey: ',$arraykey , ' Registration: ', $aircraft_data[$arraykey]['registration'],'<br><br>';
	var_export($aircraft_data);

Open in new window


If I populate the array with the first 16 posts I get
arraykey: 1 Registration: 05FM

array ( 0 => array ( 'postID' => 4700, 'registration' => 'G-TEST', 'type' => 'D9', 'serial_number' => '12345', 'owner' => 'Joe Bloggs', 'email' => 'joe@bloggs.com', 'web1' => 'http://www.joebloggsaircraft.com', 'web2' => '', 'web3' => '', 'based' => 'Bradford', 'notes' => '', ), 1 => array ( 'postID' => 4692, 'registration' => '05FM', 'type' => 'D9', 'serial_number' => '', 'owner' => 'Pierre Bourgeois', 'email' => 'Peibo@aol.com', 'web1' => NULL, 'web2' => NULL, 'web3' => NULL, 'based' => 'South of France', 'notes' => 'Powered by a VW 1600cc', ), 2 => array ( 'postID' => 4691, 'registration' => '19-3924', 'type' => 'D9', 'serial_number' => '19-3924', 'owner' => 'Lyle Passfield', 'email' => 'lyle.passfield@gmail.com', 'web1' => NULL, 'web2' => NULL, 'web3' => NULL, 'based' => 'Australia', 'notes' => 'Jabiru 2.2 engine. Fuselage widened 100mm. 

Wing area increased to 9.5sq m. Profile changed to D18, BRS, wing tanks.

Lives in shipping container on carrier. 

Assembles in about 20 minute', ), 3 => array ( 'postID' => 4690, 'registration' => '28-480', 'type' => 'D9', 'serial_number' => '', 'owner' => '', 'email' => '', 'web1' => NULL, 'web2' => NULL, 'web3' => NULL, 'based' => 'Australia', 'notes' => '1835cc VW powered', ), 4 => array ( 'postID' => 4689, 'registration' => '28-1823', 'type' => 'D9', 'serial_number' => '', 'owner' => 'Keith Jordan', 'email' => '', 'web1' => NULL, 'web2' => NULL, 'web3' => NULL, 'based' => 'Queensland, Australia', 'notes' => 'Powered by a Jabiru, 80 hp, 2.2 litre engine. 

120 knots straight and level!', ), 5 => array ( 'postID' => 4688, 'registration' => '62-AAD', 'type' => 'D9', 'serial_number' => '', 'owner' => '', 'email' => '', 'web1' => NULL, 'web2' => NULL, 'web3' => NULL, 'based' => ' St. Omer, France', 'notes' => 'Photograph location: St. Omer, France', ), 6 => array ( 'postID' => 4687, 'registration' => '62-ACH', 'type' => 'D9', 'serial_number' => '', 'owner' => '', 'email' => '', 'web1' => NULL, 'web2' => NULL, 'web3' => NULL, 'based' => 'St. Omer, France', 'notes' => 'Photograph location: St. Omer, France', ), 7 => array ( 'postID' => 4686, 'registration' => '801-Y', 'type' => 'D9', 'serial_number' => '', 'owner' => '', 'email' => '', 'web1' => NULL, 'web2' => NULL, 'web3' => NULL, 'based' => 'St. Omer, France', 'notes' => 'Photograph location: St. Omer, France





', ), 8 => array ( 'postID' => 4685, 'registration' => 'AKR-006', 'type' => 'D9', 'serial_number' => '', 'owner' => 'Chas Kenny', 'email' => 'chassam@slingshot.co.nz', 'web1' => NULL, 'web2' => NULL, 'web3' => NULL, 'based' => 'Clydevale, Southern New Zealand.', 'notes' => 'Has now been flying untouched for nearly 20 years. 

Engine is 1800cc type 4 VW.', ), 9 => array ( 'postID' => 4684, 'registration' => 'CF-RFK', 'type' => 'D9', 'serial_number' => '', 'owner' => '', 'email' => 'pearceterry@shaw.ca', 'web1' => NULL, 'web2' => NULL, 'web3' => NULL, 'based' => '', 'notes' => '1963 built D9 Model PMS1. It is now in a museum in Canada.

It has total time of about 240 hours. Powered by a C65 engine. Quite a performer. Has no tail wheel, just a skid. Other notables: larger rudder for the larger horsepower, wing dihedral is reduced to 7.5 degrees from 15 degrees and fuel tank is behind the pilot for balance of the larger engine.

This picture was taken in 1996 as CF-RFK was being made ready for the museum.

As the kit was from Falconar Aircraft in YEG it is best classified as a Falconar.

', ), 10 => array ( 'postID' => 4683, 'registration' => 'F-PHJS', 'type' => 'D9', 'serial_number' => '', 'owner' => '', 'email' => '', 'web1' => NULL, 'web2' => NULL, 'web3' => NULL, 'based' => '', 'notes' => 'Photograph location: Beaune 1987. 

Photograph supplied by Bob Horrell.', ), 11 => array ( 'postID' => 4682, 'registration' => 'F-PIYP', 'type' => 'D9', 'serial_number' => '398', 'owner' => '', 'email' => '', 'web1' => NULL, 'web2' => NULL, 'web3' => NULL, 'based' => '', 'notes' => 'VW powered.

Photograph supplied by Bob Horrell.', ), 12 => array ( 'postID' => 4681, 'registration' => 'F-FMXU', 'type' => 'D9', 'serial_number' => '', 'owner' => '', 'email' => '', 'web1' => NULL, 'web2' => NULL, 'web3' => NULL, 'based' => '', 'notes' => 'Pons, France. 15th August. 1970. 

Photograph by Alain Nouge.', ), 13 => array ( 'postID' => 4680, 'registration' => 'F-POIY', 'type' => 'D9', 'serial_number' => '', 'owner' => '', 'email' => '', 'web1' => NULL, 'web2' => NULL, 'web3' => NULL, 'based' => '', 'notes' => 'RSA Laval, France. 1969. 

Photograph by Alain Nouge', ), 14 => array ( 'postID' => 4679, 'registration' => 'F-PEVD', 'type' => 'D9', 'serial_number' => '', 'owner' => 'Christian Rose', 'email' => 'jodelrose@orange.fr', 'web1' => NULL, 'web2' => NULL, 'web3' => NULL, 'based' => '', 'notes' => '', ), 15 => array ( 'postID' => 4678, 'registration' => 'G-BDEI', 'type' => 'D9', 'serial_number' => '936', 'owner' => 'Richard Newns', 'email' => '', 'web1' => '', 'web2' => '', 'web3' => '', 'based' => '', 'notes' => 'Photograph location: Popham, UK. 

S/N PFA 936, 1977. VW 1600 powered. 

Photograph by Stuart MacConnacher, 2004.. 

', ), )

Open in new window


If I populate the array with the first 17 posts I get
arraykey: 9 Registration: CF-RFK

array ( 0 => array ( 'postID' => 4700, 'registration' => 'G-TEST', 'type' => 'D9', 'serial_number' => '12345', 'owner' => 'Joe Bloggs', 'email' => 'joe@bloggs.com', 'web1' => 'http://www.joebloggsaircraft.com', 'web2' => '', 'web3' => '', 'based' => 'Bradford', 'notes' => '', ), 9 => array ( 'postID' => 4684, 'registration' => 'CF-RFK', 'type' => 'D9', 'serial_number' => '', 'owner' => '', 'email' => 'pearceterry@shaw.ca', 'web1' => NULL, 'web2' => NULL, 'web3' => NULL, 'based' => '', 'notes' => '1963 built D9 Model PMS1. It is now in a museum in Canada.

It has total time of about 240 hours. Powered by a C65 engine. Quite a performer. Has no tail wheel, just a skid. Other notables: larger rudder for the larger horsepower, wing dihedral is reduced to 7.5 degrees from 15 degrees and fuel tank is behind the pilot for balance of the larger engine.

This picture was taken in 1996 as CF-RFK was being made ready for the museum.

As the kit was from Falconar Aircraft in YEG it is best classified as a Falconar.

', ), 15 => array ( 'postID' => 4678, 'registration' => 'G-BDEI', 'type' => 'D9', 'serial_number' => '936', 'owner' => 'Richard Newns', 'email' => '', 'web1' => '', 'web2' => '', 'web3' => '', 'based' => '', 'notes' => 'Photograph location: Popham, UK. 

S/N PFA 936, 1977. VW 1600 powered. 

Photograph by Stuart MacConnacher, 2004.. 

', ), 14 => array ( 'postID' => 4679, 'registration' => 'F-PEVD', 'type' => 'D9', 'serial_number' => '', 'owner' => 'Christian Rose', 'email' => 'jodelrose@orange.fr', 'web1' => NULL, 'web2' => NULL, 'web3' => NULL, 'based' => '', 'notes' => '', ), 13 => array ( 'postID' => 4680, 'registration' => 'F-POIY', 'type' => 'D9', 'serial_number' => '', 'owner' => '', 'email' => '', 'web1' => NULL, 'web2' => NULL, 'web3' => NULL, 'based' => '', 'notes' => 'RSA Laval, France. 1969. 

Photograph by Alain Nouge', ), 12 => array ( 'postID' => 4681, 'registration' => 'F-FMXU', 'type' => 'D9', 'serial_number' => '', 'owner' => '', 'email' => '', 'web1' => NULL, 'web2' => NULL, 'web3' => NULL, 'based' => '', 'notes' => 'Pons, France. 15th August. 1970. 

Photograph by Alain Nouge.', ), 11 => array ( 'postID' => 4682, 'registration' => 'F-PIYP', 'type' => 'D9', 'serial_number' => '398', 'owner' => '', 'email' => '', 'web1' => NULL, 'web2' => NULL, 'web3' => NULL, 'based' => '', 'notes' => 'VW powered.

Photograph supplied by Bob Horrell.', ), 10 => array ( 'postID' => 4683, 'registration' => 'F-PHJS', 'type' => 'D9', 'serial_number' => '', 'owner' => '', 'email' => '', 'web1' => NULL, 'web2' => NULL, 'web3' => NULL, 'based' => '', 'notes' => 'Photograph location: Beaune 1987. 

Photograph supplied by Bob Horrell.', ), 8 => array ( 'postID' => 4685, 'registration' => 'AKR-006', 'type' => 'D9', 'serial_number' => '', 'owner' => 'Chas Kenny', 'email' => 'chassam@slingshot.co.nz', 'web1' => NULL, 'web2' => NULL, 'web3' => NULL, 'based' => 'Clydevale, Southern New Zealand.', 'notes' => 'Has now been flying untouched for nearly 20 years. 

Engine is 1800cc type 4 VW.', ), 1 => array ( 'postID' => 4692, 'registration' => '05FM', 'type' => 'D9', 'serial_number' => '', 'owner' => 'Pierre Bourgeois', 'email' => 'Peibo@aol.com', 'web1' => NULL, 'web2' => NULL, 'web3' => NULL, 'based' => 'South of France', 'notes' => 'Powered by a VW 1600cc', ), 7 => array ( 'postID' => 4686, 'registration' => '801-Y', 'type' => 'D9', 'serial_number' => '', 'owner' => '', 'email' => '', 'web1' => NULL, 'web2' => NULL, 'web3' => NULL, 'based' => 'St. Omer, France', 'notes' => 'Photograph location: St. Omer, France





', ), 6 => array ( 'postID' => 4687, 'registration' => '62-ACH', 'type' => 'D9', 'serial_number' => '', 'owner' => '', 'email' => '', 'web1' => NULL, 'web2' => NULL, 'web3' => NULL, 'based' => 'St. Omer, France', 'notes' => 'Photograph location: St. Omer, France', ), 5 => array ( 'postID' => 4688, 'registration' => '62-AAD', 'type' => 'D9', 'serial_number' => '', 'owner' => '', 'email' => '', 'web1' => NULL, 'web2' => NULL, 'web3' => NULL, 'based' => ' St. Omer, France', 'notes' => 'Photograph location: St. Omer, France', ), 4 => array ( 'postID' => 4689, 'registration' => '28-1823', 'type' => 'D9', 'serial_number' => '', 'owner' => 'Keith Jordan', 'email' => '', 'web1' => NULL, 'web2' => NULL, 'web3' => NULL, 'based' => 'Queensland, Australia', 'notes' => 'Powered by a Jabiru, 80 hp, 2.2 litre engine. 

120 knots straight and level!', ), 3 => array ( 'postID' => 4690, 'registration' => '28-480', 'type' => 'D9', 'serial_number' => '', 'owner' => '', 'email' => '', 'web1' => NULL, 'web2' => NULL, 'web3' => NULL, 'based' => 'Australia', 'notes' => '1835cc VW powered', ), 2 => array ( 'postID' => 4691, 'registration' => '19-3924', 'type' => 'D9', 'serial_number' => '19-3924', 'owner' => 'Lyle Passfield', 'email' => 'lyle.passfield@gmail.com', 'web1' => NULL, 'web2' => NULL, 'web3' => NULL, 'based' => 'Australia', 'notes' => 'Jabiru 2.2 engine. Fuselage widened 100mm. 

Wing area increased to 9.5sq m. Profile changed to D18, BRS, wing tanks.

Lives in shipping container on carrier. 

Assembles in about 20 minute', ), 16 => array ( 'postID' => 4677, 'registration' => 'G-KDIX', 'type' => 'D9', 'serial_number' => '', 'owner' => 'D. J. Wells', 'email' => '', 'web1' => NULL, 'web2' => NULL, 'web3' => NULL, 'based' => '', 'notes' => 'Photograph location: Fenland, UK. 

Construction no: PFA / 54 - 10293', ), )

Open in new window

Note (line 1 of the output) uses the same array-search criteria but different results are returned when the array has 16 and 17 posts
Look at what you get when you sort your arrays
http://www.marcorpsa.com/ee/t2970.php

Look at the data you posted - the arrays are not sorted - so you are getting different results.

Full listing of the above here
t2970.php
t2970-15.php
t2970-16.php
SOLUTION
Avatar of Jim Riddles
Jim Riddles
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks both.

So just to clarify please.
This is how my array is built
$aircraft_data[] = array(
	'postID' => $postID,
	'registration' => get_the_title($postID),
	'type' => get_field('type', $postID),
	'serial_number' => get_field('serial_number',$postID),
	'owner' => get_field('owner', $postID),
	'email' => get_field('email', $postID),
	'web1' => get_field('web1', $postID),
	'web2' => get_field('web2', $postID),
	'web3' => get_field('web3', $postID),
	'based' => get_field('based', $postID),
	'notes' => get_field('notes', $postID)
);	

Open in new window

and using Jim R's approach I have successfully searched the array using this code ($var holds the aircraft registration)
foreach ($aircraft_data as $key => $info):
	if (array_search($var, $info)): break; endif;
endforeach;

Open in new window


I have also re-introduced the sort rountine
uasort($aircraft_data, function($a, $b) {
if ($a['registration'] == $b['registration']) return 0;
	return ($a['registration'] < $b['registration'])?-1:1;
});

Open in new window

Would this mean that
$arraykey = array_search('05FM', array_column($aircraft_data,"registration"));

Open in new window

would now produce the correct results or is it that I can't use this approach given the build of my array?
Just sort your array.
I load the array like this
function getAircraftDataFromDB()
{
	/*	Get aircraft data */	
	static $aircraft_data = false;
	
	if (!$aircraft_data):
		$aircraft_posts = get_posts(
		array(
			'post_type'      => 'aircraft',
			'post_status'    => 'publish',
			'posts_per_page' => - 1,
			'fields'         => 'ids',
		) );
		foreach( $aircraft_posts as $postID ):
			$aircraft_data[] = array(
					'postID' => $postID,
					'registration' => get_the_title($postID),
					'type' => get_field('type', $postID),
					'serial_number' => get_field('serial_number',$postID),
					'owner' => get_field('owner', $postID),
					'email' => get_field('email', $postID),
					'web1' => get_field('web1', $postID),
					'web2' => get_field('web2', $postID),
					'web3' => get_field('web3', $postID),
					'based' => get_field('based', $postID),
					'notes' => get_field('notes', $postID)
				);		
		endforeach;
	endif;

	uasort($aircraft_data, function($a, $b) {
	if ($a['registration'] == $b['registration']) return 0;
		return ($a['registration'] < $b['registration'])?-1:1;
	});

	return $aircraft_data;
}

Open in new window

and read it with this
	$aircraft_data = getAircraftDataFromDB();
	$arraykey = array_search('05FM', array_column($aircraft_data,"registration"));
	echo 'arraykey: ',$arraykey , ' Registration: ', $aircraft_data[$arraykey]['registration'],'<br>';

Open in new window


and get arraykey: 0 Registration: G-TEST as output
Is my sort wrong?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Many thanks both. For the array where I can assign the unique registration to the key (aircraft_data), Julian's solution is ideal. Where my other array comprises of media attachments I can't use the registration as key because there are duplicates. In this case I will use Jim's solution.
Both great answers to my problem and it's moved me on a great deal in the development of my website and my understanding of array manipulation.
Cheers
At the risk of putting this question on life support - why are you doing this in your code (function getAircraftDataFromDB())
uasort($aircraft_data, function($a, $b) {
	if ($a['registration'] == $b['registration']) return 0;
		return ($a['registration'] < $b['registration'])?-1:1;
	});

Open in new window

Why not just get the posts and order them when you get them
$aircraft_posts = get_posts(array(
  'post_type'      => 'aircraft',
  'post_status'    => 'publish',
  'posts_per_page' => - 1,
  'fields'         => 'ids',
  'orderby'        => 'title',
  'order'          => 'ASC'
));

Open in new window

This way the posts come out already sorted using a DB sort which is far better and array keys will be in the right order which mean your original code for searching for the registration should work as expected.
Thanks. I hadn't looked into the options of get_posts so orderby will come in handy. There are limited options for orderby aren't there? As far as I can see I can't orderby an ACF field can I?