Hi i have a PHP array that looks like this When var_dumped
array (size=3)
0 =>
array (size=8)
'Product_code' => string 'CAB55FR' (length=7)
'Elapsed' => string '0' (length=1)
'Count' => string '42' (length=2)
'Scrap' => string '0' (length=1)
'ScrapPercentage' => string '0.00' (length=4)
'Perhour' => null
'Target' => float 40
'Eff' => string '105%' (length=4)
1 =>
array (size=8)
'Product_code' => string 'MSTACK60' (length=8)
'Elapsed' => string '0.05' (length=4)
'Count' => string '0' (length=1)
'Scrap' => string '0' (length=1)
'ScrapPercentage' => null
'Perhour' => string '0.00' (length=4)
'Target' => string 'No Target set' (length=13)
'Eff' => string 'No Target set' (length=13)
However what i want the array to look like is this
array (size=3)
0 =>
object(stdClass)[8]
public 'Product_code' => string 'CAB55FR' (length=7)
public 'Elapsed' => string '0' (length=1)
public 'Count' => string '42' (length=2)
public 'Scrap' => string '0' (length=1)
public 'ScrapPercentage' => string '0.00' (length=4)
public 'Perhour' => null
1 =>
object(stdClass)[9]
public 'Product_code' => string 'MSTACK60' (length=8)
public 'Elapsed' => string '0.05' (length=4)
public 'Count' => string '0' (length=1)
public 'Scrap' => string '0' (length=1)
public 'ScrapPercentage' => null
public 'Perhour' => string '0.00' (length=4)
To create the first array i am using this snippet of code
$todayarray[] = Array(
'Product_code' => $v->Product_code,
'Elapsed' => $v->Elapsed,
'Count' => $v->Count,
'Scrap' => $v->Scrap,
'ScrapPercentage' => $v->ScrapPercentage,
'Perhour' => $v->Perhour,
'Target' => $value,
'Eff' => $eff
);
this is run in a foreach, What would be the best way to get the array this is generating to look like the second example?
$todayarray[] = (object) Array( ... );would cast the array to an object and achieve what you want. See : 3v4l.org/VWd0b$v), which you're going to great lengths to turn into an array, yet what you really want is the original object? Then just don't do all this array conversion...?! I don't get it.