• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 75
  • Last Modified:

How to get previous and next values from an array?

I have an array which is populated from a manually maintained post. The posts consist of two fields; an item description and a sequence number. The sequence of the items in the post correspond to the order in which the items appear in the menu (they're not ordered by string or numeric but rather by the order that I choose).
I read all the posts into an array and end up with:
Key    Sequence_number  Item_Desc
4750   1                                  D9
4751   2                                  D11
4752   3                                  D18
4753   4                                  D19
etc.

I sort the array by sequence number to make sure it's in the sequence number order.

When a user selects the D11 page from the menu and the page is displayed, I would also like to display the previous page link (D9) and the next page link (D18).
The only way I could see to do this would be to maintain an array and then I would be able to create a link to the next and previous pages.
Without having to loop through all values in the array to find the entry for the currently displayed page, is there a way of interrogating the array by value (e.g. D11) to obtain its array key?
0
ClintonK
Asked:
ClintonK
  • 6
  • 5
3 Solutions
 
Jim RiddlesPrepress/OMS SpecialistCommented:
You can use the array_search() function in conjunction with the array_column() function.  See code below:
$a=array(
    array(
        "Key" => "4750",
        "Sequence_number" => "1",
        "Item_Desc" => "D9"
        ),
    array(
        "Key" => "4751",
        "Sequence_number" => "2",
        "Item_Desc" => "D11"
        ),
    array(
        "Key" => "4752",
        "Sequence_number" => "3",
        "Item_Desc" => "D18"
        ),
    array(
        "Key" => "4753",
        "Sequence_number" => "4",
        "Item_Desc" => "D19"
        )
);
$key = array_search("D18", array_column($a,"Item_Desc"));

echo $key;

Open in new window

I start off by replicating your array, then I use the array_search() function to search for the index for the value D18 in the column Item_Desc.  The above code will output 2...as D18 is in the third position in the $a array.

I hope this helps, let me know if you have any questions.
0
 
ClintonKAuthor Commented:
I think that's just what I'm looking for but I'm not sure I've populated my array correctly to use what you suggest. (I have to admit I'm struggling to get my head around how arrays are addressed - I'm old to IT but new to Wordpress and PHP). This is the code I'm using to populate the array:
      $aircraft_type_ids = get_posts(
      array(
            'post_type'      => 'aircraft_type',
            'post_status'    => 'publish',
            'posts_per_page' => - 1,
            'fields'         => 'ids',
      ) );

foreach($aircraft_type_ids as $aircraft_type_ID):
      $aircraft_types[$aircraft_type_ID] = get_field('sequence_number', $aircraft_type_ID);
endforeach;
0
 
Jim RiddlesPrepress/OMS SpecialistCommented:
So, $aircraft_type_ids is your actual array?  If so, please dump the array once it is completed so we can see the structure.  Add the following once your have completed building your array:
var_dump($aircraft_type_ids);

Open in new window

Post the results...that will display the structure of your array.  Additionally, anytime you post code, please use the CODE tag.  Activate that by clicking the CODE option from the menu above your post, and pasting your code in there.  Thanks!
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
ClintonKAuthor Commented:
When I run this code
	$aircraft_type_ids = get_posts(
	array(
		'post_type'      => 'aircraft_type',
		'post_status'    => 'publish',
		'posts_per_page' => - 1,
		'fields'         => 'ids',
	) );

	var_dump($aircraft_type_ids);

Open in new window

I get this result:

array(27) { [0]=> int(4778) [1]=> int(4777) [2]=> int(4776) [3]=> int(4775) [4]=> int(4774) [5]=> int(4773) [6]=> int(4772) [7]=> int(4771) [8]=> int(4770) [9]=> int(4769) [10]=> int(4768) [11]=> int(4767) [12]=> int(4766) [13]=> int(4765) [14]=> int(4764) [15]=> int(4763) [16]=> int(4762) [17]=> int(4761) [18]=> int(4760) [19]=> int(4759) [20]=> int(4758) [21]=> int(4757) [22]=> int(4756) [23]=> int(4753) [24]=> int(4752) [25]=> int(4751) [26]=> int(4750) }
0
 
Jim RiddlesPrepress/OMS SpecialistCommented:
Okay, that looks like it is simply an array of the aircrat IDs.  What I am looking for is the array you have that has the information in your original post.  Can you provide a var_dump() of that array?
0
 
ClintonKAuthor Commented:
The array that I put in my original post was what I thought I had but actually turns out that I haven't got that at all.
I think my first problem is that I can't work out how to get the array built - all I seem to be able to manage is
Key    Sequence_Number

I can't work out how to get an array that contains the key, sequence_number AND the Item_Desc (which is the aircraft ID as you correctly say). It looks like a multidimensional array to my untrained eye.
This bit of code creates an array with a key of the postID and and a value of the sequence_number but how can I add the aircraft_ID too?

foreach($aircraft_type_ids as $aircraft_type_ID):
	$aircraft_types[$aircraft_type_ID] = get_field('sequence_number', $aircraft_type_ID);
endforeach;

Open in new window

0
 
Jim RiddlesPrepress/OMS SpecialistCommented:
Okay, before you get much further, I would strongly suggest reading up on PHP arrays.  W3Schools has an excellent tutorial located here.

That said, you can look at the code that I posted to see how I built an array that matches your specs.  Although I built that array manually, you can build it programmatically.

Going by your original post, and your followup var_dump of $aircraft_type_ids, it looks like the $aircraft_type_ids array actually contains the "Key" value from your original post.  Is that correct?  If so, it appears that you can use that to get the Sequence_number value via the get_field() function.  Can you also use the get_field() function to get the Item_Desc value?

If the answer is yes, then this is what I would propose to build the array that you showed in your original post:
foreach ($aircraft_type_ids as $aircraft_type_ID) {
    array_push($array, array(
        "Key" => $aircraft_type_ID,
        "Sequence_number" => get_field('sequence_number', $aircraft_type_ID),
        "Item_Desc" => get_field('item_desc', $aircraft_type_ID)
    ));
}

var_dump($array);

Open in new window

The above code makes the assumption that the get_field() function will allow you to get the value of item_desc in the same way that you get the value of the sequence_number.
0
 
ClintonKAuthor Commented:
Thanks for the link to W3 Schools. I have read many articles and watched many tutorials on PHP arrays but I still have a bit of a mental block with them - I'm sure I'll get there eventually.

Your code looks like it'll do exactly what I'm looking for but I'm still having a few problems.
The code
foreach ($aircraft_type_ids as $aircraft_type_ID) {
	echo $aircraft_type_ID . '&nbsp' . get_field('sequence_number', $aircraft_type_ID) . '&nbsp' . get_the_title($aircraft_type_ID) .'<br>';
}

Open in new window

produces the output:
4778 27 TAILLEFER DT-01
4777 26 200P
4776 25 FALCONAR
4775 24 DR1054
4774 23 DR1053
4773 22 DR1051
... etc
but the code
foreach ($aircraft_type_ids as $aircraft_type_ID) {
	array_push($array, array(
		"Key" => $aircraft_type_ID,
		"Sequence_number" => get_field('sequence_number', $aircraft_type_ID),
		"Type" => get_the_title($aircraft_type_ID)
	));
}
var_dump($array);

Open in new window

produces the output
NULL
0
 
Jim RiddlesPrepress/OMS SpecialistCommented:
Try modifying the code as follows:
foreach ($aircraft_type_ids as $aircraft_type_ID) {
	$array[] = array(
		"Key" => $aircraft_type_ID,
		"Sequence_number" => get_field('sequence_number', $aircraft_type_ID),
		"Type" => get_the_title($aircraft_type_ID)
	);
}
var_dump($array);

Open in new window

What output does that produce?
0
 
ClintonKAuthor Commented:
Very nearly there!
This code
	$aircraft_type_ids = get_posts(
	array(
		'post_type'      => 'aircraft_type',
		'post_status'    => 'publish',
		'posts_per_page' => - 1,
		'fields'         => 'ids',
	) );

foreach ($aircraft_type_ids as $aircraft_type_ID) {
	$array[$aircraft_type_ID] = array(
		"Sequence_number" => get_field('sequence_number', $aircraft_type_ID),
		"Type" => get_the_title($aircraft_type_ID)
	);
}

uasort($array, function($a, $b) {
    return $a['Sequence_number'] - $b['Sequence_number'];
});

echo '<pre>', print_r($array,true) .'</pre>';

Open in new window

Produces this output:
Array
(
    [4750] => Array
        (
            [Sequence_number] => 1
            [Type] => D9
        )
    [4751] => Array
        (
            [Sequence_number] => 2
            [Type] => D11
        )
    [4752] => Array
        (
            [Sequence_number] => 3
            [Type] => D18
        )
    [4753] => Array
        (
            [Sequence_number] => 4
            [Type] => D19
        )
    [4756] => Array
        (
            [Sequence_number] => 5
            [Type] => D20
        )
    [4757] => Array
        (
            [Sequence_number] => 6
            [Type] => D92
        )
    [4758] => Array
        (
            [Sequence_number] => 7
            [Type] => DR100
        )
    [4759] => Array
        (
            [Sequence_number] => 8
            [Type] => D112
        )
    [4760] => Array
        (
            [Sequence_number] => 9
            [Type] => D113
        )
    [4761] => Array
        (
            [Sequence_number] => 10
            [Type] => D117
        )
    [4762] => Array
        (
            [Sequence_number] => 11
            [Type] => D118
        )
    [4763] => Array
        (
            [Sequence_number] => 12
            [Type] => D119
        )
    [4764] => Array
        (
            [Sequence_number] => 13
            [Type] => D120
        )
    [4765] => Array
        (
            [Sequence_number] => 14
            [Type] => D140
        )
    [4766] => Array
        (
            [Sequence_number] => 15
            [Type] => D150
        )
    [4767] => Array
        (
            [Sequence_number] => 16
            [Type] => D200
        )
    [4768] => Array
        (
            [Sequence_number] => 17
            [Type] => D220
        )
    [4769] => Array
        (
            [Sequence_number] => 18
            [Type] => D221
        )
    [4770] => Array
        (
            [Sequence_number] => 19
            [Type] => D250
        )
    [4771] => Array
        (
            [Sequence_number] => 20
            [Type] => DH251
        )
    [4772] => Array
        (
            [Sequence_number] => 21
            [Type] => DR1050
        )
    [4773] => Array
        (
            [Sequence_number] => 22
            [Type] => DR1051
        )
    [4774] => Array
        (
            [Sequence_number] => 23
            [Type] => DR1053
        )
    [4775] => Array
        (
            [Sequence_number] => 24
            [Type] => DR1054
        )
    [4776] => Array
        (
            [Sequence_number] => 25
            [Type] => FALCONAR
        )
    [4777] => Array
        (
            [Sequence_number] => 26
            [Type] => 200P
        )
    [4778] => Array
        (
            [Sequence_number] => 27
            [Type] => TAILLEFER DT-01
        )
)

Which is exactly what I want. However, back to my original question of "how to get the postID from an array value" I added this code
$key = array_search("D18", array_column($array,"Type"));
echo $key;

Open in new window

and I was expecting 4752 but I get 2 instead.
I realise that 2 is the third entry in the array which is correct but I really want the key value which should be the postID
0
 
hieloCommented:
Try:
...
$array=Array();
foreach ($aircraft_type_ids as $aircraft_type_ID) {
      $array[] = array('id'=>$aircraft_type_ID,
            "Sequence_number" => get_field('sequence_number', $aircraft_type_ID),
            "Type" => get_the_title($aircraft_type_ID)
      );
}
...
$key = array_search("D18", array_column($array,"Type",'id'));
echo $key;
0
 
Jim RiddlesPrepress/OMS SpecialistCommented:
You need to build your array the way that I showed you, otherwise you will make the job harder than it has to be.  Try the following:
foreach ($aircraft_type_ids as $aircraft_type_ID) {
	$array[] = array(
		"Key" => $aircraft_type_ID,
		"Sequence_number" => get_field('sequence_number', $aircraft_type_ID),
		"Type" => get_the_title($aircraft_type_ID)
	);
}
$key= $array[array_search("D18", array_column($array,"Type"))]["Key"];

Open in new window

What the above code does, is to build the array, then searches the array for a Type of "D18", and then accesses the Key value.
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.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now