Tour PHP two-dimensional arrangement

1

I need to go through an arrangement $data that is as follows:

array(2) { 
    [0]=> object(stdClass)#18 (4) { 
        ["Descripción"]=> string(5) "Aceite"
        ["Codigo"]=> string(7) "2222222" 
        ["Precio"]=> string(2) "45" 
        ["Eliminar"]=> string(9) " Eliminar" 
    }
    [1]=> object(stdClass)#19 (4) { 
        ["Descripción"]=> string(4) "Caja" 
        ["Codigo"]=> string(7) "1111111" 
        ["Precio"]=> string(2) "50"
        ["Eliminar"]=> string(9) " Eliminar" 
    } 
}

I currently have the following for that gives me an error, that I can not access the data

for ($i=0; $i < count($data) {
    $datos['codigo']      = $repuesto->codigo;
    $datos['descripcion'] = $repuesto->Descripcion;
    $datos['valor']       = $repuesto->Precio;
    $datos['token']       = $this->funciones->RandomCaracteres(49);
    }

How do I access the fix data?

    
asked by Javier Antonio Aguayo Aguilar 11.05.2017 в 18:27
source

1 answer

4

First of all, you have an error in your cycle for Syntax and you need to pass a last parameter of how your counter will increase ( $i )

// Te faltaba esto           |______|
for ($i = 0; $i < count($data); $i++) {
//                           |______|
}

Second, where does the variable $repuesto come from?

Third, it would be much more useful to use a cycle foreach as I explain in the following steps

Having your arrangement called $data

array(2) { 
    [0]=> object(stdClass)#18 (4) { 
        ["Descripción"]=> string(5) "Aceite"
        ["Codigo"]=> string(7) "2222222" 
        ["Precio"]=> string(2) "45" 
        ["Eliminar"]=> string(9) " Eliminar" 
    }
    [1]=> object(stdClass)#19 (4) { 
        ["Descripción"]=> string(4) "Caja" 
        ["Codigo"]=> string(7) "1111111" 
        ["Precio"]=> string(2) "50"
        ["Eliminar"]=> string(9) " Eliminar" 
    } 
}

You can access the values through a cycle foreach , where $data is the array to go $key would be the index of each array within the array and $val would be the object of type stdClass as such, then:

foreach($data as $key => $val){
    $val->Descripcion; // Aceite, Caja
    $val->Codigo;      // 2222222, 1111111
    $val->Precio;      // 45, 50
    $val->Eliminar;    // Eliminar, Eliminar
}
    
answered by 11.05.2017 / 18:36
source