PHP Array Unique Sort Regular not working as expected

I'm using print_r(array_unique($array, SORT_REGULAR)); on the array below but it does not work.

I'm using print_r(array_unique($array, SORT_REGULAR)); on the array below but it does not work.

I'm trying to filter out the redundant data.

Notice that [Order] and its key value pairs are all the same. But [Transaction] and its key value pairs are unique.

I need to get the [Order] element data and combine it with the 3 different [Transaction] elements.

My array

Array
(
    [0] => Array
        (
            [Order] => Array
                (
                    [PO] => TR11214
                    [OrderID] => 242856952012
                )
        [Transaction] => Array
            (
                [TransPO] => TR11211
                [TransactionPrice] => 91.17
            )

    )

[1] => Array
    (
        [Order] => Array
            (
                [PO] => TR11214
                [OrderID] => 242856952012
            )

        [Transaction] => Array
            (
                [TransPO] => TR11212
                [TransactionPrice] => 180.41
            )

    )

[2] => Array
    (
        [Order] => Array
            (
                [PO] => TR11214
                [OrderID] => 242856952012
            )

        [Transaction] => Array
            (
                [TransPO] => TR11213
                [TransactionPrice] => 209.99
            )

    )

)

The final array I need will look something like this.

Array
(
[Order] => Array
(
[PO] => TR11214
[OrderID] => 242856952012
)

[Transaction] => Array
    (
        [0] => Array
            (
                [TransPO] => TR11211
                [TransactionPrice] => 91.17
            )

        [1] => Array
            (
                [TransPO] => TR11212
                [TransactionPrice] => 180.41
            )

        [2] => Array
            (
                [TransPO] => TR11213
                [TransactionPrice] => 209.99
            )

    )

)

I can flatten the original array and then use array_unique, but wanted to see if there is a better way to accomplish what I need.

my code:

$myarray = array(
0 => array(
"Order" => array("PO" => "TR11214", "OrderID" => 242856952012),
"Transaction" => array("TransPO" => "TR11211", "TransactionPrice" => 91.17)
),
1 => array(
"Order" => array("PO" => "TR11214", "OrderID" => 242856952012),
"Transaction" => array("TransPO" => "TR11212", "TransactionPrice" => 180.41)
),
2 => array(
"Order" => array("PO" => "TR11214", "OrderID" => 242856952012),
"Transaction" => array("TransPO" => "TR11213", "TransactionPrice" => 209.99)
)
);

print_r(array_unique($myarray, SORT_REGULAR));


PHP Interview Questions with Solutions: Prepare for PHP Interview

PHP Interview Questions with Solutions: Prepare for PHP Interview

Prepare for PHP Interviews. Set Variable With php.ini File. Logic & Output Behind PHP Code Segment. Class Concept, Error & Functions in PHP. Start PHP Now!

Description
In this course you will be introduced with some tricky questions that everyone face during their interview. In each solutions I have included some useful functions which we generally use at the time of development also. I have covered the following area in my course with Questions, Attractive Presentations and Practical Solutions that will help you to understand the logic behind PHP in a different way.

Some useful functions
PHP error types
Class concept
Access specifiers public, private and protected
Set variable with php.ini file
Operators introduced in PHP7
Logic and its output behind some code segment
Basic knowledge
PHP7, XAMPP Server, Notepad++
What will you learn
This course is build for the person who is facing interviews. Every question is well explained with the practical solution through videos. So that everybody can prepare themselves for the tricky questions asked during interviews
To continue:

Advantages of Hiring PHP Developer for your Website Project

Advantages of Hiring PHP Developer for your Website Project

PHP - Hypertext pre-processor, a scripting language used by many people in developing web pages, but most of us are unaware even of the full form. To train someone and make them learn this whole language is as difficult and time-consuming as it is...

PHP - Hypertext pre-processor, a scripting language used by many people in developing web pages, but most of us are unaware even of the full form. To train someone and make them learn this whole language is as difficult and time-consuming as it is to learn the language yourself. That’s why PHP developers are there to make your life easy. This article will give us the advantages and requirements of Hire PHP Developer for our very own website project.

First of all, let us understand the value the right developer brings to the project and why it is important for your business.

A website is a major component of any company/business and is very important for its face value, the way it represents the company on the internet is critical for any business to succeed. This is the reason why companies are looking for PHP developers who can develop their webpage.

If you're planning to do an online business, your PHP programmer will be the first person to transfer your thinking onto the webpage. You should, therefore, employ developers from PHP to make your hypothetical idea a reality.

