user3455992
user3455992

Reputation: 101

how to get number out of an array key

I have the following array:

Array
(
    [Ingredient 1 Amount] => Array
        (
            [0] => 2
        )

    [Ingredient 1] => Array
        (
            [0] => lemon  juice (t fresh-squeezed)
        )

    [Ingredient 2 Amount] => Array
        (
            [0] => 3
        )

    [Ingredient 2] => Array
        (
            [0] => 1/2 cups peeled and diced potatoes
        )

    [Ingredient 3 Amount] => Array
        (
            [0] => 1/3
        )

    [Ingredient 3 Size] => Array
        (
            [0] => cup
        )

    [Ingredient 3] => Array
        (
            [0] => diced celery
        )

    [Ingredient 4 Amount] => Array
        (
            [0] => 1/3
        )

    [Ingredient 4 Size] => Array
        (
            [0] => cup
        )

    [Ingredient 4] => Array
        (
            [0] => finely chopped onion
        )

    [Ingredient 5 Amount] => Array
        (
            [0] => 5
        )

    [Ingredient 5 Size] => Array
        (
            [0] => tablespoons
        )

    [Ingredient 5] => Array
        (
            [0] => pickles
        )

)

Each ingredient set consists of Amount, Size and just the Default. There may be one of each or there may be some missing, so I can't just count the array and divide by 3. I need to know how many ingredient sets are in the array. This is shown by the last ingredient set, so in this case I would want the number "5". How would I go about getting that?

--EDIT--

The array is being built from custom post fields in wordpress. It's what I get back from

$post_meta = get_post_meta($id);

Upvotes: 1

Views: 80

Answers (4)

Fadey
Fadey

Reputation: 430

$a = array(
    array(['ingediant1'] => 
        array(
            'amount' => 1, 
            'description' => 'whatever'
            )
        )
);
$amount = 0;
foreach ($a as $ingediant => $info) {
    $amount += $info['amount'];
}

If you'd restructure your array to something like I did, you should be able to get the total amount of ingredients easy.

Upvotes: 0

Farnabaz
Farnabaz

Reputation: 4066

you can find maximum like this

$max = 0;
foreach($array as $key => $value) {
   if(intval(str_replace("Ingredient ", "", $key)) > $max) {
      $max = intval(str_replace("Ingredient ", "", $key));
   }
}

NOTICE: as Styphon mentioned in comments if you want to get last element number, it's not necessary to loop through whole array, only process last element

Upvotes: 0

Styphon
Styphon

Reputation: 10447

As I've mentioned in my comments restructuring your array would be far better, as your current structure isn't helpful for what you want to do. However, assuming that isn't an option this is what to do:

$key = end(array_keys($array));
$num = substr($key, 11);

You can see an example of this in work here on eval.in.


This will work if the last key won't always be the one you want:

$keyArr = array_keys($array);
$keyRev = array_reverse($keyArr);
foreach($keyRev as $key) {
  if(stripos($key, 'amount') === FALSE && stripos($key, 'size') === FALSE) {
    $num = substr($key, 11);
    break;
  }
}

echo $num;

You can see an example of this in work here on eval.in.

Upvotes: 0

uzerzero
uzerzero

Reputation: 156

In my opinion you should re-structure you're array. It is not optimised.

Try something along the lines of (this is psuedo code to give the idea)

[Ingredient 1] => Array
    (
        [type] => lemon  juice (t fresh-squeezed),
        [Amount] => lemon  juice (t fresh-squeezed),
        [Size] => lemon  juice (t fresh-squeezed)

 )

Upvotes: 1

Related Questions