I have four $_POST arrays generated from multiple rows of a table
$event_id_array = $_POST['event_id'];
$booth_array = $_POST['booth'];
$parking_array = $_POST['parking'];
$load_in_array = $_POST['load_in'];
I would like to rearrange the arrays to be keyed by $event_id_array as the key of each of the other arrays = an 'event_id'
Arrays are:
$event_id_array =
array(52) {
["'3'"]=>
string(1) "3"
["'5'"]=>
string(1) "5"
["'7'"]=>
string(1) "7"
["'8'"]=>
string(1) "8"
}
$booth_array =
array(52) {
["'3'"]=>
string(1) "1"
["'5'"]=>
string(1) "2"
["'7'"]=>
string(1) "4"
["'8'"]=>
string(0) ""
}
$parking_array =
array(52) {
["'3'"]=>
string(1) "1"
["'5'"]=>
string(1) "2"
["'7'"]=>
string(0) ""
["'8'"]=>
string(0) ""
}
$load_in_array =
array(52) {
["'3'"]=>
string(4) "1:00"
["'5'"]=>
string(4) "2:00"
["'7'"]=>
string(4) "1:15"
["'8'"]=>
string(0) ""
}
I would like the new array to be efficiently update a MySQL database and was thinking a structure of the following would be best:
array(
event_id[3] => array(
'booth' => "1",
'parking' => "1",
'load_in' => "1:00"),
event_id[5] => array(
'booth' => "2",
'parking' => "2",
'load_in' => "2:00"),
event_id[7] => array(
'booth' => "4",
'parking' => "",
'load_in' => "1:15"),
event_id[8] => array(
'booth' => "",
'parking' => "",
'load_in' => ""),
)
or any other way to easily update multiple records easily
Related
I need to convert the multidimensional array to an associative array.
Please help me to convert the array. Need to remove the keys "sub_code" & "credits" and make the first string as "key" & the second string as "value". I tried a lot of ways but I failed.
Need to convert the below array.
array(9) {
[0]=> array(2)
{
["sub_code"] => string(6) "HS6151"
["credits"] => string(1) "4"
}
[1]=> array(2)
{
["sub_code"] => string(6) "MA6151"
["credits"] => string(1) "4"
}
[2]=> array(2)
{
["sub_code"] => string(6) "PH6151"
["credits"] => string(1) "3"
}
[3]=> array(2)
{
["sub_code"] => string(6) "CY6151"
["credits"] => string(1) "3"
}
[4]=> array(2)
{
["sub_code"] => string(6) "GE6151"
["credits"] => string(1) "3"
}
[5]=> array(2)
{
["sub_code"] => string(6) "GE6152"
["credits"] => string(1) "4"
}
[6]=> array(2)
{
["sub_code"] => string(6) "GE6161"
["credits"] => string(1) "2"
}
[7]=> array(2)
{
["sub_code"] => string(6) "GE6162"
["credits"] => string(1) "2"
}
[8]=> array(2)
{
["sub_code"] => string(6) "GE6163"
["credits"] => string(1) "1"
}
}
Like below.
array(9) {
["HS6151"] => string(1) "4"
["MA6151"] => string(1) "4"
["PH6151"] => string(1) "3"
["CY6151"] => string(1) "3"
["GE6151"] => string(1) "3"
["GE6152"] => string(1) "4"
["GE6161"] => string(1) "2"
["GE6162"] => string(1) "2"
["GE6163"] => string(1) "1"
}
You can use array_column like so:
$array = [
[
'sub_code' => 'HS6151',
'credits' => '4',
],
[
'sub_code' => 'MA6151',
'credits' => '4',
],
];
$result = array_column($array, 'credits', 'sub_code');
print_r($result);
Results in:
Array
(
[HS6151] => 4
[MA6151] => 4
)
Sounds pretty straight forward:
<?php
$input = [
[
"sub_code" => "HS6151",
"credits" => "4"
], [
"sub_code" => "MA6151",
"credits" => "4"
], [
"sub_code" => "PH6151",
"credits" => "3"
], [
"sub_code" => "CY6151",
"credits" => "3"
], [
"sub_code" => "GE6151",
"credits" => "3"
], [
"sub_code" => "GE6152",
"credits" => "4"
], [
"sub_code" => "GE6161",
"credits" => "2"
], [
"sub_code" => "GE6162",
"credits" => "2"
], [
"sub_code" => "GE6163",
"credits" => "1"
]
];
$output = [];
array_walk($input, function($entry) use (&$output) {
$output[$entry["sub_code"]] = $entry["credits"];
});
print_r($output);
The output obviously is:
Array
(
[HS6151] => 4
[MA6151] => 4
[PH6151] => 3
[CY6151] => 3
[GE6151] => 3
[GE6152] => 4
[GE6161] => 2
[GE6162] => 2
[GE6163] => 1
)
I have array mentioned below, I will have value always multiple of 3.
$xyz = [
["name" => "abc"],
["name" => "snds"],
["name" => ""),
["number"=> "452"],
["number" => "845120"],
["number" => "84514513200"],
["email" => "ddddf"],
["email" => "dkskns"],
["email" => "kjnksdnkds"]
];
but this is not the proper format for me to perform further operations, so I want this array like mentioned below.
$abc = [
[
"name" => "abc",
"number" => '452',
"email" => "ddddf"
],
[
"name" => "snds",
"number" => "845120",
"email" => "dkskns"
],
[
"name" => "",
"number" => "84514513200",
"email" => "kjnksdnkds"
]
];
note: the array length is dynamic but it will always be multiple of 3
One possibility could be to use the modulo % operator.
In the foreach the value is an array and you could use array_keys to get the key and reset to get the value of the first array element.
$result = [];
$count = 0;
foreach ($xyz as $value) {
if ($count%3 === 0) {
$count = 0;
}
$result[$count][array_keys($value)[0]] = reset($value);
$count++;
}
Demo
That will give you:
array(3) {
[0]=>
array(3) {
["name"]=>
string(3) "abc"
["number"]=>
string(3) "452"
["email"]=>
string(5) "ddddf"
}
[1]=>
array(3) {
["name"]=>
string(4) "snds"
["number"]=>
string(6) "845120"
["email"]=>
string(6) "dkskns"
}
[2]=>
array(3) {
["name"]=>
string(0) ""
["number"]=>
string(11) "84514513200"
["email"]=>
string(10) "kjnksdnkds"
}
}
This will do:
$result = array_map('array_merge', ...array_chunk($xyz, count($xyz) / 3));
I am working on Mageneto 2.1.7 to create custom modules creating to create a rest API, I am trying to list the products with its available colors and sizes but I can't get it, I am using the following code
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection')->addAttributeToSelect([
'name',
'price',
'image',
'color',
'size',
]);
$output = '';
foreach ($productCollection as $product) {
$output .= \Zend_Debug::dump($product->debug(), null, false);
}
var_dump($output);
I got all the data but not getting the sizes and colors as I need to list it.
the following is the output I got
<pre>
array(11) {
["entity_id"] => string(1) "1"
["attribute_set_id"] => string(1) "4"
["type_id"] => string(12) "configurable"
["sku"] => string(2) "p1"
["has_options"] => string(1) "1"
["required_options"] => string(1) "1"
["created_at"] => string(19) "2017-07-05 08:13:31"
["updated_at"] => string(19) "2017-07-10 11:25:27"
["name"] => string(2) "p1"
["image"] => string(125) "/f/a/fashion-womens-clothing-to-give-you-fresh-ideas-in-creating-your-own-fashion-clothes-so-it-looks-more-astounding-3_1.jpg"
["price"] => string(6) "1.0000"
}
array(11) {
["entity_id"] => string(1) "2"
["attribute_set_id"] => string(1) "4"
["type_id"] => string(6) "simple"
["sku"] => string(2) "p2"
["has_options"] => string(1) "0"
["required_options"] => string(1) "0"
["created_at"] => string(19) "2017-07-06 11:54:37"
["updated_at"] => string(19) "2017-07-06 11:54:37"
["name"] => string(2) "p2"
["image"] => string(38) "/i/l/il201702160307496471t_300x400.jpg"
["price"] => string(6) "1.0000"
}
array(12) {
["entity_id"] => string(1) "3"
["attribute_set_id"] => string(1) "4"
["type_id"] => string(7) "virtual"
["sku"] => string(9) "p1-purpel"
["has_options"] => string(1) "0"
["required_options"] => string(1) "0"
["created_at"] => string(19) "2017-07-09 06:49:56"
["updated_at"] => string(19) "2017-07-09 06:49:56"
["name"] => string(9) "p1-purpel"
["image"] => string(45) "/0/0/0009fb829de5ced795d1ed509e58866b_1_2.jpg"
["price"] => string(6) "1.0000"
["color"] => string(1) "4"
}
</pre>
I have an array like this. I want remove elements with duplicate id and get sum of the count
array(3) {
[0]=>
array(3) {
["Id"]=>
string(1) "1"
["Name"]=>
string(1) "a"
["Count"]=>
string(1) "2"
}
[1]=>
array(3) {
["Id"]=>
string(1) "2"
["Name"]=>
string(1) "b"
["Count"]=>
string(1) "1"
}[2]=>
array(3) {
["Id"]=>
string(1) "1"
["Name"]=>
string(1) "a"
["Count"]=>
string(1) "1"
}
}
and I need to remove elements with duplicate id and get sum of the count as shown below
array(2) {
[0]=>
array(3) {
["Id"]=>
string(1) "1"
["Name"]=>
string(1) "a"
["Count"]=>
string(1) "3"
}[1]=>
array(3) {
["Id"]=>
string(1) "2"
["Name"]=>
string(1) "b"
["Count"]=>
string(1) "1"
}
}
I have gone through many examples.. but couldn't find an answer..
Unfortunately there is no way around looping. Assuming that Name is the same for the same Id or that you don't care about the value of Name:
foreach($array as $value) {
if(!isset($result[$value['Id']])) {
$result[$value['Id']] = $value;
} else {
$result[$value['Id']]['Count'] += $value['Count'];
}
}
// re-index if needed
$result = array_values($result);
Loop the array and build result array using Id as key
If the key Id doesn't exist create it
If it does exist add Count to the current Count
just create a new array, loop through current, create if doesnt exist, and insert values (sum) into new one
what are you doing with duplicates names?
example below. hope it will help.
<?php
$tArr = array(
array(
"Id" => "1",
"Name" => "a",
"Count" => "2",
),
array(
"Id" => "2",
"Name" => "b",
"Count" => "1",
),
array(
"Id" => "1",
"Name" => "a",
"Count" => "1",
)
);
$rez = array();
foreach ($tArr as $key => $element) {
if (empty($rez[$element["Id"]])) {
$rez[$element["Id"]] = $element;
} else {
$rez[$element["Id"]]["Count"] += $element["Count"];
}
}
var_dump($rez);
/** array (size=2)
1 =>
array (size=3)
'Id' => string '1' (length=1)
'Name' => string 'a' (length=1)
'Count' => int 3
2 =>
array (size=3)
'Id' => string '2' (length=1)
'Name' => string 'b' (length=1)
'Count' => string '1' (length=1)**/
Try this
$result = array_diff_assoc($arr, array_unique($arr));
print_r($result);
This question already has answers here:
Recursive function to generate multidimensional array from database result
(5 answers)
Closed 9 years ago.
I have an array that looks something like:
$someArray = array(
array(
"id"=> 1,
"name"=> "somename1",
"parent"=> 0
),
array(
"id"=> 53,
"name"=> "somename2",
"parent"=> 1
),
array(
"id"=> 921,
"name"=> "somename3",
"parent"=> 53,
)
.
.
.
.
.
.
);
Of course, there are more cells in the array this is just a small portion.
I am trying to turn this array to something like:
$someArray = array(
array(
"id"=> 1,
"name"=> "somename1",
"parent"=> 0,
"children" => array(
array(
"id"=> 53,
"name"=> "somename2",
"parent"=> 1,
"children" => array(
array(
"id"=> 921,
"name"=> "somename3",
"parent"=> 53,
"children" => array(
)
)
)
)
)
)
.
.
.
.
.
.
);
/*
Gets reversed array,
Returns multidimensional tree array.
*/
function buildTree($parts) {
if (count($parts) == 1) {
return $parts[0];
}
$last_item = array_pop($parts);
$last_item[] = buildTree($parts);
return $last_item;
}
Testing:
$parts = array(
array('1','2','3',5),
array('3','8','3',1),
array('1', 5,'2','3'),
array('D','2','3',5),
array('A','2','3',5)
);
var_dump(buildTree(array_reverse($parts)));
Output:
array(5) { [0]=> string(1) "1" [1]=> string(1) "2" [2]=> string(1) "3" [3]=> int(5) [4]=> array(5) {
[0]=> string(1) "3" [1]=> string(1) "8" [2]=> string(1) "3" [3]=> int(1) [4]=> array(5) {
[0]=> string(1) "1" [1]=> int(5) [2]=> string(1) "2" [3]=> string(1) "3" [4]=> array(5) {
[0]=> string(1) "D" [1]=> string(1) "2" [2]=> string(1) "3" [3]=> int(5) [4]=> array(4) {
[0]=> string(1) "A" [1]=> string(1) "2" [2]=> string(1) "3" [3]=> int(5)
} } } } }