I seem to be having mental issues when it comes to arrays in php. I am not sure why, How ever my array looks like this:
$elementOptions = array(
array(
'type' => 'Text',
'name' => 'test' ,
'isRequired' => true,
'attributes' => array(
'placeholder' => 'content'
),
'subFormName' => 'content'
);
I have a for each loop as such:
foreach ($options as $key => $value) {
if (is_array($value)) {
//do something else
} else {
//do something
}
}
The issue is, if I do a var dump inside the if(isarray()){} I get the following back:
array(1) {
["placeholder"]=>
string(7) "content"
}
array(4) {
["type"]=>
string(4) "Text"
["name"]=>
string(4) "test"
["isRequired"]=>
bool(true)
["attributes"]=>
array(1) {
["placeholder"]=>
string(7) "content"
}
}
now the issue is - I do not want the following in that var dump:
array(1) {
["placeholder"]=>
string(7) "content"
}
I am not sure, based on the "data structure" above, how this 'placeholder' => 'content' is considered an array.....in either case I do not want it as part of the arrays that are var dumped.... it should just be the second array in that var dump coming back.
And that's where you guys come in, why is the place holder coming back as an array when it shouldn't (TMK - to my knowledge).
"placeholder"=>"content" isn't considered an array or coming back as an array, it's a pair in an array that you've called "attributes"
array(4) {
["type"]=>
string(4) "Text"
["name"]=>
string(4) "test"
["isRequired"]=>
bool(true)
["attributes"]=> // this is your array
array(1) { // contents of this array are...
["placeholder"]=> // this is a key
string(7) "content" // this is a variable
} // array ends
}
Related
I'm new to Laravel, and I want to store this array in DB.
This is the php code of my array:
$socialNetwork = array();
$socialNetwork[0]["name"]= "Facebook";
$socialNetwork[0]["account"]= "facebook_account";
$socialNetwork[1]["name"]= "Twitter";
$socialNetwork[1]["account"]= "twitter_account";
$socialNetwork[2]["name"]= "Instagram";
$socialNetwork[2]["account"]= "insta_account";
The var_dump() looks like this:
array(3) {
[0] => array(2) {
["name"] => string(8) "Facebook"
["account"] => string(16) "facebook_account"
}
[1] => array(2) {
["name"] => string(7) "Twitter"
["account"] => string(15) "twitter_account"
}
[2] => array(2) {
["name"] => string(9) "Instagram"
["account"] => string(13) "insta_account"
}
}
I've tried several things but I can't get it to work!
Please help with the code. The table name is socialAccounts
Add a column in your database for this field; a JSON or TEXT type will do the job.
Next, you should add the column to the $casts array on your SocialAccount model:
protected $casts = [
'facebook_account' => 'array',
];
Now, whenever you retrieve this value, it will be deserialized for you.
To store the value, just use json_encode():
$social_account->facebook_account = json_encode($facebookArrayData);
$social_account->save();
You can read more on attribute casting in the docs; https://laravel.com/docs/7.x/eloquent-mutators#attribute-casting
How to foreach through a deeper array in PHP? I want to approach 'price' and list all prices below each other.
$addons = get_product_addons($product->get_id());
When I VAR_DUMP the $addons var, it outputs the below.
array(1) {
[0]=>
array(7) {
["name"]=>
string(8) "Afmeting"
["description"]=>
string(0) ""
["type"]=>
string(6) "select"
["position"]=>
int(0)
["options"]=>
array(10) {
[0]=>
array(5) {
["label"]=>
string(8) "70 x 200"
["price"]=>
string(0) "70.00"
["min"]=>
string(0)""
...
So I want to output this result:
70.00
60.00
Etcetera.. *All prices
I guess that piece of code is what you are looking for:
foreach($addons as $addon)
{
echo $addon["options"]["price"].PHP_EOL;
}
You do not need to use foreach to access nested elements of array. Just use it's key.
PHP_EOL is a constant containing newline for your OS. For web application use special formatting suitable for your page (<br> e.g.)
You can walk or foreach through the items:
<?php
$data =
[
[
'name' => 'orange',
'options' =>
[
'price' => '6.00'
]
],
[
'name' => 'banana',
'options' =>
[
'price' => '4.00'
]
]
];
array_walk($data, function($v) {
echo $v['options']['price'], "\n";
});
Output:
6.00
4.00
Or you could create an array of prices and iterate on that (here using short function syntax):
$prices = array_map(fn($v)=>$v['options']['price'], $data);
var_export($prices);
Output:
array (
0 => '6.00',
1 => '4.00',
)
i have a question about these 2 ways of declaring the array (I thought they would be the same):
$result[$zone->id]['activities'][$activity->id] = array(
'title' => $activity->title,
'image' => $activity->image
);
$result[$zone->id]['activities'] = array(
$activity->id => array(
'title' => $activity->title,
'image' => $activity->image
)
);
So my goal is to provide an array that is sorted by the Zone then by it's activities listed under the array of "activities".
The first array gives me the following result which is correct for my example:
array(3) {
[5]=>
array(2) {
["title"]=>
string(15) "Oftalmologistas"
["image"]=>
string(28) "logotipo_1575907014_4232.png"
}
[6]=>
array(2) {
["title"]=>
string(7) "Óticas"
["image"]=>
string(28) "logotipo_1575907021_1130.png"
}
[7]=>
array(2) {
["title"]=>
string(21) "Outras especialidades"
["image"]=>
string(28) "logotipo_1575907034_8988.png"
}
}
But the second array gives me the last activity found and replaces the two above it doesn't add them to array instead it replaces them.
array(1) {
[7]=>
array(2) {
["title"]=>
string(21) "Outras especialidades"
["image"]=>
string(28) "logotipo_1575907034_8988.png"
}
}
My goal here is to understand the diference syntax between them why the first adds them to array while the seconds replaces. Also any other way of declaring the array to the same first value. Thanks in advance!
this is just simple nested arrays with different keys and values for better understanding i change it to this code:
$result[100]['activities'][200] = array(
'title' => 4000,
'image' => 3000
);
$result[300]['product'] = array(
444444=> array(
'title' => 5000,
'image' => 6000
)
);
echo '<pre>';
var_dump($result);
first we have two array and inside each of them again there is another two arrays with different key and values if you look at this picture i uploaded i think you can understand completely.
nested array result
for first example
$result[$zone->id]['activities'][$activity->id] = array(
'title' => $activity->title,
'image' => $activity->image
);
You are assigning value to key "$activity->id"
Here as id gone be dynamic it will create new key everytime.
In second example
$result[$zone->id]['activities'] = array(
$activity->id => array(
'title' => $activity->title,
'image' => $activity->image
)
);
You are assiging value/array to activities.
So every time you try to assign value to activities key it will
replace it.
So this below is the structure of JSON i have when I decode it in PHP, but for some reason I am having hard time to loop through this JSON object. I don't know how can I get each values of "incident","description","technique" from those array to save them In my DB.
array(1) {
["Access"]=>
array(2) {
[0]=>
array(3) {
["incident"]=>
string(19) "sssssssssssssssssss"
["description"]=>
string(10) "ssssssssss"
["technique"]=>
string(19) "Link "
}
[1]=>
array(3) {
["incident"]=>
string(18) "ssssssssssssssssss"
["description"]=>
string(0) ""
["technique"]=>
string(19) "Link "
}
}
}
So far I have this PHP code but it's returning me an error saying invalid argument in first foreach loop.
$objectFirst =($_POST['Access1']);
$data = json_decode($objectFirst,true);
foreach ($data->Access as $tech){
foreach($tech as $incident){
foreach($incident as $ss){
var_dump($ss->incident);
}
}
}
When you access the element with this notation, $data->Access, it means you try to access a property of the $data object. But in your case, $data is an array, therefore you have to use the array notation.
So it should be corrected as $data['Access']. One other issue in your code is the level of loops.
foreach ($data->Access as $tech){
foreach($tech as $incident){
foreach($incident as $ss){
var_dump($ss->incident);
}
}
}
The inner most loop is incorrect because $incident will contain a string, not an array. When you try to access $ss['incident'], it will fail. So just change it to:
foreach ($data['Access'] as $tech){
foreach($tech as $incident){
var_dump($incident);
}
}
Hope it helps!
<?php
$data = [
'access' =>
[
[
'foo' => 'I',
'bar' => 'got'
],
[
'foo' => 'a',
'bar' => 'big'
]
]
];
foreach($data['access'] as $array)
var_dump($array['foo'], $array['bar']);
Output:
string(1) "I"
string(3) "got"
string(1) "a"
string(3) "big"
I am working on a API project where i get an XML Object as a response. The response can contain one or more products in the NewOrder object(below).However when i try to display the info using a foreach loop it breaks if the only has one entry. i guess it is because the index [0] does not exist in the object.how can i through the xml object and display since there is no [0] i the object. OR how do i add the index [0] in the object.
object(stdClass)#49 (1) {
["NewOrder"] => object(stdClass)#50 (12) {
["BTN"] => string(10) "XXXXXXXXXXXXXXXxx"
["PreOrderTransactionId"] => string(22) "XXXXXXXX"
["PartnerOrderId"] => string(17) "XXXXXXXXXXX"
["QwestOrderId"] => string(9) "N57395699"
["SalesCode"] => string(7) "XXXXXXXX"
["OrderStatus"] => string(7) "Pending"
["OrderStatusCode"] => string(4) "OPEN"
["OrderStatusSourceSystem"] => string(5) "CPLUS"
["OrderStatusMessage"] => string(0) ""
["OrderStatusDate"] => string(10) "2013-12-09"
["OrderStatusTime"] => string(8) "08:02:30"
["ProductFamily"] => array(3) {
[0] => object(stdClass)#51 (2) {
["ProductFamilyName"] => string(7) "BUNDLE+"
["ProductLines"] => object(stdClass)#52 (3) {
["WTN"] => string(10) "3033689919"
["AppointmentDate"] => string(10) "2013-12-20"
["Products"] => object(stdClass)#53 (5) {
["ProductName"] => string(36) "INTERNET 100+ MBPS & HOME PHONE PLUS"
["Usoc"] => string(5) "BBBVC"
["Quantity"] => string(1) "1"
["Action"] => string(1) "I"
["Status"] => string(4) "OPEN"
}
}
}
}
}
}
I have tried the following but it didn't work:
if (!is_array($this->Orders->NewOrder)) {
$this->Order->NewOrder = array($this->Orders->NewOrder["NewOrder"]);
}
foreach ($this->Orders->NewOrder as $order){?>
I am getting the following error:
Fatal error: Cannot use object of type stdClass as array in
I think your NewOrder is only an array if it contains more than one object. Use something like this before your loop:
if (!is_array(yourObject["NewOrder"])) {
yourObject["NewOrder"] = array(yourObject["NewOrder"]);
}
The SoapClient has an option that always creates the array, even if here is only one element.
return new SoapClient(
'...',
array(
'location' => '...',
/.../
'features' => SOAP_SINGLE_ELEMENT_ARRAYS
)
);