Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,150,831 members, 7,810,200 topics. Date: Friday, 26 April 2024 at 11:14 PM

Array Conversion In PHP - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Array Conversion In PHP (987 Views)

Java Array Problem / Can An Array In Java Hold Multiple Types? / What The Different Between ENUM And ARRAY In Java (2) (3) (4)

(1) (Reply) (Go Down)

Array Conversion In PHP by Dlastofmykind(m): 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(m): 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...
Re: Array Conversion In PHP by Dlastofmykind(m): 3:28pm On Oct 31, 2015
Jregz:
Use array_chunk to fix this...
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);

1 Like

Re: Array Conversion In PHP by makavele: 1:18am On Nov 01, 2015
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)

Need A Male Or Female Web Developer / You Want To Build A Career In Mobile Application Development? / Stop Waiting Start Developing

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 18
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.