2

I have an array which looks like this

Array
(
[0] => Array
    (
        [0] => RANO17
        [1] => RANO99
    )

[1] => Array
    (
        [0] => Test Product Ranosys
        [1] => Rano test example
    )

[2] => Array
    (
        [0] => 7
        [1] => 2
    )

[3] => Array
    (
        [0] => 
        [1] => 
    )

[4] => Array
    (
        [0] => 200.0000
        [1] => 100.0000
    )

[5] => Array
    (
        [0] => 
        [1] => 
    )

[6] => Array
    (
        [0] => 1400
        [1] => 200
    )

)

I want to make 1D array from above array. 1D array looks like.

Array(
[0] => Array
(
  [0] => RANO17
  [1] => Test Product Ranosys
  [2] => 7
  [3] => 
  [4] => 200.0000
  [5] => 
  [6] => 1400
)

[1] =>Array 
(
  [0] => RANO99
  [1] => Rano test example
  [2] => 2
  [3] => 
  [4] => 100.0000
  [5] => 
  [6] => 200
)
)

I don't have any idea how can we do this I was googling from last two hours but didn't get any solution yet. How can we do this either using some array functions or any programming logic please help.

8
  • Have you tried anything, so far? If not, look into foreach or array_map. There aren't any existing functions that will do exactly what you want, out of the box.. Commented Jan 12, 2017 at 6:50
  • yes I do but I am getting only one array. Commented Jan 12, 2017 at 6:51
  • Array ( [0] => RANO17 [1] => Test Product Ranosys [2] => 7 [3] => [4] => 200.0000 [5] => [6] => 1400 ) Commented Jan 12, 2017 at 6:51
  • Is this 1d array or two 1d arrays ? Commented Jan 13, 2017 at 7:04
  • It is 1d array @VforVendetta Commented Jan 13, 2017 at 7:14

3 Answers 3

3
$newArr1 = array();
$newArr2 = array();

foreach ( $arr AS $element ) {
  $newArr1[] = $element[ 0 ];
  $newArr2[] = $element[ 1 ];
}
Sign up to request clarification or add additional context in comments.

8 Comments

Your second code block won't do what you think. $newArr[ $i ] will always only contain $element[ 1 ].
Thank you for your help @Ben but what will be the solution if I have dynamic values in multiarray ?
@Ramkishan - You need to specify those things in your question. The first code block does do what you want when using your example data. If it can look different, you need to specify how. Don't move the goalpost.
sorry @MagnusEriksson one minute I update the question
@Ramkishan - You should rather apologize to the people that have wasted time by answering your inaccurate question. You should keep this question as is, mark an answer and write a new question with all the details instead.
|
1

I solved it myself. It will work if multiarray has dynamic values. output of $orders_rows

Array
(
  [0] => Array
  (
    [0] => RANO17
    [1] => RANO99
  )

 [1] => Array
 (
    [0] => Test Product Ranosys
    [1] => Rano test example
 )

 [2] => Array
 (
    [0] => 7
    [1] => 2
 )

 [3] => Array
 (
    [0] => 
    [1] => 
 )

 [4] => Array
 (
    [0] => 200.0000
    [1] => 100.0000
 )

 [5] => Array
 (
    [0] => 
    [1] => 
 )

 [6] => Array
 (
    [0] => 1400
    [1] => 200
 )

)

There's code to solve my problem

foreach ($orders_rows as $singlerows) {
        for ($k = 0; $k < count($singlerows[0]); $k++) {
            for ($i = 0; $i < count($singlerows); $i++) {
                $output_array[] = $singlerows[$i][$k];
            }
        }
    }
    $orders_row = array_chunk($output_array, 7);

output of $orders_row

Array
(
  [0] => RANO17
  [1] => Test Product Ranosys
  [2] => 7
  [3] => 
  [4] => 200.0000
  [5] => 
  [6] => 1400
)
Array
(
  [0] => RANO99
  [1] => Rano test example
  [2] => 2
  [3] => 
  [4] => 100.0000
  [5] => 
  [6] => 200
)

Comments

0

Cycle through the array and dump the element of each array into separate arrays.

$myArray = array (array(1, "testing 1"), array(2, "testing 2"), array(3, "testing 3"));

$arrayOne = array();
$arrayTwo = array();

For each element of the main array, echo the two elements of the sub array into separate arrays of their own.

foreach ($myArray as $a) {
    $arrayOne[] = $a[0];
    $arrayTwo[] = $a[1];
}

print the results

print_r($arrayOne);
echo "\n\n";
print_r($arrayTwo);

Results in:

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
)


Array
(
    [0] => testing 1
    [1] => testing 2
    [2] => testing 3
)

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.