How to overwrite an objects values using an array php - php

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);

Related

get key of multi dimension array (PHP Language)

Hi I have a problem regarding multi-dimension array. I have a set of array looking like this
array:3 [▼
0 => array:1 [▼
"tree" => "0"
]
1 => array:1 [▼
"tree" => "2"
]
2 => array:1 [▼
"tree" => "0"
]
]
the array is from
dd($this->treeArray);
how do I want to get the array which the value is 0. Expected result/filter
For example:
array:2 [▼
0 => array:1 [▼
"tree" => "0"
]
1 => array:1 [▼
"tree" => "0"
]
]
or
array:1 [▼
0 => array:1 [▼
"tree" => "2"
]
]
if changing the index is impossible, its ok..
Thank you
As you are using Laravel, you could use array helper such as this
You can use like this:
use Illuminate\Support\Arr;
$newTreeArray = Arr::where($this->treeArray, function ($tree) => $tree['tree'] === "0");
do you want in_array()?
$arraysContainingZero=[];
foreach($this->treeArray as $wutkey => $wut){
if(in_array("0", $wut, true)){
$arraysContainingZero[$wutkey] = $wut;
}
}
var_dump($arraysContainingZero);
There are a lots of build in function in PHP that is useful for operating on an array. You can see the full list here: https://www.php.net/manual/en/ref.array.php
One of the function is called array_filter (https://www.php.net/manual/en/function.array-filter.php). The function iterates over each value in the array passing them to the callback function. If the callback function returns true, the current value from array is returned into the result array.
So in your case, you want to call the array_filter with a callback that return true if the value is 0. Example:
$input = [
[ 'tree' => 1 ],
[ 'tree' => 0 ],
[ 'tree' => 0 ],
];
$output = array_filter($input, function($arr) {
return $arr['tree'] === 0;
});
var_dump($output);
// result
array(2) {
[1]=>
array(1) {
["tree"]=>
int(0)
}
[2]=>
array(1) {
["tree"]=>
int(0)
}
}

Unable to add 2 associative arrays

I am trying to add 2 associative arrays, but the new array only comes with an index for the second array.
This is what I am doing
$array = ['name' => 'address', 'value' => 'us'];
$arr = ['name' => 'joe', 'value' => 'doe'];
$arr[] = $array;
This is the result
array:3 [▼
"name" => "joe"
"value" => "doe"
0 => array:2 [▶]
]
I am expecting something like this
array:2 [▼
0 => array:2 [▶]
1 => array:2 [▶]
]
As you can see, there is no index for the first array, thus making the count 3 instead of 2. Please how do I fix this?
Just create another array and add the 2 arrays to it:
$newAr = [];
$newAr[] = $array;
$newAr[] = $arr;
var_dump($newAr);

how to get data to save from arrays inside array

i have form to make a bill for multiple products,
that form return request like this
#parameters: array:5 [▼
"quantity" => array:2 [▼
0 => "1"
1 => "2"
]
"product" => array:2 [▼
0 => "Mr. Jasen Beer,OliveDrab,XS"
1 => "Carlotta Yundt"
]
"date" => array:2 [▼
0 => "2019-12-29"
1 => "2019-12-29"
]
"id" => array:2 [▼
0 => "15"
1 => "11"
]
]
}
i need to loop all arrays to make insert all at once
thanks.
Try to loop it like this, you will get the array wrap every product's attributes:
$inserted_array = [];
foreach($parameters as $key => $values) {
foreach($values as $index => $val) {
$inserted_array[$index][$key] = $val;
}
}
\DB::table('table_name')->insert($inserted_array);
And I found that there is id in your array, remember to make it fillable.

Problem Iterating through a multi-dimensional array in PHP

