-1

I would like to know how to separate the values of an array like this below, separated by variables: https://prnt.sc/pi98a5

1 - 7  - Artes 1 - artes1
2 - 10 - Artes 2 - artes2
3 - 8  - Artes 3 - artes2

ARRAY

$array = 
    [   
        ["1",           "2",            "3"         ],
        ["7",           "10",           "8"         ],
        ["Artes 1",     "Artes 2",      "Artes 3"   ],
        ["artes1",      "artes2",       "artes3"    ]
    ];

FOREACH

I tried to do it myself, but it got pretty confusing for me.

foreach ($array as $key => $row){

    foreach ($array[$key] as $key2 => $row2){

        echo $row2; //1237108Artes 1Artes 2Artes 3artes1artes2artes3

    }

}

TEST ONLINE

- https://repl.it/@tiagocaus/EgftYyui

- https://ideone.com/yahAkQ

LF00
  • 27,015
  • 29
  • 156
  • 295
Tiago
  • 797
  • 1
  • 10
  • 23
  • 1
    What do you mean by `show` in your print screen https://prnt.sc/pi98a5 ? Where are you displaying this data? Is it a command line or a browser or elsewhere ? – nice_dev Oct 12 '19 at 09:13
  • https://stackoverflow.com/a/73108176/2943403 might give some inspiration, but I find your [mcve] to be unclear/lacking. – mickmackusa Jul 26 '22 at 03:48

5 Answers5

2

Using tabs (this depends on where the output is going to), you can just implode() the data, using array_column() to extract the column from all of the rows...

for ( $i = 0; $i < count($array[0]); $i++ ) {
    echo implode("\t-\t", array_column($array, $i)).PHP_EOL;
}

gives...

1   -   7   -   Artes 1 -   artes1
2   -   10  -   Artes 2 -   artes2
3   -   8   -   Artes 3 -   artes3

To just transform the data...