With this software programming language, PHP developers all-around can easily build website frameworks, web content management systems, web template systems, and various other web-based designs.

Some of the reasons why we need to outsource these developers are:

Not everyone is the best in each field, all of us have our specific skills and talents hence, PHP developers are also the best at what they do. The time and money spent on the training of the in house employees would be saved if the professional PHP developers are hired. Instead of multitasking, if the employees were to focus on what they’re good at it would increase productivity too.

The PHP developers would be much more professional than the in-house workers. It would lead to the seriousness of work. Hence, on-time delivery is guaranteed with hired PHP developers.

In addition to these benefits, you would also be able to track your project through every stage in constant communication with your online team. These advantages make it incredibly popular and smart to hire a PHP developer.

The PHP developers have in-depth knowledge of PHP, HTML and various frameworks in terms of technical capabilities. Hiring PHP developers are advised to give your website a professional look based on PHP.

Much of web success depends on the involvement of social media. The developer can add to your social networking pages a feature that explicitly redirects visitors. In addition, SEO experts also suggest better connections to the website's social network.

Just like a tailor stitches our dresses according to our preferences and is ready to make last-minute changes. A PHP developer will also be available at the nick of your call to make the website just the way you want it to be and have a customized solution for every problem.

Read also: Why & How to Hire Dedicated PHP Developer

At some point in your business, you’re going to have problems regarding your webpage due to the rapidly changing technology, instead of struggling with ideas like these and not being able to come up with an appropriate solution a PHP web developer could help us with our problems just like any technician would help us with the problems we face in our offices or any architect would help us with designing the structure of a building or any interior designer would help us with setting up our home. The PHP development company are hubs of workers who would help us overcome these problems and are always there.

Source by

Array in PHP

Array in PHP

Arrays in PHP is a type of data structure that allows us to store multiple elements of similar data type under a single variable thereby saving us the effort of creating a different variable for every data.

A PHP array is a variable that stores more than one piece of related data in a single variable.

Think of an array as a box of chocolates with slots inside.

The box represents the array itself while the spaces containing chocolates represent the values stored in the arrays.

The diagram below illustrates the above syntax.

Numeric Arrays

Numeric arrays use number as access keys.

An access key is a reference to a memory slot in an array variable.

The access key is used whenever we want to read or assign a new value an array element.

Below is the syntax for creating numeric array in php. Array Example

<?php
$variable_name[n] = value;
?>

Or

<?php
$variable_name = array(n => value, …);
?>

HERE,

  • “$variable_name…” is the name of the variable
  • “[n]” is the access index number of the element
  • “value” is the value assigned to the array element.

Let’s now look at an example of a numeric array.

Suppose we have 5 movies that we want to store in array variables.

We can use the example shown below to do that.

<?php

$movie[0] = 'Shaolin Monk';
$movie[1] = 'Drunken Master';
$movie[2] = 'American Ninja';
$movie[3] = 'Once upon a time in China';
$movie[4] = 'Replacement Killers';

?>

Here,

Each movie is given an index number that is used to retrieve or modify its value. Observe the following code-

<?php
$movie[0]="Shaolin Monk";
$movie[1]="Drunken Master";
$movie[2]="American Ninja";
$movie[3]="Once upon a time in China";
$movie[4]="Replacement Killers";
echo $movie[3];
$movie[3] = " Eastern Condors";
echo $movie[3];
?>

Output:

Once upon a time in China Eastern Condors

As you can see from the above examples, working with arrays in PHP when dealing with multiple values of the same nature is very easy and flexible.

Alternatively, the above array variables can also be created using the following code.

<?php
$movie = array(0 => "Shaolin Monk",
1 => "Drunken Master",
2 => "American Ninja",
3 => "Once upon a time in China",
4 =>"Replacement Killers" );
echo $movie[4];
?>

Output:

Replacement Killers
PHP Associative Array

Associative array differ from numeric array in the sense that associative arrays use descriptive names for id keys.

Below is the syntax for creating associative array in php.

<?php
$variable_name['key_name'] = value;

$variable_name = array('keyname' => value);
?>

HERE,

  • “$variable_name…” is the name of the variable
  • “['key_name']” is the access index number of the element
  • “value” is the value assigned to the array element.

 Let’s suppose that we have a group of persons, and we want to assign the gender of each person against their names.

We can use an associative array to do that.The code below helps us to do that.

<?php
$persons = array("Mary" => "Female", "John" => "Male", "Mirriam" => "Female");
print_r($persons);
echo "";
echo "Mary is a " . $persons["Mary"];
?>

 HERE,

