Transform php array to an array with objects?

2

I get the following array, all data are separated by a *, I tried to use explode and implode without results, I think something more complex is needed to solve it using php 5.6

Array
       (
        [0] => 
        [1] => 1
        [2] => Dato2
        [3] => Dato3
        [4] => Dato4 
        [5] => 
        [6] => Dato6
        [7] => Dato7
        [8] => Dato8
        [9] => Dato9
        [10] => *
        [11] => 2
        [12] => Dato2
        [13] => Dato3
        [14] => Dato4 
        [15] => 
        [16] => Dato6
        [17] => Dato7
        [18] => Dato8
        [19] => Dato9
        [20] => *

    )

I would like to separate by "*" and make it look like this

Array
    (
    [0] => stdClass Object
        (
            [1] => 1
            [2] => Dato2
            [3] => Dato3
            [4] => Dato4 
            [5] => 
            [6] => Dato6
            [7] => Dato7
            [8] => Dato8
            [9] => Dato9
        )

    [1] => stdClass Object
        (
            [11] => 2
            [12] => Dato2
            [13] => Dato3
            [14] => Dato4 
            [15] => 
            [16] => Dato6
            [17] => Dato7
            [18] => Dato8
            [19] => Dato9
        )
    )

Thank you for your attention I will be attentive to your answers greetings.

    
asked by Javier Antonio Aguayo Aguilar 08.08.2018 в 18:19
source

3 answers

2

It would be something like this:

<?php
$array = [1,'dato1','dato2','*',2,'dato3','dato4','*'];
$objects = array();
$object = array();
foreach ($array as $i => $val) {
    if ($val == '*') {
        $objects[] = $object;
        $object = array();
    } else {
        $object[] = $val;
    }
}
var_dump($objects);
?>
    
answered by 08.08.2018 / 18:29
source
1

If you want to keep the keys, I would do something like this:

$array =["","1","Dato2","Dato3","Dato4","","Dato6","Dato7","Dato8","Dato9","*","2","Dato2","Dato3","Dato4",""]

$array_out = array();
$array_tmp = array();

foreach($array as $key=>$value)
{
   if(trim($value) != "*")
   { 
      $array_tmp[$key] = $value;
   }
   else
   { 
      array_push($array_out, $array_tmp);
      $array_tmp = array();
   }
} 
    
answered by 08.08.2018 в 19:39
1

given an original array:

<?php

$array_original = [
    '',
    1,
    'Dato2',
    'Dato3',
    'Dato4',
    '',
    'Dato6',
    'Dato7',
    'Dato8',
    'Dato9',
    '*',
    2,
    'Dato2',
    'Dato3',
    'Dato4',
    '',
    'Dato6',
    'Dato7',
    'Dato8',
    'Dato9',
    '*',
];

plus a final array that starts empty:

$array_final = [];

And a reference index for the subset of data within the final array:

$index_objeto = 0;

You can iterate over the original array by inserting data into the corresponding subset. Every time you find a * instead of inserting the data in the subset, you update $index_objeto .

Now, you want the final output to be an array of objects and not an array of arrays, so every time you create a subset you should cast it to stdClass

$array_final[$index_objeto] = (object) [];

So the iteration would be:

foreach ($array_original as $index => $value) {
    if ($value === '*') {

        // si * es el primer elemento, no incremento el subconjunto
        if( $index!==0 ) { 
          $index_objeto++;
        }
    } else {
        // declaro el nuevo objeto en la iteración siguiente al *
        if (!array_key_exists($index_objeto, $array_final)) {
            $array_final[$index_objeto] = (object) [];
        }
        $array_final[$index_objeto]->$index = $value;
    }
}

There are then four things to notice in the answer:

  • The subset within $array_final must be casted using (object)
  • The assignment to it must use an object operator $objeto->propiedad = valor instead of an array operator $array[propiedad] = valor .
  • Only increment $index_objeto when the value is * and it is not in the first position, to avoid that the final answer contains empty objects.
  • In the same way, if I find a * I declare the new object in the next iteration, just in case there is nothing else in the array and, again, I do not want to end with empty objects
  • You can see it working in:   link

        
    answered by 08.08.2018 в 19:29