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
)
Related
Input Array : Input array is like below and level in it can be dynamic up to any level,
$array_input = [
0 => ["level" => "L1", "points" => 1000],
1 => ["level" => "L1", "points" => 5000],
2 => ["level" =>"L2 ", "points" => 3000],
3 => ["level" => "L3", "points" => 4000],
4 => ["level" => "L3", "points" => 6000],
5 => ["level" => "L2", "points" => 4000],
6 => ["level" => "L2", "points" => 5000],
7 => ["level" => "L2", "points" => 5000],
8 => ["level" => "L1", "points" => 6000],
9 => ["level" => "L1", "points" => 2000]
];
OUTPUT ARRAY: Output array should be like below, I tried using 2 foreach loop and accessing previous Level and compare it to next Level. But it is creating problem where Level is more than 2 times.
$array_output = [
0 => [
0 => ["level" => "L1", "points" => 1000],
1 => ["level" => "L1", "points" => 5000]
],
1 => [
0 => ["level" => "L2", "points" => 3000]
],
2 => [
0 => ["level" => "L3", "points" => 4000],
1 => ["level" => "L3", "points" => 6000]
],
3 => [
0 => ["level" => "L2", "points" => 4000],
1 => ["level" => "L2", "points" => 5000],
2 => ["level" => "L2", "points" => 5000]
],
4 => [
0 => ["level" => "L1", "points" => 6000],
1 => ["level" => "L1", "points" => 2000]
]
];
This is the code i tried but it's not giving result what i want:
$opArr = [];
$i=1;
$justArr = [];
$pre = $array_input[0]['level'];
$firstArr = $array_input[0];
for($i; $i<count($array_input); $i++){ // 0
foreach($array_input[$i] as $key => $value){
if($key == 'level'){
$level = $value;
}
}
if($pre == $level ){
$opArr[] = $firstArr;
$opArr[] = $array_input[$i];
}
$pre = $array_input[$i]['level'];
$firstArr = $array_input[$i];
}
echo "<pre>";
echo "OpArr:";
print_r($opArr);
echo "<pre>";
You may give this a try. See comments for step-by-step explanation. Outputs:
array(5) {
[0]=>
array(2) {
[0]=>
array(2) {
["level"]=>
string(2) "L1"
["points"]=>
int(1000)
}
[1]=>
array(2) {
["level"]=>
string(2) "L1"
["points"]=>
int(5000)
}
}
[1]=>
array(1) {
[0]=>
array(2) {
["level"]=>
string(2) "L2"
["points"]=>
int(3000)
}
}
[2]=>
array(2) {
[0]=>
array(2) {
["level"]=>
string(2) "L3"
["points"]=>
int(4000)
}
[1]=>
array(2) {
["level"]=>
string(2) "L3"
["points"]=>
int(6000)
}
}
[3]=>
array(3) {
[0]=>
array(2) {
["level"]=>
string(2) "L2"
["points"]=>
int(4000)
}
[1]=>
array(2) {
["level"]=>
string(2) "L2"
["points"]=>
int(5000)
}
[2]=>
array(2) {
["level"]=>
string(2) "L2"
["points"]=>
int(5000)
}
}
[4]=>
array(2) {
[0]=>
array(2) {
["level"]=>
string(2) "L1"
["points"]=>
int(6000)
}
[1]=>
array(2) {
["level"]=>
string(2) "L1"
["points"]=>
int(2000)
}
}
}
Code:
<?php
// Your input array.
$array_input = [
0 => ["level" => "L1", "points" => 1000],
1 => ["level" => "L1", "points" => 5000],
2 => ["level" =>"L2", "points" => 3000],
3 => ["level" => "L3", "points" => 4000],
4 => ["level" => "L3", "points" => 6000],
5 => ["level" => "L2", "points" => 4000],
6 => ["level" => "L2", "points" => 5000],
7 => ["level" => "L2", "points" => 5000],
8 => ["level" => "L1", "points" => 6000],
9 => ["level" => "L1", "points" => 2000]
];
$output = [];
// Keep track of last group key seen so we can
// create a new child array on every boundary.
$lastGroupKey = $array_input[0]['level'];
// Output array.
$grouped = [];
// Loop over every input element.
foreach ($array_input as $input)
{
// Grouping on 'level' value, which we'll use as a 'key'.
$groupKey = $input['level'];
// If current key is not the last one seen, then this is a
// boundary. Push the current group onto the output
// and start afresh.
if ($groupKey !== $lastGroupKey)
{
$output[] = $grouped;
$grouped = [];
}
// Record each row.
$grouped[] = $input;
// Update last key seen.
$lastGroupKey = $groupKey;
}
// The end of the array is the final boundary.
// Record trailing group.
$output[] = $grouped;
var_dump($output);
/*
array(5) {
[0]=>
array(2) {
[0]=>
array(2) {
["level"]=>
string(2) "L1"
["points"]=>
int(1000)
}
[1]=>
array(2) {
["level"]=>
string(2) "L1"
["points"]=>
int(5000)
}
}
[1]=>
array(1) {
[0]=>
array(2) {
["level"]=>
string(2) "L2"
["points"]=>
int(3000)
}
}
[2]=>
array(2) {
[0]=>
array(2) {
["level"]=>
string(2) "L3"
["points"]=>
int(4000)
}
[1]=>
array(2) {
["level"]=>
string(2) "L3"
["points"]=>
int(6000)
}
}
[3]=>
array(3) {
[0]=>
array(2) {
["level"]=>
string(2) "L2"
["points"]=>
int(4000)
}
[1]=>
array(2) {
["level"]=>
string(2) "L2"
["points"]=>
int(5000)
}
[2]=>
array(2) {
["level"]=>
string(2) "L2"
["points"]=>
int(5000)
}
}
[4]=>
array(2) {
[0]=>
array(2) {
["level"]=>
string(2) "L1"
["points"]=>
int(6000)
}
[1]=>
array(2) {
["level"]=>
string(2) "L1"
["points"]=>
int(2000)
}
}
}
*/
in cycle: if element first or next element exists and have different "level" then create new element(sub-array) in result array
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
I need help in order to manipulate a multidimensional array during PHP foreach loop. I have the array below:
$sports = [
[
"id" => "soccer",
"name" => "Football",
"categories" => [
[
"id" => "s1",
"name" => "category 1",
"sportID" => "soccer"
],
[
"id" => "s2",
"name" => "category 2",
"sportID" => "soccer"
],
],
"img" => "/test.png"
],
[
"id" => "tennis",
"name" => "Tennis",
"categories" => [
[
"id" => "t1",
"name" => "category 1",
"sportID" => "tennis"
],
[ "id" => "t2",
"name" => "category 2",
"sportID" => "tennis"
],
],
"img" => "/test.png"
],
];
I have the below foreach in order to take all the sports with the corresponding categories for every sport,
foreach($sports as $s){
$categories = $s['categories'];
foreach($categories as $c){
$cats[] = [
"id" => $c['id'],
"name" => $c['name'],
"sportID" => $s['id'],
];
}
$allCategories[] = $cats;
$data[] = [
"id" => $s['id'],
"name" => $s['name'],
"categories" => $cats,
"img" => $s['img'],
];
$output[] = $data;
}
but the output is not what I expected; instead, I am getting repeated results like the one below:
array(2) {
[0]=>
array(1) {
[0]=>
array(4) {
["id"]=>
string(8) "soccer"
["name"]=>
string(8) "Football"
["categories"]=>
array(2) {
[0]=>
array(3) {
["id"]=>
string(2) "s1"
["name"]=>
string(10) "category 1"
["sportID"]=>
string(8) "soccer"
}
[1]=>
array(3) {
["id"]=>
string(2) "s2"
["name"]=>
string(10) "category 2"
["sportID"]=>
string(8) "soccer"
}
}
["img"]=>
string(9) "/test.png"
}
}
[1]=>
array(2) {
[0]=>
array(4) {
["id"]=>
string(8) "soccer"
["name"]=>
string(8) "Football"
["categories"]=>
array(2) {
[0]=>
array(3) {
["id"]=>
string(2) "s1"
["name"]=>
string(10) "category 1"
["sportID"]=>
string(8) "soccer"
}
[1]=>
array(3) {
["id"]=>
string(2) "s2"
["name"]=>
string(10) "category 2"
["sportID"]=>
string(8) "soccer"
}
}
["img"]=>
string(9) "/test.png"
}
[1]=>
array(4) {
["id"]=>
string(10) "tennis"
["name"]=>
string(8) "Tennis"
["categories"]=>
array(4) {
[0]=>
array(3) {
["id"]=>
string(2) "s-1"
["name"]=>
string(10) "category 1"
["sportID"]=>
string(8) "soccer"
}
[1]=>
array(3) {
["id"]=>
string(2) "s2"
["name"]=>
string(10) "category 2"
["sportID"]=>
string(8) "soccer"
}
[2]=>
array(3) {
["id"]=>
string(2) "t1"
["name"]=>
string(10) "category 1"
["sportID"]=>
string(10) "tennis"
}
[3]=>
array(3) {
["id"]=>
string(2) "t2"
["name"]=>
string(10) "category 2"
["sportID"]=>
string(10) "tennis"
}
}
["img"]=>
string(9) "/test.png"
}
}
}
As you can imagine this is not the correct output, as in the categories of the tennis you can see the categories of the soccer also.
How can I correct my foreach loop in order to get the correct output?
You forget to reset $cats and $data arrays.
<?php
$sports = [
[
"id" => "soccer",
"name" => "Football",
"categories" => [
[
"id" => "s1",
"name" => "category 1",
"sportID" => "soccer"
],
[
"id" => "s2",
"name" => "category 2",
"sportID" => "soccer"
],
],
"img" => "/test.png"
],
[
"id" => "tennis",
"name" => "Tennis",
"categories" => [
[
"id" => "t1",
"name" => "category 1",
"sportID" => "tennis"
],
[ "id" => "t2",
"name" => "category 2",
"sportID" => "tennis"
],
],
"img" => "/test.png"
],
];
foreach($sports as $s){
$categories = $s['categories'];
# before inner loop we need to reset both arrays
$cats = [];
$data = [];
foreach($categories as $c){
$cats[] = [
"id" => $c['id'],
"name" => $c['name'],
"sportID" => $s['id'],
];
}
$allCategories[] = $cats;
$data[] = [
"id" => $s['id'],
"name" => $s['name'],
"categories" => $cats,
"img" => $s['img'],
];
$output[] = $data;
}
echo '<PRE>';
print_r($output);
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 got an array like this:
array
'list_10' =>
array
row_0 =>
array
'Id' => string '118579'
'Status' => string '3'
row_1 =>
array
'Id' => string '117662'
'Status' => string '2'
row_2 =>
array
'Id' => string '117662'
'Status' => string '2'
'list_11' =>
array
row_0 =>
array
'Id' => string '112564'
'Status' => string '2'
row_1 =>
array
'Id' => string '153622'
'Status' => string '3'
row_2 =>
array
'Id' => string '112832'
'Status' => string '1'
i want to "natsort" the first key "list_XX", making it start with 0,1,2,.. instead of 10,11,12,13,0,1,2,3,...
i played around with array_multisort but i cant seem to
set the right params to make it do what i want, if its even capable of doing this.
any advice?
Assuming your array is something like this:
$array = [
'list_11' =>
[
'row_0' =>
[
'Id' => '118579',
'Status' => '3'
],
'row_1' =>
[
'Id' => '117662',
'Status' => '2'
],
'row_2' =>
[
'Id' => '117662',
'Status' => '2'
]
],
'list_10' =>
[
'row_0' =>
[
'Id' => '112564',
'Status' => '2'
],
'row_1' =>
[
'Id' => '153622',
'Status' => '3'
],
'row_2' =>
[
'Id' => '112832',
'Status' => '1'
]
],
'list_1' =>
[
'row_0' =>
[
'Id' => '32323232',
'Status' => '3'
],
'row_1' =>
[
'Id' => '2353333',
'Status' => '2'
],
'row_2' =>
[
'Id' => '117662',
'Status' => '2'
]
]
];
Using array_multisort :
$sort = [];
foreach($array as $el=>$val){
$sort[] = $el;
}
array_multisort($array,SORT_NUMERIC,$sort,SORT_NATURAL);
var_dump($array);
Will print :
array(3) {
["list_1"]=>
array(3) {
["row_0"]=>
array(2) {
["Id"]=>
string(8) "32323232"
["Status"]=>
string(1) "3"
}
["row_1"]=>
array(2) {
["Id"]=>
string(7) "2353333"
["Status"]=>
string(1) "2"
}
["row_2"]=>
array(2) {
["Id"]=>
string(6) "117662"
["Status"]=>
string(1) "2"
}
}
["list_10"]=>
array(3) {
["row_0"]=>
array(2) {
["Id"]=>
string(6) "112564"
["Status"]=>
string(1) "2"
}
["row_1"]=>
array(2) {
["Id"]=>
string(6) "153622"
["Status"]=>
string(1) "3"
}
["row_2"]=>
array(2) {
["Id"]=>
string(6) "112832"
["Status"]=>
string(1) "1"
}
}
["list_11"]=>
array(3) {
["row_0"]=>
array(2) {
["Id"]=>
string(6) "118579"
["Status"]=>
string(1) "3"
}
["row_1"]=>
array(2) {
["Id"]=>
string(6) "117662"
["Status"]=>
string(1) "2"
}
["row_2"]=>
array(2) {
["Id"]=>
string(6) "117662"
["Status"]=>
string(1) "2"
}
}
}