1

I have a one dimensional array which looks like this:

$table = array("rnbqkbnr", 
               "pppppppp", 
               "00000000", 
               "00000000",
               "00000000", 
               "00000000", 
               "00000000", 
               "PPPPPPPP", 
               "RNBQKBNR" );

What I want to do is make that array a two dimensional, each element to be an array on it's own, so I can access every letter.

Something like this:

$arr= array(
    array("r", "n", "b", "q", "k", "b", "n", "r"),
    array("p", "p", "p", "p", "p", "p", "p", "p"),
    array("0", "0", "0", "0", "0", "0", "0", "0"),
    array("0", "0", "0", "0", "0", "0", "0", "0"),        
    array("0", "0", "0", "0", "0", "0", "0", "0"),        
    array("0", "0", "0", "0", "0", "0", "0", "0"),
    array("P", "P", "P", "P", "P", "P", "P", "P"),
    array("R", "N", "B", "Q", "K", "B", "N", "R")
);

I tried different finctions like str_split(), or array_chunk(), or explode(), but everytime I end up getting the first function ($table) which is one dimensional and I can't acces every element separately.

Every help is appreciated :)

0

7 Answers 7

4

You can already access every letter.

A string is series of characters (Php Doc)

This work's fine.

print_r($table[0][0]); // return r
Sign up to request clarification or add additional context in comments.

Comments

3

You can use array_map coupled with str_split to arrive at a pretty easy solution to this:

<?php

$table = array(
   "rnbqkbnr", 
   "pppppppp", 
   "00000000", 
   "00000000",
   "00000000", 
   "00000000", 
   "00000000", 
   "PPPPPPPP", 
   "RNBQKBNR" 
);


$table = array_map('str_split', $table); // thanks @RolandStarke

var_dump($table);

Comments

1

You can loop through each of the array in your outer array:

for($i=0; $i<count($table); $i++){
    $table[$i] = str_split($table[$i]);
}

Comments

1

Just use str_split inside a foreach loop

foreach($table as $key=>$line)
{
 $table[$key]=str_split($line);
}

Comments

0

You can do as following. Just split each string to characters.

$table = array("rnbqkbnr",
    "pppppppp",
    "00000000",
    "00000000",
    "00000000",
    "00000000",
    "00000000",
    "PPPPPPPP",
    "RNBQKBNR");

$newArray = array();
foreach ($table as $t) {
    $newArray[] = str_split($t);
}

Comments

0

This code should help fix your problem up. Also, it should work for various array sizes.

$table = array("rnbqkbnr",
    "pppppppp",
    "00000000",
    "00000000",
    "00000000",
    "00000000",
    "00000000",
    "PPPPPPPP",
    "RNBQKBNR");

for ($i = 0; $i != count($table); ++$i) {
    if (isset($subArray)) {
        unset($subArray);
    }

    for ($j = 0; $j != strlen($table[$i]); ++$j) {
        $subArray[] = substr($table[$i], $j, 1);
    }

    $twoDimensionalArray[] = $subArray;
}

print_r($twoDimensionalArray);

Comments

0

Not sure what you've tried but this should work:

foreach($table as $v)
{
    $arr[] = str_split($v);
}
unset($v);

3 Comments

why would you be looping with $v as a reference when not even using the reference? Just seems to me to be an unnecessary risk waiting to happen if you forget the unset. You could also just do $v = ... and it will overwrite the original value in the array.
Passing by reference is faster? Even if that's true, this isn't going to be the bottleneck in the program. It's better to code for readability instead of micro-optimizing like that. Passing something by reference and then not using it makes the code less readable IMO.
I show passing by reference is pretty much the same all over. 3v4l.org/Z4Asq, both trading spaces as to which was faster.

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.