Array Item Referring to Another Sibling Element - php

Can i do this in PHP, or any others way around ? I want to refer to another element in the array.
$config = array(
'factory-code' => array(
'01', '02'
),
'commodity-filter' => array(
'factory' => array(
'steel' => array( $this->factory-code ),
),
'branch' => array(
'steel' => array( $this->factory-code, '09' ),
)
)
);

You'll have to create the factory-code in a separate array
$factory_code => array('01', '02');
$config = array(
'factory-code' => $factory_code,
'commodity-filter' => array(
'factory' => array(
'steel' => array( $factory_code ),
),
'branch' => array(
'steel' => array( $factory_code, '09' ),
)
)
);

Related

php use array_slice in nested array

I have an array like this
$data = array(
'2021-08-31' => array( 'country_id' => array( '191' => array( '20098' ) ) ),
'2021-08-18' => array( 'country_id' => array( '214' => array( '14876' ) ) ),
'2021-07-12' => array( 'country_id' => array( '103' => array( '19709' ) ) ),
'2021-07-07' => array( 'country_id' => array( '206' => array( '9568' ) ) ),
'2021-06-28' => array( 'country_id' => array( '246' => array( '19561' ), '132' => array( '19277' ), '1' => array( '2816' ), '83' => array( '19530' ), '21' => array('3340'), '202' => array( '14136' ), '103' => array( '17006' ) ) ),
'2021-06-25' => array( 'country_id' => array( '99' => array( '14561' ), '102' => array( '26789' ), '21' => array( '7690' ), '21' => array( '242325' ), '11' => array('436432'), '999' => array( '43734' ), '768' => array( '34437' ) ) ),
);
Here I want to apply array_slice only for country_id element.
So lets say if I want to show first 6 elements of country id then it should something like this
$data = array(
'2021-08-31' => array( 'country_id' => array( '191' => array( '20098' ) ) ),
'2021-08-18' => array( 'country_id' => array( '214' => array( '14876' ) ) ),
'2021-07-12' => array( 'country_id' => array( '103' => array( '19709' ) ) ),
'2021-07-07' => array( 'country_id' => array( '206' => array( '9568' ) ) ),
'2021-06-28' => array( 'country_id' => array( '246' => array( '19561' ), '132' => array( '19277' ) ) ),
);
If I want to show elements from 3 to 9 then it should show something like this
$data = array(
'2021-07-12' => array( 'country_id' => array( '103' => array( '19709' ) ) ),
'2021-07-07' => array( 'country_id' => array( '206' => array( '9568' ) ) ),
'2021-06-28' => array( 'country_id' => array( '246' => array( '19561' ), '132' => array( '19277' ), '1' => array( '2816' ), '83' => array( '19530' ), '21' => array('3340') ) ),
);
So can someone tell me how to achieve this without changing key value pair of above array.
Any help and suggestions would be really appreciable.
Following logic might help you on your way. Just set the slice you'd like to extract with $start and $end and the result is stored in array $store:
// set your slice with start and end
$start = 3;
$end = 9;
$count = 0;
$store = []; // the result store
foreach($data as $date => $record) {
foreach($record['country_id'] as $k => $countryIds) {
foreach($countryIds as $id) {
$count++;
if($count >= $start && $count <= $end) $store[$date]['country_id'][$k] = $countryIds;
}
}
}
demo

How to merge multiple dimension array cakephp with two key