Output:

Array ( [Mary] => Female [John] => Male [Mirriam] => Female ) Mary is a Female

Associative array are also very useful when retrieving data from the database.

The field names are used as id keys. 

PHP Multi-dimensional arrays

These are arrays that contain other nested arrays.

The advantage of multidimensional arrays is that they allow us to group related data together.

Let’s now look at a practical example that implements a php multidimensional array.

The table below shows a list of movies by category.

The above information can be represented as a multidimensional array. The code below shows the implementation.

<?php
$movies =array(
"comedy" => array("Pink Panther", "John English", "See no evil hear no evil"),
"action" => array("Die Hard", "Expendables"),
"epic" => array("The Lord of the rings"),
"Romance" => array("Romeo and Juliet")
);
print_r($movies);
?>

 HERE,

Output:

Array ( [comedy] => Array ( [0] => Pink Panther [1] => John English [2] => See no evil hear no evil ) [action] => Array ( [0] => Die Hard [1] => Expendables ) [epic] => Array ( [0] => The Lord of the rings ) [Romance] => Array ( [0] => Romeo and Juliet ) )

Another way to define the same array is as follows

<?php
$film=array(

                "comedy" => array(

                                0 => "Pink Panther",

                                1 => "john English",

                                2 => "See no evil hear no evil"

                                ),

                "action" => array (

                                0 => "Die Hard",

                                1 => "Expendables"

                                ),

                "epic" => array (

                                0 => "The Lord of the rings"

                                ),

                "Romance" => array

                                (

                                0 => "Romeo and Juliet"

                                )

);
echo $film["comedy"][0];
?>

Output:

Pink Panther

 Note: the movies numeric array has been nested inside the categories associative array 

PHP Arrays: Operators

PHP Array Functions

Count function

The count function is used to count the number of elements that an php array contains. The code below shows the implementation.

<?php
$lecturers = array("Mr. Jones", "Mr. Banda", "Mrs. Smith");
echo count($lecturers);
?>

Output:

3

is_array function

The is_array function is used to determine if a variable is an array or not. Let’s now look at an example that implements the is_array functions.

<?php
$lecturers = array("Mr. Jones", "Mr. Banda", "Mrs. Smith");
echo is_array($lecturers);
?>

Output:

1

Sort

This function is used to sort arrays by the values.

If the values are alphanumeric, it sorts them in alphabetical order.

If the values are numeric, it sorts them in ascending order.

It removes the existing access keys and add new numeric keys.

The output of this function is a numeric array

<?php
$persons = array("Mary" => "Female", "John" => "Male", "Mirriam" => "Female");

sort($persons);

print_r($persons);
?>

Output:

Array ( [0] => Female [1] => Female [2] => Male )

ksort

This function is used to sort the array using the key. The following example illustrates its usage.

<?php
$persons = array("Mary" => "Female", "John" => "Male", "Mirriam" => "Female");

ksort($persons);

print_r($persons);
?>

Output:

Array ( [John] => Male [Mary] => Female [Mirriam] => Female )

asort

This function is used to sort the array using the values. The following example illustrates its usage.

<?php

$persons = array("Mary" => "Female", "John" => "Male", "Mirriam" => "Female");

asort($persons);

print_r($persons);

?>

Output:

Array ( [Mary] => Female [Mirriam] => Female [John] => Male )

Why use arrays?

  • Contents of Arrays can be stretched,
  • Arrays easily help group related information such as server login details together
  • Arrays help write cleaner code.
Summary
  • Arrays are special variables with the capacity to store multi values.
  • Arrays are flexibility and can be easily stretched to accommodate more values
  • Numeric arrays use numbers for the array keys
  • PHP Associative array use descriptive names for array keys
  • Multidimensional arrays contain other arrays inside them.
  • The count function is used to get the number of items that have been stored in an array
  • The is_array function is used to determine whether a variable is a valid array or not.
  • Other array functions include sort, ksort, assort etc.

Thanks For Visiting, Keep Visiting

Further reading

☞ PHP for Beginners - Become a PHP Master - CMS Project

☞ Learn Object Oriented PHP By Building a Complete Website

☞ MEVP Stack Vue JS 2 Course: MySQL + Express.js + Vue.js +PHP

☞ Object Oriented PHP & MVC

☞ PHP OOP: Object Oriented Programming for beginners + Project

☞ Learn PHP Fundamentals From Scratch

☞ The Complete PHP MySQL Professional Course with 5 Projects

☞ The Complete JavaScript Course 2019: Build Real Projects!


This post was originally published here