I have a query:
$stockBook = DB::select(DB::Raw(" my query"));
dump($stockBook);
My o/p:
array:1 [▼
0 => {#533 ▼
+"BOOKID": "1"
+"REMAINING": 17.0
}
]
I want to retrieve the REMAINING attribute data, but I am getting error. I tried :
$remain_ = $stockBook[1]['REMAINING'];
Undefined offset: 1
again I tried
$remain_ = $stockBook->REMAINING;
Trying to get property 'REMAINING' of non-object
How can get the value of REMAINING attribute?
if you have only single object inside array then you can use this
$arr = array:1 [▼
0 => {#533 ▼
"BOOKID": "1"
"REMAINING": 17.0
}
]
$object = collect($arr);
$item = $object->first();
dd($item->REMAINING);
But if you have multiple objects inside array then use this
$arr = array:1 [▼
0 => {#533 ▼
"BOOKID": "1"
"REMAINING": 17.0
},
1 => {#533 ▼
"BOOKID": "1"
"REMAINING": 17.0
}
]
$object = collect($arr);
$list = $object->pluck("REMAINING");
dd($list);
By using above example you will get list of all the Remainings from array object.
Hope this will help you.
Array numeration starts with 0 so $array[0] is the first element, not $array[1].
$stockBook = (array)$stockBook;
$remain_ = $stockBook[0]['REMAINING'];
Try this:
$stockBook[0]->REMAINING;
If you want to retrieve element from array use []
If you want to retrieve element from object use ->
In your example we see that you have array of php objects,
then you should use both.
foreach((array)$stockBook as $key => $value){ // $stockBook or $stockBook[0]
var_dump($key, $value);
}
// or
var_dump($stockBook->BOOKID, $stockBook->REMAINING); // $stockBook or $stockBook[0]
If this is your data structure
array:1 [▼
0 => {#533 ▼
+"BOOKID": "1"
+"REMAINING": 17.0
}
]
and this is what you do
$remain_ = $stockBook[1]['REMAINING'];
Undefined offset: 1
Then the error message explain the first part of your problem. You are trying to access the element with index 1 in an array that only has one element with index 0. Arrays are zero-based. That means, the first element is 0, the second is 1 etc. So you are trying to access an element that doesn't exist.
The second issue, according to your comment
Cannot use object of type stdClass as array when I did $remain_ =
$stockBook[0]['REMAINING'];
indicates that each element in the array is an object.
So to get the REMAINING attribute of the object at index 0 in the array you could do.
echo $stockBook[0]->REMAINING; // would print "17.0"
Related
I am new to laravel. I have an array called data which is fetching the data as a form of nested array. Its structure looks like this.
array:1 [▼
0 => array:2 [▼
0 => {#3 ▼
+"id": "8"
+"title": "Accounting"
+"type": {
+"id": "18"
+"title": "BookKeeping"
}
}
1 => {#3 ▼
+"id": "10"
+"title": "Accounting"
+"type": {
+"id": "20"
+"title": "Balancesheet"}
}
]
]
This is the data that I am getting in the form of a nested array. I need to find a maximum id i.e. 10 so that I can get the associated type title based on the max id. I tried in several ways by using array helpers, and using foreach loop but could not succeed.Any kind of help and support is appreciated. Hope to get a positive rep
you can use foreach.
$array = <your array variable>;
$max = 0;
foreach($array as $value) if($value['id'] > $max) $max = $value['id'];
or if you want get key of this variable
$array = <your array variable>;
$max = 0;
foreach($array as $key => $value) if($value['id'] > $max) $max = $key;
or if your array is in ascending order you can just get your last elements' id like this:
$array = <your array variable>;
$max = array_pop($array)['id'];
note: but if you use array_pop you lost last element - https://www.php.net/manual/en/function.array-pop.php
and there is way to not lost
$array = <your array variable>;
$max = end($array)['id'];
I have the following array containing one or more objects:
array:1 [▼
0 => ApiS7File {#484 ▼
+id: 19
+type: "file"
+z: "e1a4f81f.f90428"
+name: ""
+filename: "example/example.txt"
}
]
If the user suplies me with an options array
$options = ['filename' => 'hello', 'name' => 'thanks']
I want the array object to be overwritten using the user suplied values:
array:1 [▼
0 => ApiS7File {#484 ▼
+id: 19
+type: "file"
+z: "e1a4f81f.f90428"
+name: "thanks"
+filename: "hello"
}
]
How can I achieve this?
This might solve your problem.
//assuming $arr is your array
foreach($arr as $a){
foreach($options as $key=>$value){
$a->$key = $value;
}
}
return $arr;
You can use array_replace,
$result = array_replace($yourArray, $options);
Here is syntax for the same
$basket = array_replace($base, $replacements,// you can pass multiple arrays);
I have an PHP object - $new_step.
In Laravel, if I dd on a child object that is an array - dd($new_step->data->options); - of it, I get a valid entry:
array:5 [▼
0 => {#695 ▼
+"position": 1
+"value": "Dogs"
}
1 => {#694 ▼
+"position": 2
+"value": "Cats"
}
2 => {#693 ▼
+"position": 3
+"value": "Ferretts"
}
3 => {#669 ▼
+"position": 4
+"value": "Gophers"
}
4 => {#665 ▼
+"position": 5
+"value": "Possum"
}
]
However, when I try to do a foreach on the object:
foreach ($new_step->data->options as $options) {
$options->count = 0;
}
I get this error:
ErrorException
Undefined property: stdClass::$options
Why does the foreach fail?
Turns out that there was an array in the iteration that was an object and not an array. To test, I moved the for loop further out of the chain and tested each iteration thru it to see that the 5th iteration was the one responsible for causing the error.
To do that foreach you must store a result of Object in the variable.
$temp = $new_step->data->options;
foreach ($temp as $options) {
$options->count = 0;
}
At the end, you will get in your list count variable for each array as you want?
Do dd($temp) below foreach to check result.
If you again don't get right array probably you have some bug in $new_step->data->options.
I have a multi dimensional array in the following form
array:2 [▼
"dashboardData" => array:1 [▶]
"widgetData" => array:5 [▼
0 => {#214 ▼
+"_id": "575fcf6d298fbfd833000041"
+"created": "2016-06-14T09:33:33.492Z"
+"dashboardid": "575fcebc298fbfd833000036"
+"datasource": {#215 ▶}
}
1 => {#249 ▶}
2 => {#285 ▶}
3 => {#297 ▶}
4 => {#333 ▶}
]
]
I have deleted a lot of data but the above just be enough to demonstrate what I am after. Essentially, the widgetData part of my array has a random order for its elements.
As you can see above though, each element in this part of my array has a created value. Is it possible to order just this part of my array (widgetData) based on the created date?
Thanks
Because your dates are using standard UTC date strings, you can sort your array by iterating the components and assigning the values to the original array (assuming it's variable), i.e:
let arrayCopy = array
for index in 0..<arrayCopy.count {
if let widgetData: [[String: AnyObject]] = array[index]["widgetData"] as? [[String: AnyObject]] {
let sortedWidgetData = widgetData.sort({ (dictionary1, dictionary2) -> Bool in
return (dictionary1["created"] as! String) < (dictionary2["created"] as! String)
})
array[index]["widgetData"] = sortedWidgetData
}
}
You could also just do this at runtime on your datasource as and when it's needed.
I have an array which looks like the following
array:2 [▼
0 => array:1 [▼
"input1" => "Something"
]
1 => array:1 [▼
"input2" => ""
]
]
Now the first element will always have some data. It is the second element I am interested in. At the moment, I am trying this
if(!empty($clientGroup[0][1]) || !empty($clientGroup[1][1]))
var_dump("Some Data");
} else {
var_dump("Both Empty");
}
The else should only be triggered if both elements are empty e.g.
array:2 [▼
0 => array:1 [▼
"input1" => ""
]
1 => array:1 [▼
"input2" => ""
]
]
If one of them have any data, the if should be triggered (so for the first array I showed, the if should be triggered).
How would I go about doing this, empty does not seem to work.
Thanks
The 2nd level keys do not exist so you will always be told the values are empty. Change the line
if(!empty($clientGroup[0][1]) || !empty($clientGroup[1][1]))
to,
if(!empty($clientGroup[0]['input1']) || !empty($clientGroup[1]['input2']))
and you should get the results you're after.
It's not realy 2D array because you have associative array inside an other array.
you must use key name (input1, input2) to access the value.
I recommend to use
if($retourdata[0]["input1"] !== "" || $retourdata[1]["input2"] !== "")