I just using function query() of cakePhp. The query will return array something like :
array(
(int) 0 => array(
'cate' => array(
'date' => '2016-12-05',
),
'cate_detail' => array(
'rel_data_category' => '11'
),
'cate_item' => array(
'price' => '150.000'
),
'item' => array(
'code' => '1'
)
),
(int) 1 => array(
'cate' => array(
'date' => '2016-12-05',
),
'cate_detail' => array(
'rel_data_category' => '10'
),
'cate_item' => array(
'price' => '250.000'
),
'item' => array(
'code' => '1'
)
),
(int) 2 => array(
'cate' => array(
'date' => '2016-12-06',
),
'cate_detail' => array(
'rel_data_category' => '10'
),
'cate_item' => array(
'price' => '250.000'
),
'item' => array(
'code' => '2'
)
),
(int) 3 => array(
'cate' => array(
'date' => '2016-12-06',
),
'cate_detail' => array(
'rel_data_category' => '10'
),
'cate_item' => array(
'price' => '250.000'
),
'item' => array(
'code' => '1'
)
)
)
Now, I want to check if array have the same cate.date and item.code will merge array (in this case is elements 0,1 of my array). Output something like :
array(
(int) 0 => array(
'cate' => array(
'date' => '2016-12-05',
),
'cate_detail' => array(
(int) 0 => array (
'rel_data_category' => '11',
'price' => '150.000'
),
(int) 1 => array(
'rel_data_category' => '10',
'price' => '250.000'
)
),
'item' => array(
'code' => '1'
)
),
(int) 1 => array(
'cate' => array(
'date' => '2016-12-06',
),
'cate_detail' => array(
(int) 0 => array (
'rel_data_category' => '10'
'price' => '250.000'
)
),
'item' => array(
'code' => '2'
)
),
(int) 2 => array(
'cate' => array(
'date' => '2016-12-06',
),
'cate_detail' => array(
(int) 0 => array (
'rel_data_category' => '10'
'price' => '250.000'
)
),
'item' => array(
'code' => '1'
)
)
)
Please help!
To do that
First declare a variable which later you will store new merge array data
$filter_data = array();
then loop your existing array
foreach($items as $item) {
// do somethind
then create a function which check if item which same date and code not yet exist in the new array
// checker
if (!checkExist($item['cate']['date'], $item['item']['code'])) {
Create a variable for temporary data that will append to the new array later
$data = array(
'cate' => $item['cate'],
'item' => $item['item']
);
Then create loop again which will append all index item cate_detail and cate_item who have the same
date and code from the parent loop
}
}
So your code would be like this
$items = array(
(int) 0 => array(
'cate' => array(
'date' => '2016-12-05',
),
'cate_detail' => array(
'rel_data_category' => '11'
),
'cate_item' => array(
'price' => '150.000'
),
'item' => array(
'code' => '1'
)
),
(int) 1 => array(
'cate' => array(
'date' => '2016-12-05',
),
'cate_detail' => array(
'rel_data_category' => '10'
),
'cate_item' => array(
'price' => '250.000'
),
'item' => array(
'code' => '1'
)
),
(int) 2 => array(
'cate' => array(
'date' => '2016-12-06',
),
'cate_detail' => array(
'rel_data_category' => '10'
),
'cate_item' => array(
'price' => '250.000'
),
'item' => array(
'code' => '2'
)
),
(int) 3 => array(
'cate' => array(
'date' => '2016-12-06',
),
'cate_detail' => array(
'rel_data_category' => '10'
),
'cate_item' => array(
'price' => '250.000'
),
'item' => array(
'code' => '1'
)
)
);
$filter_data = array();
foreach($items as $item) {
if (!checkExist($item['cate']['date'], $item['item']['code'])) {
$data = array(
'cate' => $item['cate'],
'item' => $item['item']
);
foreach($items as $detail) {
if ($detail['cate']['date'] == $item['cate']['date'] &&
$detail['item']['code'] == $item['item']['code']) {
$data['cate_detail'][] = array(
'rel_data_category' => $detail['cate_detail']['rel_data_category'],
'price' => $detail['cate_item']['price']
);
}
}
$filter_data[] = $data;
}
}
function checkExist($date, $code){
global $filter_data;
foreach($filter_data as $data) {
if ($data['cate']['date'] == $date &&
$data['item']['code'] == $code) {
return true;
}
}
return false;
}
pr($filter_data);
Demo
OR
$items = array(
(int) 0 => array(
'cate' => array(
'date' => '2016-12-05',
),
'cate_detail' => array(
'rel_data_category' => '11'
),
'cate_item' => array(
'price' => '150.000'
),
'item' => array(
'code' => '1'
)
),
(int) 1 => array(
'cate' => array(
'date' => '2016-12-05',
),
'cate_detail' => array(
'rel_data_category' => '10'
),
'cate_item' => array(
'price' => '250.000'
),
'item' => array(
'code' => '1'
)
),
(int) 2 => array(
'cate' => array(
'date' => '2016-12-06',
),
'cate_detail' => array(
'rel_data_category' => '10'
),
'cate_item' => array(
'price' => '250.000'
),
'item' => array(
'code' => '2'
)
),
(int) 3 => array(
'cate' => array(
'date' => '2016-12-06',
),
'cate_detail' => array(
'rel_data_category' => '10'
),
'cate_item' => array(
'price' => '250.000'
),
'item' => array(
'code' => '1'
)
)
);
$filter_data = array();
$exists = array();
foreach($items as $item) {
$index_key = $item['cate']['date'].'-'.$item['item']['code'];
if (!isset($exists[$index_key])) {
$exists[$index_key] = 1;
$data = array(
'cate' => $item['cate'],
'item' => $item['item'],
'cate_detail' => getItems($item['cate']['date'], $item['item']['code'])
);
$filter_data[] = $data;
}
}
function getItems($date, $code) {
global $items;
$data = array();
foreach($items as $detail) {
if ($detail['cate']['date'] == $date && $detail['item']['code'] == $code) {
$data[] = array(
'rel_data_category' => $detail['cate_detail']['rel_data_category'],
'price' => $detail['cate_item']['price']
);
}
}
return $data;
}
pr($filter_data);
Demo

