Saint Robson
Saint Robson

Reputation: 5525

How To Get The Value Of Array, But Without Index

I have this array :

$order_list = array ( array ("tangible", 1, 8, 1, 19000),
                      array ("tangible", 6, 2, 10, NULL),
                      array ("tangible", 1, 17, 1, 28000));

and I want to group together the product_id ($order_list[2]) based on vendor_id ($order_list[1]) and I did it. it looks like this :

Array
(
    [1] => Array
        (
            [0] => Array
                (
                    [product_id] => 8
                    [pcs] => 1
                    [weight] => 115.00
                )

            [1] => Array
                (
                    [product_id] => 17
                    [pcs] => 1
                    [weight] => 120.00
                )

        )

    [6] => Array
        (
            [0] => Array
                (
                    [product_id] => 2
                    [pcs] => 10
                    [weight] => 250.00
                )

        )

)

now the problem is...

how to get the value of this new array while the index of this new array contains vendor_id?

I'm expecting 1 or 6 as result of $new_array[0] and $new_array[1] respectively. so I can create for loop. is it still possible to get that? thanks before.

UPDATE : I have this code get the value :

foreach ($order_array as $value) {
    echo '<pre>';
    print_r($value);
}

but, unfortunately I get this output as result :

Array
(
    [0] => Array
        (
            [product_id] => 8
            [pcs] => 1
            [weight] => 115.00
        )

    [1] => Array
        (
            [product_id] => 17
            [pcs] => 1
            [weight] => 120.00
        )

)
Array
(
    [0] => Array
        (
            [product_id] => 2
            [pcs] => 10
            [weight] => 250.00
        )

)

I still can't get 1 and 6 :-(

Upvotes: 0

Views: 380

Answers (1)

Chetan Ameta
Chetan Ameta

Reputation: 7896

add key field in foreach loop:

$order_list = Array
(
    1 => Array
    (
        0 => Array
        (
            'product_id' => 8,
            'pcs' => 1,
            'weight' => 115.00
        ),

        1 => Array
        (
            'product_id' => 17,
            'pcs' => 1,
            'weight' => 120.00
        )

    ),

    6 => Array
    (
        0 => Array
        (
            'product_id' => 2,
            'pcs' => 10,
            'weight' => 250.00
        )

    )

);

foreach ($order_list as $vendor_id => $value) {
    echo '<pre>';
    echo "Vendor Id: " . $vendor_id . '<br />';
    print_r($value);
}

output:

Vendor Id: 1
Array
(
    [0] => Array
        (
            [product_id] => 8
            [pcs] => 1
            [weight] => 115
        )

    [1] => Array
        (
            [product_id] => 17
            [pcs] => 1
            [weight] => 120
        )

)
Vendor Id: 6
Array
(
    [0] => Array
        (
            [product_id] => 2
            [pcs] => 10
            [weight] => 250
        )

)

Upvotes: 2

Related Questions