₦airaland Forum

Welcome, Guest: RegisterLoginWith GoogleTrendingRecentNew

Stats: 3,325,297 members, 8,421,215 topics. Date: Saturday, 06 June 2026 at 01:15 AM

Toggle theme

Array Conversion In PHP - Programming - Nairaland

Nairaland ForumScience/TechnologyProgrammingArray Conversion In PHP (1129 Views)

1 Reply (Go Down)

Array Conversion In PHP by Dlastofmykind(op): 11:07am On Oct 31, 2015
Good day. I'm having issues converting this array to an associative array.
The array is in this format:
[10] => Array ( [0] => Nigeria )
[11] => Array ( [0] => South Africa )
[12] => Array ( [0] => Spain )
[13] => Array ( [0] =>England )
[14] => Array ( [0] => Germany )
[15] => Array ( [0] => Mali )
and i want it to be converted to this format:
[10] => Array ( [0] => Nigeria [1] => South Africa [2] => Spain [3] =>England )
[11] => Array ( [0] => Germany [1] => Mali )
Please how can it be done? Thanks
Re: Array Conversion In PHP by Adesege(m): 12:36pm On Oct 31, 2015
What code have you tried?

Can you post the code that generated the first code so I can have a look??

What programming language are you using also?
Re: Array Conversion In PHP by Dlastofmykind(op): 12:57pm On Oct 31, 2015
The code is lengthy. many lines of codes.

The above snippet is just like an illustration of how the array will be displayed but the concept of converting it to the oda array is what I'v not really figured out.
Re: Array Conversion In PHP by Nobody: 2:56pm On Oct 31, 2015
Use array_chunk to fix this...huh
Re: Array Conversion In PHP by Dlastofmykind(op): 3:28pm On Oct 31, 2015
Jregz:
Use array_chunk to fix this...huh
Ok. Thanks
Re: Array Conversion In PHP by Adesege(m): 4:14pm On Oct 31, 2015
Dlastofmykind:
Ok. Thanks
Does that help??

If not, you can rewrite the code to show where you are having problem and paste here.

I cant work on abstract
Re: Array Conversion In PHP by thewebcraft(m): 11:48pm On Oct 31, 2015
try array_values function:

$array = array_values($myarray);
Re: Array Conversion In PHP by makavele:
Dlastofmykind:
The code is lengthy. many lines of codes.

The above snippet is just like an illustration of how the array will be displayed but the concept of converting it to the oda array is what I'v not really figured out.
no matter how long, it is always good to provide visuals . .
cos you could have made a very tiny error (not even in the arrays function) . . that would kill the whole fin . . .

meanwhile . . you can use multi-dimensional arrays . . . a working example below:

<?php

$data = array
(

array ("Africa", "Nigeria", "Mali", "Egypt", 'Ghana'),
array ('Europe', 'United Kingdom', 'Russia', 'France', 'Germany'),
array ('Asia', 'Japan', 'China', 'Hong Kong', 'Thailand'),

);


echo $data[0][0]. "n " . "countries include the following: " . $data[0][1] . ", " .
$data[0][2] . ", " . $data[0][3] . " and " . $data[0][4] . ".";

echo '<br>' . '<br>';

echo "Countries in " . $data[1][0]. " include: The " . $data[1][1] . ", " .
$data[1][2] . ", " . $data[1][3] . " and " . $data[1][4] . ".";

echo '<br>' . '<br>';

echo "And others like " . $data[2][1] . ", " . $data[2][2] . ", " . $data[2][3] . " and " . $data[2][4] . " can be found in " . $data[2][0] . ".";

?>
and it will output the following:

African countries include the following: Nigeria, Mali, Egypt and Ghana.

Countries in Europe include: The United Kingdom, Russia, France and Germany.

And others like Japan, China, Hong Kong and Thailand can be found in Asia.
1 Reply

Java Array ProblemCan An Array In Java Hold Multiple Types?What The Different Between ENUM And ARRAY In Java234

Sales And Installations Of Wallpaper And 3D Wall PanelsA Simpler Website Testing Tool Is Launched By Google Targeting Small BusinessesP2P Site Web Designers, Based In Kogi, Makurdi,abuja