-3

I have this array:

$result = array(
    '0' => array(
        'field_categories' => 'Visitor', 
        'field_name' => 'Visitor-1', 
    ), 
    '1' => array(
        'field_categories' => 'Visitor', 
        'field_name' => 'Visitor-2'
    ), 
    '2' => array(
        'field_categories' => 'Visitor', 
        'field_name' => 'Visitor-3'
    ), 
    '3' => array(
        'field_categories' => 'Visitor', 
        'field_name' => 'Visitor-4'
    ), 
    '4' => array(
        'field_categories' => 'Doctor', 
        'field_name' => 'Doctor-1'
    ), 
    '5' => array(
        'field_categories' => 'Doctor', 
        'field_name' => 'Doctor-2'
    ), 
    '6' => array(
        'field_categories' => 'Manager', 
        'field_name' => 'Manager-1'
    ), 
    '7' => array(
        'field_categories' => 'Manager', 
        'field_name' => 'Manager-2'
    ), 
    '8' => array(
        'field_categories' => 'Manager', 
        'field_name' => 'Manager-3'
    )
);
echo '<pre>',print_r($result),'</pre>';

And how can I convert it to this format? (output of echo '<pre>',print_r($result),'</pre>';) Thanks

Array
(
    [0] => Visitor
    [1] => Visitor-1
    [2] => Visitor-2
    [3] => Visitor-3
    [4] => Visitor-4
    [5] => Doctor
    [6] => Doctor-1
    [7] => Doctor-2
    [8] => Manager
    [9] => Manager-1
    [10] => Manager-2
    [11] => Manager-3
)
4

3 に答える 3

2
$result = array(
    '0' => array(
        'field_categories' => 'Visitor', 
        'field_name' => 'Visitor-1', 
    ), 
    '1' => array(
        'field_categories' => 'Visitor', 
        'field_name' => 'Visitor-2'
    ), 
    '2' => array(
        'field_categories' => 'Visitor', 
        'field_name' => 'Visitor-3'
    ), 
    '3' => array(
        'field_categories' => 'Visitor', 
        'field_name' => 'Visitor-4'
    ), 
    '4' => array(
        'field_categories' => 'Doctor', 
        'field_name' => 'Doctor-1'
    ), 
    '5' => array(
        'field_categories' => 'Doctor', 
        'field_name' => 'Doctor-2'
    ) );

 foreach($result as $key=>$val)
    {
      $newarray[$val['field_categories']][] = $val['field_name'];
    }

print_r( $newarray);

出力

Array
(
    [Visitor] => Array
        (
            [0] => Visitor-1
            [1] => Visitor-2
            [2] => Visitor-3
            [3] => Visitor-4
        )

    [Doctor] => Array
        (
            [0] => Doctor-1
            [1] => Doctor-2
        )

)
于 2013-10-01T03:47:09.383 に答える
0

If you are trying to de-dupe field_name and field_categories you can assign each value to the key of an array, and then do an array_flip

$new_result = array();
$i = 0;
foreach($result as $res){
    $new_result[$res["field_name"]] = $i;
    $new_result[$res["field_categories"]] = $i+1;
    $i += 2;
}

$new_result = array_flip($new_result);
于 2013-10-01T03:41:40.583 に答える