简体   繁体   中英

How to re-write an associative array in php?

I have an assoc array that I want to rewrite into a much simpler assoc array but I don't know how to start it.

This is the array that I have:

Array
(
    [username] => Array
        (
            [notEmpty] => Please fill this field
            [alphaNumeric] => Username can only be letters and numbers.
            [between] => Username must be from 3 to 15 characters only.
        )

    [first_name] => Array
        (
            [_empty] => This field cannot be left empty
        )

    [last_name] => Array
        (
            [_empty] => This field cannot be left empty
        )

)

And this is the array that I want to produce:

Array
(
    "username" => "Please fill this field",
    "first_name" => "This field cannot be left empty",                                            
    "last_name" => "This field cannot be left empty"
)

Sorry if this may be a simple question but I really don't know where to start and especially the logic when it comes to getting only the first value of the array and not include the others.

Thank you so much, and I really appreciate it!

You can use reset to get the first value of an array; use array_map to apply that to each element in your top-level array:

$data = array (
    'username' => 
    array (
        'notEmpty' => 'Please fill this field',
        'alphaNumeric' => 'Username can only be letters and numbers.',
        'between' => 'Username must be from 3 to 15 characters only.'
    ),
    'first_name' => 
    array (
        '_empty' => 'This field cannot be left empty'
    ),
    'last_name' => 
    array (
        '_empty' => 'This field cannot be left empty'
    )
);

$output = array_map(function ($a) { return reset($a); }, $data);
print_r($output);

Output:

Array
(
    [username] => Please fill this field
    [first_name] => This field cannot be left empty
    [last_name] => This field cannot be left empty
)

Demo on 3v4l.org

you just need to frame array using for loops

$framedArray=array(); $UnframedArray=array( "username"=> array ( "notEmpty" => "Please fill this field", "alphaNumeric" => "Username can only be letters and numbers.", "between" => "Username must be from 3 to 15 characters only." ), "first_name"=>array( "_empty" => "This field cannot be left empty" ),"last_name" => array ( "_empty" => "This field cannot be left empty" ), );

// print_r($UnframedArray);exit;

foreach($UnframedArray as $k=>$v){ //$k is keys , in your case your keys are username,first_name,last_name $temp=""; foreach($v as $kk=>$vv){ // to add all your key based validations if($temp==""){ $temp.=$vv; }else{ $temp.=",".$vv; }
} $framedArray[$k]=$temp;

}

EXPECTED OUTPUT: Array ( [username] => Please fill this field,Username can only be letters and numbers.,Username must be from 3 to 15 characters only. [first_name] => This field cannot be left empty [last_name] => This field cannot be left empty )

Blockquote

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM