This question already has answers here:
How to access array elements
(6 answers)
Closed 4 years ago.
$form = array(
array(
'form' => 'Change Schedule',
'data' => array(
array(
'element'=>'input',
'name'=>'form-start',
'class'=>'form-control',
'type'=>'text',
'column'=>'col-md-12',
'label'=>'Schedule'
)
),
),
array(
'form' => 'Maintenance',
'data' => array(
array(
'element'=>'input',
'name'=>'form-room-place',
'class'=>'form-control',
'type'=>'text',
'column'=>'col-md-12',
'label'=>'Room # / Place'
)
),
),
);
This is the array I made, I wanted to get the array with form = Maintenance only. Is this possible with php to get the array via string arrays I want to pass?
My attempt:
$form(('form'=>'Change Dormitory'));
You've got 2 arrays inside the first array, so the internal arrays are $form[0] or $form[1].
You could then do $form[1]["form"] to get "Maintenance"
if you want to get the array data inside form = "Maintenance",
you can filter by form value like this:
$newForm = array();
foreach ($form as $key => $value) {
if ($value['form'] == 'Maintenance') {
$arr[] = $value;
}
}
var_dump($newForm);
Related
This question already has answers here:
PHP Associative Array Duplicate Keys
(6 answers)
Closed 4 months ago.
I have an array in my php code
$list = array(
'RETAIL' => 'SUPERMARKET'
'RETAIL' => 'BAR'
'RETAIL' => 'DEP. MARKET'
'BUSINESS' => 'HOTEL'
'BUSINESS' => 'PUB'
'OTHER' => 'GROCERY'
'OTHER' => 'BUTCHERY'
// I have 20+ items
);
foreach( $list as $type => $name ){
var_dump($type,$name);
}
//var_dump() output
// RETAIL SUPERMARKET
// BUSINESS HOTEL
// OTHER BUTCHERY
I'm facing the problem that when I try to loop the array only three values will be returned and the rest are ignored. How I can fix this?
I'm trying to loop the array to save the data into a custom wordpress database. With the same way I've successfully looped another array inserted the keys and values into the db.
I think a better structure for your array would something like this
$list = [
'RETAIL' => [
'BAR',
'RESTAURANT'
]
];
And you could loop over like so
foreach ($list as $businessType => $businesses) {
foreach ($businesses as $business) {
echo "<li>{$business}</li>";
}
}
Just an example
As a general way of handling instances where you have more than one element to each piece of data (even when it's not in a tree structure like this may be), you should structure each item in the list as either an array or object, eg:
$list_of_arrays = [
['RETAIL', 'SUPERMARKET'],
['RETAIL', 'BAR'],
['RETAIL', 'DEP. MARKET'],
];
foreach( $list_of_arrays as $array ){
echo "<li>{$array[0]} {$array[1]}</li>";
}
or
$list_of_objects = [
(object)['type' => 'RETAIL', 'subtype' => 'SUPERMARKET'],
(object)['type' => 'RETAIL', 'subtype' => 'BAR'],
(object)['type' => 'RETAIL', 'subtype' => 'DEP. MARKET'],
];
foreach( $list_of_objects as $object ){
echo "<li>{$object->type} {$object->subtype}</li>";
}
Maybe I show heavily but as you only have two data for each entity, why your table is not built like this at the base...?
$list = array(
'SUPERMARKET'=>'RETAIL',
'BAR'=>'RETAIL',
'DEP. MARKET'=>'RETAIL',
'HOTEL'=>'BUSINESS',
'PUB'=>'BUSINESS',
'GROCERY'=>'OTHER',
'BUTCHERY'=>'OTHER'
// I have 20+ items
);
You do have keys that uniquely identify entities.
This question already has answers here:
How do I Sort a Multidimensional Array in PHP [duplicate]
(10 answers)
Sorting multidimensional array, upper case being sorted before lowercase
(2 answers)
Closed 8 years ago.
I try to find any solution with sort multi-dimensional arrays, but don't see anything match my requirement.
Here is my example that I want.
I want to sort below array by "views" DESC
$arr = array(
0=>array(
'id'=>11,
'views'=>10,
'title'=> 'html'
),
1=>array(
'id'=>12,
'views'=>4,
'title'=> 'Java'
),
2=>array(
'id'=>13,
'views'=>100,
'title'=> 'Boostrap'
)
);
to
$arr = array(
0=>array(
'id'=>13,
'views'=>100,
'title'=> 'Boostrap'
),
1=>array(
'id'=>11,
'views'=>10,
'title'=> 'html'
),
2=>array(
'id'=>12,
'views'=>4,
'title'=> 'Java'
)
);
without loop in php.
How can I do this.
This question already has answers here:
Forcing a SimpleXML Object to a string, regardless of context
(11 answers)
Closed 8 years ago.
I'm working on storing result of xml feed into database... I'm able to load file... but when I want to store data into array, it stores instead of value ([title] = 'Klapka 120mm';) this:
[title] => SimpleXMLElement Object ( [0] => Klapka 120mm )
Do you know, where might be problem?
Source code:
Here is part of one function:
$import_file = simplexml_load_file($this->input->post('import_url')); // load file from url
$affected_products = 0;
foreach($import_file->SHOPITEM as $product) {
$affected_products += $this->import_product($product);
}
Here is first part of function import_product:
public function import_product($product)
{
/* save product data into array */
$data = array(
'title' => $product->PRODUCT,
'content' => $product->DESCRIPTION,
'price' => $product->PRICE,
'price_vat' => $product->PRICE_VAT,
'ean' => $product->EAN,
'count' => $product->AVAILABILITY
);
die(print_r($data));
Thank you very much for your replies
You have to cast the elements to strings, as all these elements are instances of SimpleXMLElement.
$data = array(
'title' => (string)$product->PRODUCT,
'content' => (string)$product->DESCRIPTION,
'price' => (string)$product->PRICE,
'price_vat' => (string)$product->PRICE_VAT,
'ean' => (string)$product->EAN,
'count' => (string)$product->AVAILABILITY
);
For some of them a cast to an integer or a float may be of interest
i got a little problem with phalcon php and multidimensional forms.
I'm adding elements to a phalcon form like this:
$display = new Text('language[en-US][display]');
$display->setLabel($t->_('Display'));
$display->addValidator(new PresenceOf(array(
'message' => 'The display field is required'
)));
$this->add($display);
And in the view this results to:
<input type="text" name="language[en-US][display]" value="some value">
which is totally fine and my expected output, but it seems like due to the fact that php automatically converts this syntax (language[en-US][display]) to this array:
array(
'language' => array(
'en-US' => array(
'display' => 'some value'
)
)
)
the phalcon php Phalcon\Forms\Form method isValid() cannot assign it to the right form element, when passing it the $_POST data, so my question is, is there a easy way to convert that array to an array like this:
array(
'language[en-US][display]' => 'some value'
)
Just iterating with foreach is enough?
$array = array(
'language' => array(
'en-US' => array(
'display' => 'some value'
)
)
);
$newkey = key($array);
foreach($array[$newkey] as $key=>$val) {
$newkey .= "[".$key."]";
foreach ($val as $subkey=>$subval) {
$newarray[$newkey. "[".$subkey."]"] = $subval;
}
}
var_dump($newarray);
This question already has answers here:
Merging arrays with the same keys
(6 answers)
Closed 8 years ago.
What I'm trying to do is read an INI file with PHP and then merge the array with another array. For example:
$a1 = parse_ini_file("file1.ini",true);
$a2 = parse_ini_file("file2.ini",true);
$info = array_merge($a1,$a2);
Now here's my problem, if I merge the arrays: The sub-arrays will be completely replaced if it is written in the second ini file.
file1.ini
[BASIC]
title=Title
otherInfo=1
[SECONDSECTION]
thestuffs=3
file2.ini
[BASIC]
otherInfo=6
Now if I were to look at the merged data $info I would get something like this:
$info = array(
"BASIC" => array(
"otherInfo" => 6
),
"SECONDSECTION" => array(
"thestuffs" => 3
),
);
DESIRED RESULT:
$info = array(
"BASIC" => array(
"title" => "Title"
"otherInfo" => 6
),
"SECONDSECTION" => array(
"thestuffs" => 3
),
);
Change array_merge() to array_merge_recursive().