$trans = [];
for ( $i = 0; $i < count($array[0]); $i++ ) {
    $trans[] =  array_column($array, $i);
}
Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
  • How do I put the values in separate variables so that I can retrieve them elsewhere? https://prnt.sc/pi98a5 – Tiago Oct 12 '19 at 08:30
  • I need to transform that array (https://prnt.sc/pi98a5), and save it that way (https://prnt.sc/pi9hth), so I asked how to lose the values. Did you get it? – Tiago Oct 12 '19 at 09:16
  • Sorry - only just back from the gym. I know you have accepted another answer, but just updated my answer for the sake of completion. – Nigel Ren Oct 12 '19 at 10:32
  • That's what it most looks like, but it's not good yet. If you can help me I change the concluding merchant. – Tiago Oct 12 '19 at 19:59
  • Not sure what you mean by *I change the concluding merchant* – Nigel Ren Oct 12 '19 at 20:00
  • Where there is that green option, where we mark it was completed. – Tiago Oct 12 '19 at 20:05
  • His code is saving the array between `{}`, but should save between `[]`, correct? – Tiago Oct 12 '19 at 20:07
1
$array = 
    array(  
        ["1",           "2",            "3"         ],
        ["7",           "10",           "8"         ],
        ["Artes 1",     "Artes 2",      "Artes 3"   ],
        ["artes1",      "artes2",       "artes3"    ]
    );

    $newArray=array();
    foreach ($array as $key => $value) {
        foreach ($value as $key2 => $value2) {
            $newArray[$key2][]=$value2;

        }
    }
    foreach ($newArray as $key => $value) {
        foreach ($value as $key2 => $value2) {
             if($key2!=0)
             echo " - ";    
             echo "$value2";
        }
        echo "<br>";
    }
Mukesh Yadav
  • 101
  • 6
1

This will help you:

<table>
<?php
$array = [ ["1", "2", "3" ], ["7", "10", "8" ], ["Artes 1", "Artes 2", "Artes 3" ], ["artes1", "artes2", "artes3" ] ];
foreach ($array as $key => $row){ 
echo "<tr>";
foreach($array as $key2=> $row2){
echo "<td>".$row2[$key]."</td>";
} 
echo "</tr>";
}?>
</table>

Edit: Try to understand code

<table>
<?php
$array = [ ["1", "2", "3" ], ["7", "10", "8" ], ["Artes 1", "Artes 2", "Artes 3" ], ["artes1", "artes2", "artes3" ] ];
$new_arr=[];
foreach ($array as $key => $row){
$var=[];
echo "<tr>";
foreach($array as $key2=> $row2){
echo "<td>".$row2[$key]."</td>";
array_push($var,$row2[$key]);
} 
array_push($new_arr,$var);
echo "</tr>";
}
foreach($new_arr as $key => $row){
foreach($row as $key2=> $row2){
echo $row2;
}
}?>
</table>

This will create $new_array array. Try to echo and see output from same code

Ritesh Khandekar
  • 3,885
  • 3
  • 15
  • 30
  • How do I put the values in separate variables so that I can retrieve them elsewhere? https://prnt.sc/pi98a5 – Tiago Oct 12 '19 at 08:24
  • Define new variables in primary loop and use `array_push()` function in secondary loop – Ritesh Khandekar Oct 12 '19 at 08:31
  • I need to transform that array (https://prnt.sc/pi98a5), and save it that way (https://prnt.sc/pi9hth), so I asked how to lose the values. Did you get it? – Tiago Oct 12 '19 at 09:17
1

Tranpose the array, then join each line like, Demo

$array_transpose = null;
foreach($array as $k_row => $row){
    foreach($row as $k_col => $v){
        $array_transpose[$k_col][$k_row] = $v;
    }
}
foreach($array_transpose as $row){
    echo join("\t-\t",$row) . "\n";
}
LF00
  • 27,015
  • 29
  • 156
  • 295
  • How do I put the values in separate variables so that I can retrieve them elsewhere? https://prnt.sc/pi98a5 – Tiago Oct 12 '19 at 08:42
  • I don't catch it, what value to save? – LF00 Oct 12 '19 at 08:44
  • I need to transform that array (https://prnt.sc/pi98a5), and save it that way (https://prnt.sc/pi9hth), so I asked how to lose the values. Did you get it? – Tiago Oct 12 '19 at 08:59
  • `$array_transpose` is the transformed array, have you check it? – LF00 Oct 12 '19 at 09:00
  • Yes, but I don't know how to ride the way I need it https://prnt.sc/pi9hth – Tiago Oct 12 '19 at 09:02
  • I need to transform that array (https://prnt.sc/pi98a5), and save it that way (https://prnt.sc/pi9hth), so I asked how to lose the values. Did you get it? – Tiago Oct 12 '19 at 09:16
1

So basically the answer provided by other users work, but i think you are not able to understand how you can put these values somewhere as a separate string.

$array =[

    ["1",           "2",            "3"         ],
    ["7",           "10",           "8"         ],
    ["Artes 1",     "Artes 2",      "Artes 3"   ],
    ["artes1",      "artes2",       "artes3"    ]
];

$arr = [];
$newArray=array();
foreach ($array as $key => $value) {
    foreach ($value as $key2 => $value2) {
        $newArray[$key2][]=$value2;

    } 
}

$arr = [];
foreach($newArray as $val){
    // $arr[] = implode(' - ', $val);

    $key = $val[0];

    $arr[$key] = array(
    "title" => $val[2],
    "var" => $val[3],
    "value" => $val[1],
    );
}

print_r($arr);


// A            B        C         D
// 1    -       7   - Artes 1 - artes1
// 2    -       10  - Artes 2 - artes2
// 3    -       8   - Artes 3 - artes3
Umer Abbas
  • 1,866
  • 3
  • 13
  • 19
  • I need to transform that array (https://prnt.sc/pi98a5), and save it that way (https://prnt.sc/pi9hth), so I asked how to lose the values. Did you get it? – Tiago Oct 12 '19 at 09:00
  • I'm not able to see your screenshots, use this [online screen shot tool] (https://snipboard.io/) and post the links of screenshot again – Umer Abbas Oct 12 '19 at 09:06
  • I need to transform that array (https://snipboard.io/yI0n1C.jpg), and save it that way (https://snipboard.io/NBMo08.jpg), so I asked how to lose the values. Did you get it? – Tiago Oct 12 '19 at 09:13
  • I don't understand, second screenshot shows mixed values, one array object is different than the other array object. please show the expected result that you need in your output ? – Umer Abbas Oct 12 '19 at 09:17
  • I'm glad you understood – Tiago Oct 12 '19 at 09:20
  • I just have to finish this. – Tiago Oct 12 '19 at 09:21
  • I just edited my answer, please check, here is a link to the working [code](https://tehplayground.com/KiPLTJO5TVOsw8DC) – Umer Abbas Oct 12 '19 at 09:27
  • Your code is saving the array between `{}`, but should save between `[]`, correct? – Tiago Oct 12 '19 at 20:08
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/200762/discussion-between-tiago-and-omar-abbas). – Tiago Oct 12 '19 at 20:12