-3
Array
(
    [0] => Array
    (
        [id] => 21153
        [genre] => ["History","Drama", "Thriller"]
    )

    [1] => Array
    (
        [id] => 21152
        [genre] => ["ACTION"]
    )

    [2] => Array
    (
        [id] => 21151
        [genre] => ["ROMANTIC"]
    )

    [3] => Array
    (
        [id] => 21150
        [genre] => ["Drama"]
    )

)

I have with the above array and I want to convert that array to an unique array as mentioned below, and there should be any duplicate values.

Array(
    [History] => "History"
    [ACTION] => "ACTION"
    [ROMANTIC] => "ROMANTIC"
    [Drama] => "Drama"
    [Thriller]=>"Thriller"
)
6
  • 4
    what have you tried, where did you get stuck? Commented Sep 25, 2018 at 9:14
  • "and there should be any duplicate values" - which duplicate values do you want? Commented Sep 25, 2018 at 9:16
  • 1
    What's the point of having an index similar to the value ? Commented Sep 25, 2018 at 9:17
  • the format of this [genre] => ["History","Drama", "Thriller"] makes me stumble. Is this the output of a print_r()?? What datatype is 'genre'? To achieve this output via print_r it would have to be the literal string ["History","Drama", Thriller"] Commented Sep 25, 2018 at 9:28
  • "and there should be any duplicate values"- Sorry my mistake, we shouldn't allow any duplicate key and value pair Commented Sep 25, 2018 at 10:53

5 Answers 5

0

I don't really get how the resulting array makes any sense, but here you are:
Loop through the array, loop through the genres and push items to a new array.

<?
$a = Array
(
    [
        "id" => 21153,
        "genre" => ["History","Drama", "Thriller"]
    ],

    [        
        "id" => 21152,
        "genre" => ["ACTION"]
    ]

);


$result = [];
foreach($a as $b) {
    foreach($b['genre'] as $genre) {
        $result[$genre] = $genre;  // don't need to check if it exists already, because we'll overwrite it anyway.
    }
}

echo "<pre>";
print_r($result);
echo "</pre>";

// output:
Array
(
    [History] => History
    [Drama] => Drama
    [Thriller] => Thriller
    [ACTION] => ACTION
)
Sign up to request clarification or add additional context in comments.

Comments

0

It can be done by using foreach() loop and in_array() function like below

$myArray=array();
$array = array(
            array("id"=>21153,"genre"=>array("History","Drama", "Thriller")),
            array("id"=>21152,"genre"=>array("ACTION")),
            array("id"=>21151,"genre"=>array("ROMANTIC")),
            array("id"=>21150,"genre"=>array("Drama"))          
        );

foreach($array as $arr){    
    foreach($arr as $genres){
        if(is_array($genres)){
            foreach($genres as $elem){
                  if (!in_array($elem, $myArray))
                  {
                    $myArray[$elem]=$elem;
                  }
            }
        }
    }
}

print_r($myArray);

Comments

0

This is a function that resolves what you ask, though I'm not sure it is quite useful to have value and key being identical. I'm just looping over all genders and add only the ones I don't already have store in returning array. probably a bit dangerous with your code because it is case sensitive

function getGenders(array $rawValues) :array {
    $distinctGenders = [];
    foreach ($rawValues as $row) {
        foreach ($row["genre"] as $gender) {
            if (!in_array($gender, $distinctGenders)) {
                $distinctGenders[$gender] = $gender;
            }
        }
    }

    return $distinctGenders;
}

Comments

0

By doing the changes, I have fixed this,

$responseData = Array(
[0] => Array
    (
        [id] => 21153
        [genre] => ["History","Drama", "Thriller"]
    )

[1] => Array
    (
        [id] => 21152
        [genre] => ["ACTION"]
    )

[2] => Array
    (
        [id] => 21151
        [genre] => ["ROMANTIC"]
    )

[3] => Array
    (
        [id] => 21150
        [genre] => ["Drama"]
    )

       foreach ($responseData as $data){
       $strReplace = str_replace(array('[',']') , ''  ,  $data['genre']);
       $explodeData[] = explode(',', $strReplace);  
   }

And the output after exploding is :

$explodeData = Array

( [0] => Array ( [0] => "History" [1] => "Drama" [2] => "Thriller" )

[1] => Array
    (
        [0] => "ACTION"
    )

[2] => Array
    (
        [0] => "ROMANTIC"
    )

[3] => Array
    (
        [0] => "Drama"
    )

)

And finally by the foreach loop :

       foreach ($explodeData as $arr) {
        foreach ($arr as $genres) {
            if (!in_array($genres, $myArray)) {
                $myArray[$genres] = $genres;
            }
        }
    }

And finally the required output comes as below :

Array(
["History"] => "History"
["Drama"] => "Drama"
["Thriller"] => "Thriller"
["ACTION"] => "ACTION"
["ROMANTIC"] => "ROMANTIC"
)

Comments

0
  1. Flatten the column of data.
  2. Assign keys from the values (this ensures uniqueness).

Code: (Demo)

$array = [
    ["id" => 21153, "genre" => ["History", "Drama", "Thriller"]],
    ["id" => 21152, "genre" => ["ACTION"]],
    ["id" => 21151, "genre" => ["ROMANTIC"]],
    ["id" => 21150, "genre" => ["Drama"]]         
];

$flat = array_merge(...array_column($array, 'genre'));
var_export(
    array_combine($flat, $flat)
);

Output:

array (
  'History' => 'History',
  'Drama' => 'Drama',
  'Thriller' => 'Thriller',
  'ACTION' => 'ACTION',
  'ROMANTIC' => 'ROMANTIC',
)

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.