1

For example

[Nationality_1] string(8)=>Indian [Nationality_5] string(12)=>American [Nationality_12] string(17)=>Japanese

I got these array values by foreach loop but I want to put these value to single array by index on strings

Desired output [Nationality] [0] string(8)=>Indian [1] string(12)=>American[2]string(17)=>Japanese

I have tried array_values but output Null

I tried this but creates duplicate array in loop for multiple orders Please help me on it . Thanks $Nationality[] = $value;

1
  • 1
    your question is not clear Commented Sep 27, 2017 at 16:31

2 Answers 2

1

One solution is below code:

$nationality = array(
    'Nationality_1' => 'Indian',
    'Nationality_2' => 'American',
    'Nationality_3' => 'Japanese'
);
$temp = array();
foreach ($nationality as $val) {
    $temp[] = $val;
}
$nationality = $temp;
print_r($nationality);
unset($temp);
// Array ( [0] => Indian [1] => American [2] => Japanese )
Sign up to request clarification or add additional context in comments.

Comments

0

It sounds like you may want to use array_values to pull all the nationalities from your input list.

array_values

Return all the values of an array

http://php.net/array_values

What you do with that is then up to you but from your desired output it seems you want them under another array with a "Nationality" key?

Here is an example...

$input = array(
    'Nationality_1' => 'Indian',
    'Nationality_5' => 'American',
    'Nationality_12' => 'Japanese'
);

$output = array('Nationality' => array_values($input));

var_dump($output);

/*
array(1) {
  ["Nationality"]=>
  array(3) {
    [0]=>
    string(6) "Indian"
    [1]=>
    string(8) "American"
    [2]=>
    string(8) "Japanese"
  }
}
*/

See the code in action: https://eval.in/869938

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.