Working on a Laravel application whereby I am consuming some data from an API. I get the response as a JSON object and convert to array. It appears as a complex multi-dimensional array
(nested arrays). Am trying to loop through it using a nested foreach so as to reach out to the id of each item but I keep failing..
The response is stored in a variable called usmDet
The array response
array:1 [▼
0 => array:1 [▼
0 => array:3 [▼
"id" => "74696"
"agents" => array:13 [▶]
"policies" => array:481 [▶]
]
1 => array:3 [▼
"id" => "1525"
"agents" => array:8 [▶]
"policies" => array:357 [▶]
]
]
1 => array:1 [▼
0 => array:3 [▼
"id" => "73401"
"agents" => array:1 [ …1]
"policies" => array:8 [ …8]
]
1 => array:3 [▼
"id" => "210"
"agents" => array:13 [ …13]
"policies" => array:773 [ …773]
]
]
]
My nested foreach
foreach($usmDet as $key => $value){
if(is_array($value)){
foreach($value as $key => $value){
echo $key." ".$value."<br>";
}
}
echo "<br>";
}
The id is part of the array, as you can access it like $value['id']
In the second foreach to prevent confusion you should select a different name for the key and the value.
Try it like this:
foreach($usmDet as $key => $value){
if(is_array($value)){
foreach($value as $k => $v){
echo $v['id'] . "<br>";
}
}
}
Result:
74696
1525
73401
210
Php demo
To get all the values for key "id" when multiple nested arrays, you could use array_walk_recursive
$ids = [];
array_walk_recursive($usmDet, function($value, $key) use (&$ids){
if ($key === "id") {
$ids[] = $value;
}
});
print_r($ids);
Php demo

How to compare last 2 elements of an array?

I have an array:
array:8 [▼
0 => array:1 [▼
"data" => "789"
]
1 => array:1 [▼
"data" => "800"
]
2 => array:1 [▼
"data" => "789"
]
3 => array:1 [▼
"data" => "787"
]
4 => array:1 [▼
"data" => "787"
]
5 => array:1 [▼
"data" => "787"
]
6 => array:1 [▼
"data" => "787"
]
7 => array:1 [▼
"data" => "787"
]
]
I need to take out the last 2 elements of the array and compare them. I tried using $getLast2 = array_slice($chart_data, -2, 2, true); to get the last 2.
array:2 [▼
6 => array:1 [▼
"data" => "787"
]
7 => array:1 [▼
"data" => "787"
]
]
Which then splits it. But Im not sure how to compare these 2 elements within this new array. As the last 2 elements which are now 6 and 7 could change as more data is added. I basically need to tell if the first element is great than, less than or equal to the second element.
You can use built in end() function and then prev():
if (end($chart_data) == prev($chart_data)) {
echo 'Two last elements of an array are equal!';
}
Pass last variable as false;
preserve_keys
Note that array_slice() will reorder and reset the numeric array indices by default. You can change this behaviour by setting preserve_keys to TRUE.
array_slice($chart_data, -2, 2, false);
You have output this array
array:2 [▼
6 => array:1 [▼
"data" => "787"
]
7 => array:1 [▼
"data" => "787"
]
]
Re-index them to using PHP array_values() function
$outputedArray = array_values($outputedArray)
if($outputedArray[0]['data'] > $outputedArray[1]['data'])
echo "0 index is greater";
If the array you obtained from your previous code is called $splitArray, then you could do the following:
list($array1, $array2) = $splitArray;
if ($array1['data']>$array2['data'])
{
echo "1st is greater than 2nd<br>";
}
else
{
echo "1st is not greater than 2nd<br>";
}
If it suits you, you can just use the array_pop to do the comparison in this manner:
<?php
$array = [
["data" => "789"],
["data" => "800"],
["data" => "789"],
["data" => "787"],
["data" => "787"],
["data" => "789"],
["data" => "787"],
["data" => "787"],
];
// MAKE A COPY OF THE ORIGINAL ARRAY:
$arrayCopy = $array;
// POP THE LAST ELEMENT OFF THE $arrayCopy AND SAVE IT AS $lastElem:
$lastElem = array_pop($arrayCopy);
// POP THE LAST ELEMENT OFF THE $arrayCopy AGAIN AND SAVE IT AS $beforeLastElem:
$beforeLastElem = array_pop($arrayCopy);
// NOW YOU CAN COMPARE THE LAST ELEMENT AND THE ONE BEFORE IT
if($lastElem == $beforeLastElem){
echo "The last 2 Elements of \$array are the same";
}else{
echo "The last 2 Elements of \$array are not identical.";
}

Categories