0

I have an array like this:

 Array ( [0] => Array 
             ([category_name] => Operating System
              [sub_category_name] => Windows 8) 
         [1] => Array 
             ([category_name] => Operating System
              [sub_category_name] => Linux)
         [2] => Array 
             ([category_name] => Mobile
              [sub_category_name] => Nokia))

I would like to customize it like this:

Array('Operating System' => 'Windows 8', 'Operating System' => 'Linux', 'Mobile' => 'Nokia')

Thanks in advance. Any help or suggestion would be a great help...

4
  • 1
    2 foreach -> Print all values in 1 new array Commented Mar 20, 2014 at 10:27
  • @ICanHasCheezburger how i can write 2 foreach...could you please give me more details.. so that i can try.. Commented Mar 20, 2014 at 10:29
  • @Muhammad :| Please have a look here. This is basic and can easily be found with a little search. Commented Mar 20, 2014 at 10:31
  • 1
    The sample ARRAY you posted cannot be achieve because you have Operating System as KEY twice and this will overwrite values. If you want to just echo than you can surely do a Foreach or while, but if you want the ARRAY as variable than it is not possible. Commented Mar 20, 2014 at 11:01

3 Answers 3

2
$n = array();
$i = 0;

array_walk($arr, function($subArr) {
    global $n;
    global $i;
    $n[$subArr['category_name'] . $i] = $subArr['sub_category_name'];
    $i++;
});

var_dump($n); // array(3) { ["Operating System0"]=> string(9) "Windows 8" ["Operating System1"]=> string(5) "Linux" ["Mobile2"]=> string(5) "Nokia" } 

NOTE: As OP updated question, I updated answer too, though in the same array the same keys are not permitted, so I added counter at the end of the key in the new array.

DEMO

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

7 Comments

i am really very sorry :( .. i have updated the required output in the question...
you can't have duplicates key in array
what he want is different, look output in question
@crypticous sir thank you very much.. for your important time.. but you demo $arr is deffirent from my question array...
@ToKeN Sir .. thank you very much sir.. for your important time.. :)
|
1

this may help

$result = array();
foreach($firstArray as $row) {
    $result[$row['category_name']] = $row['sub_category_name'];
}

if you want to merge duplicate keys you can write like this

$result = array();
foreach($firstArray as $row) {
    if(isset($result[$row['category_name']])) {
        if(!is_array($result[$row['category_name']])) {
            $tmp = array();
            $tmp[] = $result[$row['category_name']];
            $result[$row['category_name']] = $tmp;
        }
        $result[$row['category_name']][] = $row['sub_category_name'];
    } else 
    $result[$row['category_name']] = $row['sub_category_name'];
}

7 Comments

Sir thanks,,, but your code giving me.. Array ( [0] => Operating System [1] => Windows 8 [2] => Operating System ) and my requirement is different..
you want remove duplicate elements? @Muhammad
no.. i want the category and sub_category should come as key and values
check out my answer, it should do what you are looking for
i am really very sorry :( .. i have updated the required output in the question...
|
1
PHP does not allow duplicate keys for same array.
If you want to use same keys for multiple values then you should use multidimensional array as mentioned in below example.

As per your above code below is your array 
$array = array("0" => array("category_name" => "Operating System", "sub_category_name" => "Windows 8"),
    "1" => array("category_name" => "Operating System", "sub_category_name" => "Linux"),
    "2" => array("category_name" => "Mobile", "sub_category_name" => "Nokia"));

you can merge the array and for same key use multidimensional array

$new_array = array();
foreach ($array as $arr) {

    $arrmerge = array($arr['category_name'] => $arr['sub_category_name']);
    if (array_key_exists($arr['category_name'], $new_array)) {
        $arrmerge[$arr['category_name']] = array($new_array[$arr['category_name']], $arr['sub_category_name']);
    }

    $new_array = array_merge($new_array, $arrmerge);
}
print_r($new_array);

I hope this will help you.

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.