0

I have two array as bellow :

First array :

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [name] => one
                    [number] => 051
                )

            [1] => Array
                (
                    [name] => two
                    [number] => 052
                )

            [2] => Array
                (
                    [name] => three
                    [number] => 053
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [name] => four
                    [number] => 061
                )

            [1] => Array
                (
                    [name] => five
                    [number] => 062
                )

        )

)

I want to make output from first array above

[0] => 051, 052, 053.
[1] => 061, 062.

    Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [name] => book
                    [number] => 41
                )

            [1] => Array
                (
                    [name] => pencil
                    [number] => 42
                )

        )

    [1] => Array
        (
            [name] => eraser
            [number] => 71
        )

)

I want to make output from second array above

[0] => 41, 42.
[1] => 71.

Please advise. Thank you.

2
  • 1
    and what have you tried so far? Commented Nov 30, 2018 at 15:28
  • I wonder how a question showing no effort gets an upvote?! Commented Nov 30, 2018 at 15:58

2 Answers 2

0

You can make a try like this way with two foreach() loop.

$numbers = [];
foreach ($array as $k => $v) {
    $num = [];
    foreach ($v as $k2 => $v2) {
       $num[] = $v2['number']; 
    }
     $numbers[$k] = implode(',',$num).'.';
}

print_r($numbers);

DEMO: https://3v4l.org/mEeO7

Sign up to request clarification or add additional context in comments.

Comments

0

you can try something like this

$arr = Array (
    Array (
            Array (
                    "name" => "one",
                    "number" => "051"
                ),
            Array  (
                    "name" => "two",
                    "number" => "052"
                ),
            Array (
                    "name" => "three",
                    "number" => "053"
                )
        ),

    Array (
            Array (
                    "name" => "four",
                    "number" => "061"
                ),
            Array  (
                    "name" => "five",
                    "number" => "062"
                )
        )
);

foreach ($arr as $k => $s_arr) {
    echo "[" . $k . "] => ";
    foreach ($s_arr as $k2 => $v2) {
        echo $v2["number"] . " ";
    }

    echo "\n";
}

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.