0

I have an array like this on php :

Array
(
[0] => Array
    (
        [CONSIGNEE] => PT. XX
        [CONTAINER] => DFSU1587848

    )

[1] => Array
    (
        [CONSIGNEE] => PT. YYY
        [CONTAINER] => TEMU2365554
    )

[2] => Array
    (
        [CONSIGNEE] => PT. ZZZ
        [CONTAINER] => CBHU5788073

    )

[3] => Array
    (
        [CONSIGNEE] => PT. HHH
        [CONTAINER] => CBHU5788073
    )

[4] => Array
    (
        [CONSIGNEE] => PT. OOO
        [CONTAINER] => CBHU3884376
    )
)

Please see in element CONTAINER.

How can I know that on those array just have 4 item container, which is,

DFSU1587848, TEMU2365554, CBHU5788073, CBHU3884376.

If php have a built function like that ?

2
  • I could be understanding you wrong but won't if (count($array) == 4) { do what you want? Commented May 17, 2016 at 14:43
  • I just wanna to say to user, that you have 4 container. I just need this letter "4" Commented May 17, 2016 at 14:45

4 Answers 4

2
$containers = array_unique(array_column($data, 'CONTAINER'));
$count = count($containers);
  1. array_column gets a column in a 2-dimensional array.
  2. array_unique removes duplicates from the array.
  3. count counts all the elements in the array.
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, it so helpfull for beginner like me
0

You can use an array_map for that:

$yourArray = [
    ['x' => 'a', 'CONTAINER' => 'aa'],
    ['x' => 'a', 'CONTAINER' => 'bb'],
    ['x' => 'a', 'CONTAINER' => 'cc'],
    ['x' => 'a', 'CONTAINER' => 'aa'],
    ['x' => 'a', 'CONTAINER' => 'aa'],
    ['x' => 'a', 'CONTAINER' => 'bb'],
];


$arr = array_unique(array_map(function($x){ return $x['CONTAINER']; }, $yourArray));
echo (count($arr));

Itt will echos 3.

Comments

0

It's a bit unclear what you're asking - from what I understand, you wish to create an array with the CONTAINER values in this array.

You can do this by looping through the array and adding those values to a new array, like so:

//Set up new array
$containers = [];

//Loop through existing array
foreach($array as $a){

    //See if this array has a "container" key
    if($a["CONTAINER"]){

        //Push the new Container value to the $containers array
        $containers[] = $a["CONTAINER"];

    }

}

//Output the finished product
print_r($containers);

Once this is done, to count the number of elements with the CONTAINER key:

$num = count($containers);

For just the count of CONTAINER keys:

$n = 0;
foreach($array as $a){
    if($a["container"]) $n++;
}

echo $n;

1 Comment

I just wanna to say to user, that you have 4 container. I just need this letter "4"
0

To get all CONTAINER values do the following:

$key = 'CONTAINER';
$containers = array_map(function($item) use ($key) {
    return $item[$key];
}, $array);

print_r($containers);

If you just want to know how much CONTAINER values there are, do a count() after the above code.

echo count($containers);

Comments

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.