I have some code that generates an array with an object in it and I want to access the object values.
$results = array();
$std_arr = array();
$data['examlist'] = $this->exam_model->get($exam);
foreach ($student_array as $key => $value) {
$std_arr[] = $value->student_id;
$results[] = $this->student_model->get_single_student_result($value->student_id);
}
and When i log the results i got
Array
(
[0] => Array
(
[0] => stdClass Object
(
[exam_schedule_id] => 14
[get_marks] => 23.00
[id] => 1
[exam_id] => 1
[teacher_subject_id] => 1
[name] => GEO
[teacher_id] => 16
[subject_id] => 1
[code] => Geography
)
)
[1] => Array
(
[0] => stdClass Object
(
[exam_schedule_id] => 14
[get_marks] => 0.00
[id] => 1
[exam_id] => 1
[teacher_subject_id] => 1
[name] => GEO
[teacher_id] => 16
[subject_id] => 1
[code] => Geography
)
How can I access these 'stdClass Object' property?
Related
I have product options that came from database and I want to merge all the values by same product_id .
I'm using a foreachloop.
stdClass Object(
[id] => 22
[product_id] => 48
[values] => Array (
[0] => stdClass Object (
[id] => 81
[option_id] => 22
[name] => Black
[value] => black
)
)
),
stdClass Object(
[id] => 10
[product_id] => 50
[values] => Array (
[0] => stdClass Object (
[id] => 33
[option_id] => 10
[name] => L
[value] => l
),
[1] => stdClass Object (
[id] => 34
[option_id] => 10
[name] => M
[value] => m
)
)
),
stdClass Object(
[id] => 24
[product_id] => 48
[values] => Array (
[0] => stdClass Object (
[id] => 98
[option_id] => 11
[name] => X
[value] => x
),
[1] => stdClass Object (
[id] => 99
[option_id] => 11
[name] => XL
[value] => xl
),
[2] => stdClass Object (
[id] => 100
[option_id] => 11
[name] => XLL
[value] => xll
)
)
)
I want to combine these array by same product_id and get output like :
stdClass Object(
[id] => 22
[product_id] => 48
[values] => Array (
[0] => stdClass Object (
[id] => 81
[option_id] => 22
[name] => Black
[value] => black
),
[1] => stdClass Object (
[id] => 98
[option_id] => 11
[name] => X
[value] => x
)
[2] => stdClass Object (
[id] => 99
[option_id] => 11
[name] => XL
[value] => xl
),
[3] => stdClass Object (
[id] => 100
[option_id] => 11
[name] => XLL
[value] => xll
)
),
stdClass Object(
[id] => 10
[product_id] => 50
[values] => Array (
[0] => stdClass Object (
[id] => 33
[option_id] => 10
[name] => L
[value] => l
),
[1] => stdClass Object (
[id] => 34
[option_id] => 10
[name] => M
[value] => m
)
)
)
I'm using foreach loop , How to do this inside foreach loop ?
I want to merge values of same product_id. What is the best way to do this ?
My code is:
foreach($data['products'] as $pro){
foreach($pro->options as $opt){
debug($opt);
}
}
You can try something like this :
1/ Your data
$array = /* array with all your product obj */;
// Create your new result array
$result = array();
2/ The foreach loop
// Loop throught all your object
foreach ($array as $obj) {
// Your current product id
$current_product_id = $obj->product_id;
// The values of this product
$current_product_values = $obj->values;
// Loop through all the value of the product
foreach ($current_product_values as $value) {
// Add those value to your result array for the current product id
$result[$current_product_id][] = $value;
}
}
Is it what you are looking for?
I am try to format an array to a JSON object that highcharts supports. My array from the database is as follows:
Array
(
[0] => Array
(
[Group_ID] => 1
[Name] => A line graph
[month] => 4
[amount] => 7700
)
[1] => Array
(
[Group_ID] => 2
[Name] => B Line graph
[month] => 4
[amount] => 390
)
[2] => Array
(
[Group_ID] => 1
[Name] => A line graph
[month] => 5
[amount] => 5000
)
[3] => Array
(
[Group_ID] => 2
[Name] => B line graph
[month] => 5
[amount] => 210
)
)
I need to create an array like this to be able to create a highchart compatible JSON object:
Array
(
[0] => Array
(
[name] => A revenue
[data] => Array
(
[4] => 7700 //amount for the fourth month
[5] => 5000 //amount for the fifth month
)
)
[1] => Array
(
[name] => B revenue
[data] => Array
(
[4] => 390 //amount for the fourth month
[5] => 210 //amount for the fifth month
)
)
)
I have managed to come up with this array using my foreach but I cant seem to find a way to do it correctly:
Array
(
[0] => Array
(
[name] => A line graph
[amount] => 7700
[month] => 4
)
[1] => Array
(
[name] => B line graph
[amount] => 390
[month] => 4
)
[2] => Array
(
[name] => A line graph
[amount] => 5000
[month] => 5
)
[3] => Array
(
[name] => B line graph
[amount] => 210
[month] => 5
)
)
My foreach:
foreach ($data as $key => $value) {
$r[] = [
'name' => $value['Line_GraphName'],
'data' => $value['amount'],
'month' => $value['month']
];
}
You can create it very simply like this:
foreach ($data as $value) {
$r[$value['Group_ID']]['name'] = $value['Line_GraphName'];
$r[$value['Group_ID']]['data'][$value['month']] = $value['amount'];
}
Loop and create a result with Group_ID as the key and add the name key and value
Add data array and append month as key and amount as value
If you don't like having Group_ID as the key and want to re-index:
$r = array_values($r);
The solution using array_walk and array_values functions:
$result = [];
array_walk($data, function($v) use(&$result) {
$key = $v['Group_ID'] . $v['Name'][0]; // compound key: "group_id + revenue's prefix"
if (!isset($result[$key])) {
$result[$key] = ['name' => $key[1] . " Revenue", 'data' => [$v['month'] => $v['amount']]];
} else {
$result[$key]['data'][$v['month']] = $v['amount'];
}
});
print_r(array_values($result));
The output:
Array
(
[0] => Array
(
[name] => A Revenue
[data] => Array
(
[4] => 7700
[5] => 5000
)
)
[1] => Array
(
[name] => B Revenue
[data] => Array
(
[4] => 390
[5] => 210
)
)
)
I want to convert this array in a single dimensional flat array without losing the sort order.
Array
(
[0] => Array
(
[id] => 1
[title] => Computer
[parent_id] => 0
[children] => Array
(
[0] => Array
(
[id] => 4
[title] => keyboard
[parent_id] => 1
[children] => Array
(
[0] => Array
(
[id] => 6
[title] => Mouse
[parent_id] => 4
[children] => Array
(
[0] => Array
(
[id] => 7
[title] => webcam
[parent_id] => 6
)
)
)
)
)
)
)
[1] => Array
(
[id] => 43
[title] => Mobile
[parent_id] => 0
[children] => Array
(
[0] => Array
(
[id] => 5
[title] => bar phones
[parent_id] => 43
)
[1] => Array
(
[id] => 47
[title] => Touchscreen
[parent_id] => 43
[children] => Array
(
[0] => Array
(
[id] => 41
[title] => Samsung
[parent_id] => 47
)
[1] => Array
(
[id] => 44
[title] => Micromax
[parent_id] => 47
)
[2] => Array
(
[id] => 45
[title] => Huawei
[parent_id] => 47
)
)
)
)
)
[2] => Array
(
[id] => 46
[title] => Camera
[parent_id] => 0
)
[3] => Array
(
[id] => 42
[title] => Heater
[parent_id] => 0
)
)
Give it try with below function:
function makeOneDimensionArray(array $array, &$res = array())
{
foreach($array as $arr)
{
$res[] = array(
'id' => $arr['id'],
'title' => $arr['title'],
'parent_id' => $arr['parent_id']
);
if(isset($arr['children']))
{
makeOneDimensionArray($arr['children'], $res);
}
}
return $res;
}
$finalArr = makeOneDimensionArray($your_array);
print_r($finalArr);
I need to pull the ShipQty from each of the following STDClass Objects and insert it into a new array.
Array ( [0] => stdClass Object ( [PRODUCTID] => 2 [ORDERID] => 7 [SHIPQTY] => 3 [STATUS] => 1 [SHIPDATE] => 2013-12-18 ) [1] => stdClass Object ( [PRODUCTID] => 2 [ORDERID] => 5 [SHIPQTY] => 2 [STATUS] => 1 [SHIPDATE] => 2014-01-02 ) [2] => stdClass Object ( [PRODUCTID] => 2 [ORDERID] => 2 [SHIPQTY] => 5 [STATUS] => 1 [SHIPDATE] => 2014-01-08 ) [3] => stdClass Object ( [PRODUCTID] => 2 [ORDERID] => 1 [SHIPQTY] => 2 [STATUS] => 1 [SHIPDATE] => 2014-01-16 ) )
I want the following: array(3,2,5,2)
Thank you in advance!
try this (assuming your array is named $myArr):
$result = array();
foreach ($myArr as $obj) {
$result[] = $obj->SHIPQTY;
}
Hope this helps.
I have one array in php like this :
Array
(
[0] => Array
(
[name] => abc
[id] => 107
[CycleNumber] => 1
[Type] => People
)
[1] => Array
(
[name] => john
[id] => 312
[CycleNumber] => 5
[Type] => People
)
[2] => Array
(
[name] => jenny
[id] => 110
[CycleNumber] => 3
[Type] => People
)
[3] => Array
(
[name] => metting room
[id] => 590
[CycleNumber] => 4
[Type] => Facility
)
[4] => Array
(
[name] => projector
[id] => 470
[CycleNumber] => 4
[Type] => Facility
)
)
I want to replace the duplicate type with blank. but the first one should have that type name and others should have blank. so the result array should be like this :
Array
(
[0] => Array
(
[name] => abc
[id] => 107
[CycleNumber] => 1
[Type] => People
)
[1] => Array
(
[name] => john
[id] => 312
[CycleNumber] => 5
[Type] =>
)
[2] => Array
(
[name] => jenny
[id] => 110
[CycleNumber] => 3
[Type] =>
)
[3] => Array
(
[name] => metting room
[id] => 590
[CycleNumber] => 4
[Type] => Facility
)
[4] => Array
(
[name] => projector
[id] => 470
[CycleNumber] => 4
[Type] =>
)
)
I want array in this format only. and i am using PHP zend.
I search for this but most of them showing to remove that element from array. but i don't want to remove it. i want to replace it with blank but want to show the first one.
Tried Code
$result = array();
$result1 = array();
$result2 = array();
$y = array();
$y1 = array();
foreach ($data as $entry) {
$type= $entry["type"];
if (!isset($y[$type])) {
$y[$type] = array();
unset($entry["type"]);
$result[$type][] = $entry;
}
}
can anyone tell me how to do that ?
Code
$i = 0;
$found = 0;
foreach($data as $key=>&$val) {
if($i != 0) {
if($data[$i]['Type'] == $data[$found]['Type']) {
$data[$i]['Type'] = "";
}
else {
$found = $i;
}
}
$i++;
}
echo "<pre>";
print_r($data);
Output
Array
(
[0] => Array
(
[name] => abc
[id] => 107
[CycleNumber] => 1
[Type] => People
)
[1] => Array
(
[name] => john
[id] => 312
[CycleNumber] => 5
[Type] =>
)
[2] => Array
(
[name] => jenny
[id] => 110
[CycleNumber] => 3
[Type] =>
)
[3] => Array
(
[name] => metting room
[id] => 590
[CycleNumber] => 4
[Type] => Facility
)
[4] => Array
(
[name] => projector
[id] => 470
[CycleNumber] => 4
[Type] =>
)
)
Example