PHP Array Formatting for array inner array without using RecursiveIteratorIterator
How can I get this format? Result should be dynamic:
1
a
b
0
This is my original array:
$arr = array(0 => 1,
array(
0 => 'a',
1 => 'b'
),
'c' => '0'
);
I've tried:
$kArr = array();
for($i=0; $i<count($arr); $i++ ){
$iArr = array();
echo $arr[$i];
foreach($arr[$i] as $key => $value){
if(!is_array($value)) continue;
foreach($value as $subKey => $subValue){
$iArr = $subValue;
}
}
if(count($iArr) > 0 )
$kArr[$i] = $iArr;
}
echo '<pre>'; print_r($kArr);
Where I am wrong? if there is another method then please let me know.
php arrays multidimensional-array
|
show 8 more comments
How can I get this format? Result should be dynamic:
1
a
b
0
This is my original array:
$arr = array(0 => 1,
array(
0 => 'a',
1 => 'b'
),
'c' => '0'
);
I've tried:
$kArr = array();
for($i=0; $i<count($arr); $i++ ){
$iArr = array();
echo $arr[$i];
foreach($arr[$i] as $key => $value){
if(!is_array($value)) continue;
foreach($value as $subKey => $subValue){
$iArr = $subValue;
}
}
if(count($iArr) > 0 )
$kArr[$i] = $iArr;
}
echo '<pre>'; print_r($kArr);
Where I am wrong? if there is another method then please let me know.
php arrays multidimensional-array
What exactly is "this format"? Are you looking for a way to recursively print the values?
– Nico Haase
Nov 27 '18 at 10:47
are you just trying to convert multi-dimensional array into a 2d array?
– treyBake
Nov 27 '18 at 10:49
Yes, I mentioned the result which format I want. If the array has inner array then the result must be print as per my formatted.
– Vipul Jethva
Nov 27 '18 at 10:49
4
Possible duplicate of How to Flatten a Multidimensional Array?
– ponury-kostek
Nov 27 '18 at 10:50
@ponury-kostek Can you please help me to use my array and result which I want?
– Vipul Jethva
Nov 27 '18 at 10:55
|
show 8 more comments
How can I get this format? Result should be dynamic:
1
a
b
0
This is my original array:
$arr = array(0 => 1,
array(
0 => 'a',
1 => 'b'
),
'c' => '0'
);
I've tried:
$kArr = array();
for($i=0; $i<count($arr); $i++ ){
$iArr = array();
echo $arr[$i];
foreach($arr[$i] as $key => $value){
if(!is_array($value)) continue;
foreach($value as $subKey => $subValue){
$iArr = $subValue;
}
}
if(count($iArr) > 0 )
$kArr[$i] = $iArr;
}
echo '<pre>'; print_r($kArr);
Where I am wrong? if there is another method then please let me know.
php arrays multidimensional-array
How can I get this format? Result should be dynamic:
1
a
b
0
This is my original array:
$arr = array(0 => 1,
array(
0 => 'a',
1 => 'b'
),
'c' => '0'
);
I've tried:
$kArr = array();
for($i=0; $i<count($arr); $i++ ){
$iArr = array();
echo $arr[$i];
foreach($arr[$i] as $key => $value){
if(!is_array($value)) continue;
foreach($value as $subKey => $subValue){
$iArr = $subValue;
}
}
if(count($iArr) > 0 )
$kArr[$i] = $iArr;
}
echo '<pre>'; print_r($kArr);
Where I am wrong? if there is another method then please let me know.
php arrays multidimensional-array
php arrays multidimensional-array
edited Nov 27 '18 at 10:57
Vipul Jethva
asked Nov 27 '18 at 10:47
Vipul JethvaVipul Jethva
432620
432620
What exactly is "this format"? Are you looking for a way to recursively print the values?
– Nico Haase
Nov 27 '18 at 10:47
are you just trying to convert multi-dimensional array into a 2d array?
– treyBake
Nov 27 '18 at 10:49
Yes, I mentioned the result which format I want. If the array has inner array then the result must be print as per my formatted.
– Vipul Jethva
Nov 27 '18 at 10:49
4
Possible duplicate of How to Flatten a Multidimensional Array?
– ponury-kostek
Nov 27 '18 at 10:50
@ponury-kostek Can you please help me to use my array and result which I want?
– Vipul Jethva
Nov 27 '18 at 10:55
|
show 8 more comments
What exactly is "this format"? Are you looking for a way to recursively print the values?
– Nico Haase
Nov 27 '18 at 10:47
are you just trying to convert multi-dimensional array into a 2d array?
– treyBake
Nov 27 '18 at 10:49
Yes, I mentioned the result which format I want. If the array has inner array then the result must be print as per my formatted.
– Vipul Jethva
Nov 27 '18 at 10:49
4
Possible duplicate of How to Flatten a Multidimensional Array?
– ponury-kostek
Nov 27 '18 at 10:50
@ponury-kostek Can you please help me to use my array and result which I want?
– Vipul Jethva
Nov 27 '18 at 10:55
What exactly is "this format"? Are you looking for a way to recursively print the values?
– Nico Haase
Nov 27 '18 at 10:47
What exactly is "this format"? Are you looking for a way to recursively print the values?
– Nico Haase
Nov 27 '18 at 10:47
are you just trying to convert multi-dimensional array into a 2d array?
– treyBake
Nov 27 '18 at 10:49
are you just trying to convert multi-dimensional array into a 2d array?
– treyBake
Nov 27 '18 at 10:49
Yes, I mentioned the result which format I want. If the array has inner array then the result must be print as per my formatted.
– Vipul Jethva
Nov 27 '18 at 10:49
Yes, I mentioned the result which format I want. If the array has inner array then the result must be print as per my formatted.
– Vipul Jethva
Nov 27 '18 at 10:49
4
4
Possible duplicate of How to Flatten a Multidimensional Array?
– ponury-kostek
Nov 27 '18 at 10:50
Possible duplicate of How to Flatten a Multidimensional Array?
– ponury-kostek
Nov 27 '18 at 10:50
@ponury-kostek Can you please help me to use my array and result which I want?
– Vipul Jethva
Nov 27 '18 at 10:55
@ponury-kostek Can you please help me to use my array and result which I want?
– Vipul Jethva
Nov 27 '18 at 10:55
|
show 8 more comments
5 Answers
5
active
oldest
votes
foreach ($arr as $k => $v) {
if (is_array($v)) {
foreach ($v as $k2 => $v2) {
echo "$v2n";
}
} else {
echo "$vn";
}
}
if you want to do it from scratch you can try something like this.
add a comment |
To achieve your desired result you have to loop through your $arr variable and check if it's an array using is_array function. If it is an array then loop through that array and out put the values.
Try the following code.
$arr = array(0 => 1,
array(
0 => 'a',
1 => 'b'
),
'c' => '0'
);
foreach($arr as $key => $value)
{
if(is_array($value))
{
foreach($value as $inner_key => $inner_value)
{
echo "<br>";
echo $inner_value;
}
}
else
{
echo "<br>";
echo $value;
}
}
add a comment |
I wrote a function that gets the values of the internal arrays and adds them in the correct order:
function array_values_recursive(array $array)
{
$values = array_values($array);
foreach ($values as $key => $value) {
if (is_array($value)) {
$innerValues = array_values_recursive($value);
if ($innerValues) {
array_splice($values, $key, 1, $innerValues);
}
}
}
return $values;
}
Example:
$array = array(
0 => 1,
array(
0 => 'a',
1 => 'b'
),
'c' => '0'
);
$values = array_values_recursive($array);
echo(implode(PHP_EOL, $values));
Will print:
1
a
b
0
add a comment |
function flatten($array) {
// flattened array
$flat = ;
// flattening anonymous function
$flatten = function ($array) use (&$flatten, &$flat) {
// loop over an array
foreach ($array as $item) {
// if item is array, use flattening function
if (is_array($item)) {
$flatten($item);
} else {
// else push item to flattened array
$flat = $item;
}
}
};
// initial call of flattening function
$flatten($array);
// return flattened array
return $flat;
}
var_dump(flatten([
0 => 1,
[
0 => 'a',
1 => 'b'
],
'c' => '0'
]));
add a comment |
Just use array_walk_recursive.
$r = ;
array_walk_recursive($arr, function ($e) use (&$r) {
$r = $e;
});
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53497935%2fphp-array-formatting-for-array-inner-array-without-using-recursiveiteratoriterat%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
foreach ($arr as $k => $v) {
if (is_array($v)) {
foreach ($v as $k2 => $v2) {
echo "$v2n";
}
} else {
echo "$vn";
}
}
if you want to do it from scratch you can try something like this.
add a comment |
foreach ($arr as $k => $v) {
if (is_array($v)) {
foreach ($v as $k2 => $v2) {
echo "$v2n";
}
} else {
echo "$vn";
}
}
if you want to do it from scratch you can try something like this.
add a comment |
foreach ($arr as $k => $v) {
if (is_array($v)) {
foreach ($v as $k2 => $v2) {
echo "$v2n";
}
} else {
echo "$vn";
}
}
if you want to do it from scratch you can try something like this.
foreach ($arr as $k => $v) {
if (is_array($v)) {
foreach ($v as $k2 => $v2) {
echo "$v2n";
}
} else {
echo "$vn";
}
}
if you want to do it from scratch you can try something like this.
answered Nov 27 '18 at 11:02
RomMerRomMer
10111
10111
add a comment |
add a comment |
To achieve your desired result you have to loop through your $arr variable and check if it's an array using is_array function. If it is an array then loop through that array and out put the values.
Try the following code.
$arr = array(0 => 1,
array(
0 => 'a',
1 => 'b'
),
'c' => '0'
);
foreach($arr as $key => $value)
{
if(is_array($value))
{
foreach($value as $inner_key => $inner_value)
{
echo "<br>";
echo $inner_value;
}
}
else
{
echo "<br>";
echo $value;
}
}
add a comment |
To achieve your desired result you have to loop through your $arr variable and check if it's an array using is_array function. If it is an array then loop through that array and out put the values.
Try the following code.
$arr = array(0 => 1,
array(
0 => 'a',
1 => 'b'
),
'c' => '0'
);
foreach($arr as $key => $value)
{
if(is_array($value))
{
foreach($value as $inner_key => $inner_value)
{
echo "<br>";
echo $inner_value;
}
}
else
{
echo "<br>";
echo $value;
}
}
add a comment |
To achieve your desired result you have to loop through your $arr variable and check if it's an array using is_array function. If it is an array then loop through that array and out put the values.
Try the following code.
$arr = array(0 => 1,
array(
0 => 'a',
1 => 'b'
),
'c' => '0'
);
foreach($arr as $key => $value)
{
if(is_array($value))
{
foreach($value as $inner_key => $inner_value)
{
echo "<br>";
echo $inner_value;
}
}
else
{
echo "<br>";
echo $value;
}
}
To achieve your desired result you have to loop through your $arr variable and check if it's an array using is_array function. If it is an array then loop through that array and out put the values.
Try the following code.
$arr = array(0 => 1,
array(
0 => 'a',
1 => 'b'
),
'c' => '0'
);
foreach($arr as $key => $value)
{
if(is_array($value))
{
foreach($value as $inner_key => $inner_value)
{
echo "<br>";
echo $inner_value;
}
}
else
{
echo "<br>";
echo $value;
}
}
edited Nov 27 '18 at 11:12
answered Nov 27 '18 at 11:05
Sajjad AliSajjad Ali
22419
22419
add a comment |
add a comment |
I wrote a function that gets the values of the internal arrays and adds them in the correct order:
function array_values_recursive(array $array)
{
$values = array_values($array);
foreach ($values as $key => $value) {
if (is_array($value)) {
$innerValues = array_values_recursive($value);
if ($innerValues) {
array_splice($values, $key, 1, $innerValues);
}
}
}
return $values;
}
Example:
$array = array(
0 => 1,
array(
0 => 'a',
1 => 'b'
),
'c' => '0'
);
$values = array_values_recursive($array);
echo(implode(PHP_EOL, $values));
Will print:
1
a
b
0
add a comment |
I wrote a function that gets the values of the internal arrays and adds them in the correct order:
function array_values_recursive(array $array)
{
$values = array_values($array);
foreach ($values as $key => $value) {
if (is_array($value)) {
$innerValues = array_values_recursive($value);
if ($innerValues) {
array_splice($values, $key, 1, $innerValues);
}
}
}
return $values;
}
Example:
$array = array(
0 => 1,
array(
0 => 'a',
1 => 'b'
),
'c' => '0'
);
$values = array_values_recursive($array);
echo(implode(PHP_EOL, $values));
Will print:
1
a
b
0
add a comment |
I wrote a function that gets the values of the internal arrays and adds them in the correct order:
function array_values_recursive(array $array)
{
$values = array_values($array);
foreach ($values as $key => $value) {
if (is_array($value)) {
$innerValues = array_values_recursive($value);
if ($innerValues) {
array_splice($values, $key, 1, $innerValues);
}
}
}
return $values;
}
Example:
$array = array(
0 => 1,
array(
0 => 'a',
1 => 'b'
),
'c' => '0'
);
$values = array_values_recursive($array);
echo(implode(PHP_EOL, $values));
Will print:
1
a
b
0
I wrote a function that gets the values of the internal arrays and adds them in the correct order:
function array_values_recursive(array $array)
{
$values = array_values($array);
foreach ($values as $key => $value) {
if (is_array($value)) {
$innerValues = array_values_recursive($value);
if ($innerValues) {
array_splice($values, $key, 1, $innerValues);
}
}
}
return $values;
}
Example:
$array = array(
0 => 1,
array(
0 => 'a',
1 => 'b'
),
'c' => '0'
);
$values = array_values_recursive($array);
echo(implode(PHP_EOL, $values));
Will print:
1
a
b
0
answered Nov 27 '18 at 11:26
Serge AndriichukSerge Andriichuk
491
491
add a comment |
add a comment |
function flatten($array) {
// flattened array
$flat = ;
// flattening anonymous function
$flatten = function ($array) use (&$flatten, &$flat) {
// loop over an array
foreach ($array as $item) {
// if item is array, use flattening function
if (is_array($item)) {
$flatten($item);
} else {
// else push item to flattened array
$flat = $item;
}
}
};
// initial call of flattening function
$flatten($array);
// return flattened array
return $flat;
}
var_dump(flatten([
0 => 1,
[
0 => 'a',
1 => 'b'
],
'c' => '0'
]));
add a comment |
function flatten($array) {
// flattened array
$flat = ;
// flattening anonymous function
$flatten = function ($array) use (&$flatten, &$flat) {
// loop over an array
foreach ($array as $item) {
// if item is array, use flattening function
if (is_array($item)) {
$flatten($item);
} else {
// else push item to flattened array
$flat = $item;
}
}
};
// initial call of flattening function
$flatten($array);
// return flattened array
return $flat;
}
var_dump(flatten([
0 => 1,
[
0 => 'a',
1 => 'b'
],
'c' => '0'
]));
add a comment |
function flatten($array) {
// flattened array
$flat = ;
// flattening anonymous function
$flatten = function ($array) use (&$flatten, &$flat) {
// loop over an array
foreach ($array as $item) {
// if item is array, use flattening function
if (is_array($item)) {
$flatten($item);
} else {
// else push item to flattened array
$flat = $item;
}
}
};
// initial call of flattening function
$flatten($array);
// return flattened array
return $flat;
}
var_dump(flatten([
0 => 1,
[
0 => 'a',
1 => 'b'
],
'c' => '0'
]));
function flatten($array) {
// flattened array
$flat = ;
// flattening anonymous function
$flatten = function ($array) use (&$flatten, &$flat) {
// loop over an array
foreach ($array as $item) {
// if item is array, use flattening function
if (is_array($item)) {
$flatten($item);
} else {
// else push item to flattened array
$flat = $item;
}
}
};
// initial call of flattening function
$flatten($array);
// return flattened array
return $flat;
}
var_dump(flatten([
0 => 1,
[
0 => 'a',
1 => 'b'
],
'c' => '0'
]));
edited Nov 27 '18 at 11:21
answered Nov 27 '18 at 11:14
ponury-kostekponury-kostek
4,75441123
4,75441123
add a comment |
add a comment |
Just use array_walk_recursive.
$r = ;
array_walk_recursive($arr, function ($e) use (&$r) {
$r = $e;
});
add a comment |
Just use array_walk_recursive.
$r = ;
array_walk_recursive($arr, function ($e) use (&$r) {
$r = $e;
});
add a comment |
Just use array_walk_recursive.
$r = ;
array_walk_recursive($arr, function ($e) use (&$r) {
$r = $e;
});
Just use array_walk_recursive.
$r = ;
array_walk_recursive($arr, function ($e) use (&$r) {
$r = $e;
});
answered Nov 27 '18 at 10:51
kopirokopiro
52429
52429
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53497935%2fphp-array-formatting-for-array-inner-array-without-using-recursiveiteratoriterat%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What exactly is "this format"? Are you looking for a way to recursively print the values?
– Nico Haase
Nov 27 '18 at 10:47
are you just trying to convert multi-dimensional array into a 2d array?
– treyBake
Nov 27 '18 at 10:49
Yes, I mentioned the result which format I want. If the array has inner array then the result must be print as per my formatted.
– Vipul Jethva
Nov 27 '18 at 10:49
4
Possible duplicate of How to Flatten a Multidimensional Array?
– ponury-kostek
Nov 27 '18 at 10:50
@ponury-kostek Can you please help me to use my array and result which I want?
– Vipul Jethva
Nov 27 '18 at 10:55