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...

nanobash
  • 5,419
  • 7
  • 38
  • 56
Mohammed Sufian
  • 1,743
  • 6
  • 35
  • 62
  • 1
    2 foreach -> Print all values in 1 new array – AyB Mar 20 '14 at 10:27
  • @ICanHasCheezburger how i can write 2 foreach...could you please give me more details.. so that i can try.. – Mohammed Sufian Mar 20 '14 at 10:29
  • @Muhammad :| Please have a look [here.](http://stackoverflow.com/questions/6413589/php-foreach-with-multidimensional-array) This is basic and can easily be found with a little search. – AyB Mar 20 '14 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. – Sumit Gupta Mar 20 '14 at 11:01

3 Answers3

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

nanobash
  • 5,419
  • 7
  • 38
  • 56
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'];
}
Farnabaz
  • 4,030
  • 1
  • 22
  • 42
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.
payal
  • 36
  • 2