I am using Laravel, and I have a foreach function like this
$name = person::find(1);
foreach($name as $dbitems) {
$person->services()->updateOrCreate([
'service' => $dbitems->Name,
'time' => $dbitems->Time
'price' => $anotherarrayhere
]);
}
In the place of $anotherarrayhere, I want to get this array from the outside of foreach function.
$anotherarrayhere = $peopleserviceprice[]
How can I include the $anotherarrayhere variable in the above foreach function?
foreach is not a function, you can use variables from outside the loop without problems. Maybe you're confused with collection callbacks that needs to specific tell variables
Any way, you're iterating over a model, not a collection, since you're using find.
$person = person::find(1);
$person->services()->updateOrCreate([
'service' => $person->name,
'time' => $person->time
'price' => $anotherarrayhere
]);
Related
I am new in PHP. I am working for send data using PHP API. Its method is like below
$createContact->setCustomFieldValues(
[
array(
'customFieldId' => 'pcVFD6',
'value' => array('35')
),
array(
'customFieldId' => 'pcVnzW',
'value' => array('37')
)
]
);
I have data in array like this
$aa = array("pcVFD6"=>"35", "pcVnzW"=>"37");
I want pass this values to above function but I am not getting idea about proper way to do it.
I have tried something like this
foreach ($aa as $key => $value){
$createContact->setCustomFieldValues([
array(
'customFieldId' => $key,
'value' => array($value)
)
]
);
}
its working but passing only last one array to API. Anyone here can please help me for achieve my goal?
Thanks!
You need to transform the array before you pass it to setCustomFieldValues. You don't want to be calling setCustomFieldValues multiple times as your current attempt does, as that's not equivalent to the original. You just need to change the structure of the array ahead of time.
For example:
$aa = array("pcVFD6"=>"35", "pcVnzW"=>"37");
$transformedArr = array();
foreach ($aa as $key => $value){
$transformedArr[] = array(
'customFieldId' => $key,
'value' => array($value)
);
}
$createContact->setCustomFieldValues($transformedArr);
This is a simple question yet I'm unable to find an answer to this. I'm not looking for corrections to my code, simply education regarding this issue.
The array is defined before the first foreach begins so I can use it outside of the loop.
$arrayVar = array();
foreach ($variables as $key => $variable){
$arrayVar = array(
'name' => $squad['full_name'],
'position' => $squad['position']
);
}
This populates the array with data. However, when used in another loop, the array resets instead of appending to the end.
EDIT: John's answer resolves the issue. A simple inclusion of square brackets has saved me around 1000 lines.
You keep overwriting your array in every iteration instead of appending to it.
$arrayVar = array();
foreach ($variables as $key => $variable){
$arrayVar[] = array( // <= Add to array instead of overwriting it
'name' => $squad['full_name'],
'position' => $squad['position']
);
}
I am using PHP 7.1.33 and Laravel Framework 5.8.36.
I am getting receiving data from a database row-by-row and I am creating a model using updateOrCreate() like the following:
foreach ($arr as $v) {
$product = new Product();
$product->name = $v['name'];
$product->url = $v['url'];
$product->price = $v['price'];
// save
$matchThese = ['name' => $name, 'url' => $url];
$product->updateOrCreate($matchThese);
}
However, nothing gets created.
Any suggestions what I am doing wrong?
I appreciate your replies!
updateOrCreate takes 2 parameters ($attributes, $values).
$attributes is an array containing key-value pairs which will basically be used for where clauses i.e. it will check the database for the attributes passed in this array.
$values is an array containing key-value pairs of what to update in the database.
If it can't find a row in the database matching the first array it will combine the values in the arrays to create a new row.
To achieve what you're after you can do:
Product::updateOrCreate(
['name' => $v['name'], 'url' => $v['url']],
['price' => $v['price']]
);
Try this way
Product::updateOrCreate(
['name' => $name, 'url' => $url],
['price' => v['price']],
)
First parameter is for search the register.
Second parameter set new values.
For more information, you can read Eloquent: Getting Started - Documentation
I am using PHP7.
I have an Object I am trying to parse:
$RECORD = {
'name' => 'Stephen Brad Taylor',
'address' => '432 Cranberry Hills, Pittsburg',
'phone' => '708 865 456',
'Account' => (Object Vendor/Entity/User) {
'email' => 'INeedThisEmail#pleaseHelp.com' // I want to access this property.
'id' => 34,
'accessible' => ['email', 'id]
}
}
I have an array which I am using to select certain fields from RECORD:
$fieldnames = [
'name',
'address',
'phone',
'Account["email"]'
];
I am trying to parse the fieldnames from RECORD as follows:
$data[]
foreach($fieldnames as $k => $fieldname) {
$data[k] = $RECORD->$fieldname
}
The method above works for the first-level attributes: name, address, and phone. However, email returns null.
I have tried the following below and none have worked:
$data[k] = RECORD->${$fieldname}
$propertyName = '$RECORD->$fieldname'
$data[k] = ${$propertyName}
Does anyone know a way to access an object's properties using a string from an object reference?
Much gratitude <3
You can't use Account["email"] directly as a property accessor, it won't be split up to find the nested property. You need to parse it yourself.
foreach($fieldnames as $k => $fieldname) {
if (preg_match('/^(.*)\["(.*)"\]$/', $fieldname, $match) {
$data[$k] = $RECORD->{$match[1]}->{$match[2]};
} else {
$data[$k] = $RECORD->$fieldname;
}
}
Also, you need $ in $k.
This code only works for 1 level deep. If you need to deal with arbitrary levels, you'll have to write a recursive procedure. See How to access and manipulate multi-dimensional array by key names / path? for examples of how to code this.
I use anonymous functions for such cases. (PHP 7.4)
$index = fn($item) => $item->dotaItem->prop_def_index;
if (get_class($collection) === Collection::class) {
$index = fn($item) => $item->prop_def_index;
}
Here is a sample request which works, but the names are hard coded
$client->batchDeleteAttributes(array(
'DomainName' => $domainName,
'Items' => array(
array('Name' => '5149572a86deb5161fbb22bdab',),
array('Name' => '5149572a86deb5161fbf7487b9',),
)
));
I can get the name values using the following loop
foreach($_POST['d'] as $key => $value)
{
}
I am confused how to integrate the foreach loop with the api request. I assume I need to create an array using the foreach loop and then use that array in the api request but I don't know the syntax.
Thanks!
Without knowing the format of your $_POST data it's hard to give a detailed solution, but something along these lines should get you heading in the right direction:
$names = array();
foreach ($_POST['d'] as $value)
{
$names[] = array('Name' => $value);
}