How to Group a multidimensional array

How can I group the array based Name Menu Category and Name Menu ? Is there any native php functions are available to do this? I 've searched on google but not yet well understood
(int) 0 => array(
'MenuCategory' => array(
'name' => 'Products'
),
'Menu' => array(
'name' => 'A',
'url' => 'a'
),
'Sub' => array(
'name' => 'A1',
'url' => 'a1'
)
),
(int) 1 => array(
'MenuCategory' => array(
'name' => 'Products'
),
'Menu' => array(
'name' => 'A',
'url' => 'a'
),
'Sub' => array(
'name' => 'A2',
'url' => 'a2'
)
),
(int) 2 => array(
'MenuCategory' => array(
'name' => 'Products'
),
'Menu' => array(
'name' => 'B',
'url' => 'b'
),
'Sub' => array(
'name' => null,
'url' => null
)
),
(int) 3 => array(
'MenuCategory' => array(
'name' => 'Data'
),
'Menu' => array(
'name' => 'A',
'url' => 'a'
),
'Sub' => array(
'name' => null,
'url' => null
)
),
(int) 4 => array(
'MenuCategory' => array(
'name' => 'Data'
),
'Menu' => array(
'name' => 'B',
'url' => 'b'
),
'Sub' => array(
'name' => 'B1',
'url' => 'b1'
)
),
(int) 5 => array(
'MenuCategory' => array(
'name' => 'Data'
),
'Menu' => array(
'name' => 'C',
'url' => 'c'
),
'Sub' => array(
'name' => null,
'url' => null
)
),
(int) 6 => array(
'MenuCategory' => array(
'name' => 'Report'
),
'Menu' => array(
'name' => 'A',
'url' => 'a'
),
'Sub' => array(
'name' => null,
'url' => null
)
),
(int) 7 => array(
'MenuCategory' => array(
'name' => 'Report'
),
'Menu' => array(
'name' => 'B',
'url' => 'b'
),
'Sub' => array(
'name' => null,
'url' => null
)
),
(int) 8 => array(
'MenuCategory' => array(
'name' => 'Report'
),
'Menu' => array(
'name' => 'C',
'url' => 'c'
),
'Sub' => array(
'name' => null,
'url' => null
)
),
This is a result that will be issued later
Products
A
A1
A2
B
Data
A
B
B1
C
Report
A
B
C
if I assume your array variable name is $menu, try this code.
$new = array();
array_map(function($a) use(&$new){
if($a['Sub']['name'])
$new[$a['MenuCategory']['name']][$a['Menu']['name']][$a['Sub']['name']] = $a['Sub']['name'];
else
$new[$a['MenuCategory']['name']][$a['Menu']['name']] = $a['Menu']['name'];
}, $menu);
print_r($new);
and will output
Array
(
[Products] => Array
(
[A] => Array
(
[A1] => A1
[A2] => A2
)
[B] => B
)
[Data] => Array
(
[A] => A
[B] => Array
(
[B1] => B1
)
[C] => C
)
[Report] => Array
(
[A] => A
[B] => B
[C] => C
)
)
Hope this help.
Group them by level first
use foreach to sort them example
foreach ($input_arr as $key => &$entry) {
$level_arr[$entry['level']][$key] = $entry;
}

