0

I have this PHP array:

$statuses = array(
  'delivery-ongoing' => array("status" => "at-10", "traffic" => "FCL", "type" => "export"),
  'delivered' => array("status" => "at-13", "traffic" => "FCL", "type" => "export"),
  'delivery-ongoing' => array("status" => "00--00", "traffic" => "FCL", "type" => "import"),
  'return-to-ongoing' => array("status" => "to-04", "traffic" => "FCL", "type" => "import"),
  'delivered' => array("status" => "at-13", "traffic" => "FCL", "type" => "import")
);

I have to select status by key "delivery-ongoing" where type = "import"

I can play with the array structure since is a constant within my class.

I tried

$statuses['delivery-ongoing']['status']

How do I get the right status for type = "import"

Is there a sort of loop I have to do or there is another way to do this?

4
  • use a foreach loop then if statement the $value['type'] field Commented May 30, 2017 at 16:02
  • 5
    Consider re-structuring your array. You can't have two identical keys in your array. The key delivery-ongoing appears twice. Commented May 30, 2017 at 16:02
  • @Janne Klouman: This is my problem. I know. That's why I have the type which makes the difference. Or I can do unique keys like this: delivery-ongoing-import and delivery-ongoing-export Commented May 30, 2017 at 16:04
  • 3
    You should not use the same key multiple times because "If multiple elements in the array declaration use the same key, only the last one will be used as all others are overwritten." form here. Commented May 30, 2017 at 16:13

3 Answers 3

1

You can use array_filter

$filtered = array_filter($statuses, function($value, $key) {
    return ($key == 'delivery-ongoing' && $value['type'] == 'import');
}, ARRAY_FILTER_USE_BOTH);

print_r($filtered);

Also, as it has been suggested in the comments, you can re-name your keys, maybe by appending an ID after the status.

Eq:

'delivery-ongoing-101' => array("status" => "at-10", "traffic" => "FCL", "type" => "export"),
Sign up to request clarification or add additional context in comments.

1 Comment

The ARRAY_FILTER_ flags were introduced in PHP version 5.6+ I will rename my key to be unique. Thanks
1

There are several problems with this array:

1- There's an error in a parenthesis that is closed too early at this line:

'return-to-ongoing' => array("status" => "to-04", "traffic" => "FCL"), "type" => "import",

2- If you define the same key two times on the same array, you won't be able to access the first element that was defined with this key. If you use a debugger, you will see that only 3 elements are available in your array, cause there are more than one that share the same key and only the last one is saved.

But to get the value that you are looking for, you can use this loop:

foreach ($statuses as $key => $value) {

    if($key == 'delivery-ongoing' && $value['type'] == 'import'){

        $result = $value['status'];

        break;
    }
}

The status for the type import is available at $result after the loop ends.

1 Comment

That was a typo. Thanks.
1

Your $statuses must have the following structure:

$statuses = array(
  'delivery-ongoing' => array(
    array("status" => "at-10", "traffic" => "FCL", "type" => "export"), 
    array("status" => "00--00", "traffic" => "FCL", "type" => "import")
  ),
  'delivered' => array(
    array("status" => "at-13", "traffic" => "FCL", "type" => "export"), 
    array("status" => "at-13", "traffic" => "FCL", "type" => "import")
    ),
  'return-to-ongoing' => array(array("status" => "to-04", "traffic" => "FCL", "type" => "import")),
);

Now, you can do what you want to do by doing:

 function filter_by_value ($array, $index, $value){ 
    if(is_array($array) && count($array)>0)  
    { 
        foreach(array_keys($array) as $key){ 
            $temp[$key] = $array[$key][$index]; 

            if ($temp[$key] == $value){ 
                $newarray[$key] = $array[$key]; 
            } 
        } 
      } 
  return $newarray; 
} 

$imported = filter_by_value($statuses['delivery-ongoing'], 'type', 'import');

print_r($imported);

1 Comment

That's useful. Thanks.

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.