MeshWorld India LogoMeshWorld.

PHP Interview Questions

Listen to ArticleAI Speech
~3 min read narration
100%
PHP Interview Questions

What are the differences between echo and print in PHP?

  • Both statements print and echo are used to output/print data on the screen.
  • The echo statement does not return any value whereas print always returns an integer value (1).
  • The echo accepts multiple arguments whereas print only accepts a single argument.
  • Both echo and print statements can be used with or without parentheses ().

During development, the developer needs to go through loops to prepare a single dimensional array out of a multi-dimensional array.

Laravel provides many helpers which come handy in multiple ways and saves developers work.

One of such is Arr::flatten() which flattens a multi-dimensional array into a single level by default.

Tested with Laravel versions 5.8, 6 and 7.

We need to import Illuminate\Support\Arr namespace with use keyword.

Kindly check for your version on the official DOC for Laravel Helper Arr::flatten(). This link will take you to Laravel 7 Documentation.

Syntax

Arr::flatten(array $array, [int $depth = INF])

Parameters

$array (necessary)

  • The first parameter is necessary and must be an array that needs to be flattened.
  • Laravel will extract elements and provides us with a new single dimension array based on the depth parameter.

$depth (optional)

  • The second parameter specifies the depth and it’s an optional parameter.
  • This value must be an integer type.
  • The depth specifies how deep a nested array should be flattened.
  • The default value for depth is infinity(INF) from which Laravel will extract elements and provides us with a new single dimension array.

Return value

It returns a new 1-D flattened array.

Example

$data = [
    'country' => 'India 🇮🇳',
    'languages' => [
        'Gujarati',
        'Hindi',
        'Sanskrit',
        'Tamil',
        'Urdu',
    ],
];

dd(
	Arr::flatten($data)
);

Output

array:3 [
  0 => "India 🇮🇳"
  1 => "Gujarati"
  2 => "Hindi"
  3 => "Sanskrit"
  4 => "Tamil"
  5 => "Urdu"
]

Deeper Nested Arrays

This method also works on our specified level deep. We simply need to pass the appropriate depth parameter in order to flatten deeper multi-dimensional or nested arrays.

Example with 2 levels deep

$data = [[12, [2, 4], 124, 7, 10, 8, [15, [6, 7]]]];

dd(
    Arr::flatten($data, 2)
);

Output for 2 levels deep

array:9 [
  0 => 12
  1 => 2
  2 => 4
  3 => 124
  4 => 7
  5 => 10
  6 => 8
  7 => 15
  8 => array:2 [
    0 => 6
    1 => 7
  ]
]

Example with INF level deep

$data = [[12, [2, 4], 124, 7, 10, 8, [15, [6, 7]]]];

dd(
    Arr::flatten($data, INF)
);

Output for INF level deep

array:10 [
  0 => 12
  1 => 2
  2 => 4
  3 => 124
  4 => 7
  5 => 10
  6 => 8
  7 => 15
  8 => 6
  9 => 7
]

Resources

Happy 😄 coding

With ❤️ from 🇮🇳

Reader Quality Feedback

Did this technical guide help solve your problem?

Suggest Errata ($0)
Vishnu
Primary Author

Vishnu

Founder & Principal Architect at MeshWorld. Senior engineer and instructor specializing in AI agent systems, scalable web architecture, and modern development workflows.

Explore Author Archive
Compute Fuel & Open Testbed
100% Independent & Verified

Fuel High-Density, Zero-Fluff Engineering Deep-Dives

Every guide on MeshWorld is validated on physical Linux nodes and reproducible testbeds. If this article saved you hours of debugging or unblocked production, consider funding our next cluster run.

Weekly Dispatch

Join MeshWorld Dispatch

Get practical tutorials, system blueprints, and curated AI engineering notes straight to your inbox. No fluff, zero spam.

Zero spam. 1-click unsubscribe anytime.Prefer RSS?
Curated Continuations

Up Next in This Domain.

Browse Full Archive