While in a Array

hope someone can help me.
I try to write a Import Script for Articles in my Shopsystem.
For this edit the basic Code from the Wiki of the Shopsystem.
Here look
$updateArticle = array(
'configuratorSet' => array(
'groups' => array(
array(
'name' => 'Farben',
'options' => $farbenarray
),
array(
'name' => 'Größe',
'options' => $sizearray
),
)
),
'taxId' => 1,
'variants' => array(
array(
'isMain' => true,
'number' => 'turn',
'inStock' => 15,
'additionaltext' => 'L / Black',
'configuratorOptions' => array(
array('group' => 'Size', 'option' => 'L'),
array('group' => 'Color', 'option' => 'Black'),
),
'prices' => array(
array(
'customerGroupKey' => 'EK',
'price' => 1999,
),
)
),
array(
'isMain' => false,
'number' => 'turn.1',
'inStock' => 15,
'additionnaltext' => 'S / Black',
'configuratorOptions' => array(
array('group' => 'Size', 'option' => 'S'),
array('group' => 'Color', 'option' => 'Black'),
),
'prices' => array(
array(
'customerGroupKey' => 'EK',
'price' => 999,
),
)
),
array(
'isMain' => false,
'number' => 'turn.2',
'inStock' => 15,
'additionnaltext' => 'S / Red',
'configuratorOptions' => array(
array('group' => 'Size', 'option' => 'S'),
array('group' => 'Color', 'option' => 'Red'),
),
'prices' => array(
array(
'customerGroupKey' => 'EK',
'price' => 999,
),
)
),
array(
'isMain' => false,
'number' => 'turn.3',
'inStock' => 15,
'additionnaltext' => 'XL / Red',
'configuratorOptions' => array(
array('group' => 'Size', 'option' => 'XL'),
array('group' => 'Color', 'option' => 'Red'),
),
'prices' => array(
array(
'customerGroupKey' => 'EK',
'price' => 999,
),
)
)
)
);
Now i wan't to replace all the Variant Array with a While request.
Replace all like this
array(
'isMain' => true,
'number' => 'turn',
'inStock' => 15,
'additionaltext' => 'L / Black',
'configuratorOptions' => array(
array('group' => 'Size', 'option' => 'L'),
array('group' => 'Color', 'option' => 'Black'),
),
'prices' => array(
array(
'customerGroupKey' => 'EK',
'price' => 1999,
),
)
),
With a like a request like that ( i know thats don't work but i hope you understand what i like to do)
if ($resultat = $db->query('SELECT * FROM cache_article ORDER by id WHERE artikelnummer = "'.$herstellernummer.'"')) {
while($daten = $resultat->fetch_object() ){
// WHILE THE ARRAY CODES
array(
'isMain' => true,
'number' => $daten->artikelnummer,
'inStock' => 1,
'additionaltext' => ''.$daten->size.' / '.$daten->color.'',
'configuratorOptions' => array(
array('group' => 'Size', 'option' => $daten->size),
array('group' => 'Color', 'option' => $daten->color),
),
'prices' => array(
array(
'customerGroupKey' => 'EK',
'price' => $daten->price,
),
)
),
// END THE ARRAY Codes
}
}
How i can do that correct so that works?
Please give me a Example for the request and how i can integrate that.
Sorry for my bad english! -.-
Thank You
I think i understand you.What you need is to store the generated arrays inside another, like this:
while(...
$arrayOfArrays[] = Array(...
That is explained here.
Basically $variable[] appends an element to the end of the array, but if the array doesn't exists then it creates it first.

PHP > Form a multi-dimensional array from a nested set model flat array

Okay, basically, I have formed a mySQL query which returns a data set all sorted perfectly of my data structure. If I was to declare it, it would look something like this:
$arr = array(
array( 'name' => 'Root', 'depth' => 0 ),
array( 'name' => 'Food', 'depth' => 0 ),
array( 'name' => 'Fruit', 'depth' => 1 ),
array( 'name' => 'Bannnanna', 'depth' => 2 ),
array( 'name' => 'Apple', 'depth' => 2 ),
array( 'name' => 'Bannnanna', 'depth' => 2 ),
array( 'name' => 'Meat', 'depth' => 1 ),
array( 'name' => 'Furniture', 'depth' => 0 )
);
What I want, is to feed this $arr into a method so that I can then get a multi-dimensional array back.
For instance, Fruit would be a child of Food. And Bananana would be a child of Fruit.
I need the keys of each 'child' to be 'pages'.
The array that I showed above can also return other information such as 'lft' and 'rgt'. I do not have 'parentId' set but I can change my model if its required, but I would prefer to stick with Nested Set Model.
something like this maybe
$arr = array(
array( 'name' => 'Root', 'depth' => 0 ),
array( 'name' => 'Food', 'depth' => 0 ),
array( 'name' => 'Fruit', 'depth' => 1 ),
array( 'name' => 'Bannnanna', 'depth' => 2 ),
array( 'name' => 'Apple', 'depth' => 2 ),
array( 'name' => 'Bannnanna', 'depth' => 2 ),
array( 'name' => 'Meat', 'depth' => 1 ),
array( 'name' => 'Furniture', 'depth' => 0 )
);
$p = array(array());
foreach($arr as $n => $a) {
$d = $a['depth'] + 1;
$p[$d - 1]['children'][] = &$arr[$n];
$p[$d] = &$arr[$n];
}
print_r($p[0]);
The following code assumes that the order and depth of your values are correct. Note that it removes the duplicates by using keys.
header('Content-Type: text/plain');
$arr = array(
array( 'name' => 'Root', 'depth' => 0 ),
array( 'name' => 'Food', 'depth' => 0 ),
array( 'name' => 'Fruit', 'depth' => 1 ),
array( 'name' => 'Bannnanna', 'depth' => 2 ),
array( 'name' => 'Apple', 'depth' => 2 ),
array( 'name' => 'Bannnanna', 'depth' => 2 ),
array( 'name' => 'Meat', 'depth' => 1 ),
array( 'name' => 'Furniture', 'depth' => 0 )
);
function process( &$arr, &$prev_sub = null, $cur_depth = 0 ) {
$cur_sub = array();
while( $line = current( $arr ) ) {
if( $line['depth'] < $cur_depth ) {
return $cur_sub;
} elseif( $line['depth'] > $cur_depth ) {
$prev_sub = process( $arr, $cur_sub, $cur_depth + 1 );
} else {
$cur_sub[$line['name']] = $line['name'];
$prev_sub =& $cur_sub[$line['name']];
next( $arr );
}
}
return $cur_sub;
}
$values = process( $arr );
print_r( $values );
Output:
Array
(
[Root] => Root
[Food] => Array
(
[Fruit] => Array
(
[Bannnanna] => Bannnanna
[Apple] => Apple
)
[Meat] => Meat
)
[Furniture] => Furniture
)

Categories