I'm trying to create a timeline with an apex chart.
I can't literally print the Json data.
$temp[$r["Durum"]]['data'][] = array(
'x' => $r['Basladı'],
'y' => array(
strtotime($r['Saat']),
strtotime($r['Bitis'])
)
);
output
{
"P": {
"data": [
{
"x": "Basladi",
"y": [
1602418875,
1602418884
]
},
{
"x": "Basladi",
"y": [
1602418887,
1602418902
]
},
]
}
}
output I want
{
{
"name" : "P",
"data": [
{
"x": "Basladi",
"y": [
1602418875,
1602418884
]
},
{
"x": "Basladi",
"y": [
1602418887,
1602418902
]
},
]
}
}
Here is a code example with some sample input that will work for you.
Sample data (in your case the data from your database):
$yourdata = array(
array(
'Durum' => 'p',
'Basladı' => 'Basladı',
'Saat' => '12.12.2020',
'Bitis' => '12.12.2020'
),
array(
'Durum' => 'p',
'Basladı' => 'Basladı',
'Saat' => '12.12.2020',
'Bitis' => '12.12.2020'
),
array(
'Durum' => 's',
'Basladı' => 'Basladı',
'Saat' => '12.12.2020',
'Bitis' => '12.12.2020'
)
);
Code snippet:
// create empty temp array for the result
$temp = [];
// loop through your data (only an example - replace with your data)
foreach($yourdata as $r) {
// defines a temp data array to add
$temp_data = array(
'x' => $r['Basladı'],
'y' => array(
strtotime($r['Saat']),
strtotime($r['Bitis'])
)
);
// check if there is already an entry with that name
if(($key = array_search($r["Durum"], array_column($temp, 'name'))) !== false) {
// if there is, only add the data to it
$temp[$key]['data'][] = $temp_data;
} else {
// otherwise push a new array with the name and data to temp
$temp[] = array(
'name' => $r["Durum"],
'data' => array($temp_data)
);
}
}
var_dump($temp); // process with the data
The content of the variable $temp:
Array
(
[0] => Array
(
[name] => p
[data] => Array
(
[0] => Array
(
[x] => Basladı
[y] => Array
(
[0] => 1607731200
[1] => 1607731200
)
)
[1] => Array
(
[x] => Basladı
[y] => Array
(
[0] => 1607731200
[1] => 1607731200
)
)
)
)
[1] => Array
(
[name] => s
[data] => Array
(
[0] => Array
(
[x] => Basladı
[y] => Array
(
[0] => 1607731200
[1] => 1607731200
)
)
)
)
)
Related
I have the following array, we'll call it $arr and I have prepared a sample array. I need to manipulate the path $arr['svg'] to have a specific key and value always at index 0 position. This is a sample data-set and depending on the data I'm working with the key's and values are not fixed, however the main point is to always have the title array (key and value) at the top of the svg array.
$arr = array("svg" =>
array(
0 => array("#style" => "overflow:visible", "#xlink:href" => "test.png"),
1 => array("g" => "", "#id" => "Layer_2"),
2 => array("g" => "", "#id" => "Layer_3"),
3 => array("title" => "test")
),
"#version" => 1.2,
"#baseProfile" => "tiny-ps",
"#id" => "Layer_1",
"#xmlns" => "http://www.w3.org/2000/svg"
);
I am trying to achieve two things under the array path $arr['svg']
If the array key title exists in $arr['svg'] and it is not in index 0 position
then move it to index 0 of $arr['svg'] and shift everything else
down.
If the array key title DOES NOT exist in $arr['svg'] then add it array('title' =>
'test') to index 0 position of $arr['svg'] and shift
everything else down.
The expected output of $arr will be like so:
Array
(
[svg] => Array
(
[0] => Array
(
[title] => test
)
[1] => Array
(
[#style] => overflow:visible;
[#xlink:href] => test.png
)
[2] => Array
(
[g] =>
[#id] => Layer_2
)
[3] => Array
(
[g] =>
[#id] => Layer_3
)
)
[#version] => 1.2
[#baseProfile] => tiny-ps
[#id] => Layer_1
[#xmlns] => http://www.w3.org/2000/svg
)
I am trying to use this function to achieve this but it seems this function only works from the root array position $arr, not within a specific path $arr['svg']. If it can be modified to work within a specific path that would hopefully solve the issue.
//source: https://gist.github.com/wpscholar/0deadce1bbfa4adb4e4c
function push_at_to_associative_array($array, $key, $new ){
$keys = array_keys( $array );
$index = array_search( $key, $keys, true );
$pos = false === $index ? count( $array ) : $index + 1;
$array = array_slice($array, 0, $pos, true) + $new + array_slice($array, $pos, count($array) - 1, true);
return $array;
}
Usage:
$title = array("title" => "test');
$arr = push_at_to_associative_array($arr, 'svg', $title);
process the svg array into a new one setting [0] to the default title if we later find a title, replace svg[0]['title'] with the found one, then finally replace the original svg part of the array with the new one.
$arr = [
"svg" =>
[
["#style" => "overflow:visible", "#xlink:href" => "test.png"],
["g" => "", "#id" => "Layer_2"],
["g" => "", "#id" => "Layer_3"],
["title" => "Fred"]
],
"#version" => 1.2,
"#baseProfile" => "tiny-ps",
"#id" => "Layer_1",
"#xmlns" => "http://www.w3.org/2000/svg"
];
function push_at_to_associative_array(&$arr)
{
$new_svg = [];
foreach ($arr['svg'] as $key => $svg){
if ( $key == 0){
$new_svg[] = ['title'=>'test'];
}
if ( !array_key_exists('title', $svg) ){
$new_svg[] = $svg;
} else {
# amend title
$new_svg[0]['title'] = $svg['title'];
}
}
$arr['svg'] = $new_svg;
}
push_at_to_associative_array($arr);
print_r($arr);
RESULTS
Array
(
[svg] => Array
(
[0] => Array
(
[title] => Fred
)
[1] => Array
(
[#style] => overflow:visible
[#xlink:href] => test.png
)
[2] => Array
(
[g] =>
[#id] => Layer_2
)
[3] => Array
(
[g] =>
[#id] => Layer_3
)
)
[#version] => 1.2
[#baseProfile] => tiny-ps
[#id] => Layer_1
[#xmlns] => http://www.w3.org/2000/svg
)
And if you run it without a title in the array
$arr = [
"svg" =>
[
["#style" => "overflow:visible", "#xlink:href" => "test.png"],
["g" => "", "#id" => "Layer_2"],
["g" => "", "#id" => "Layer_3"]
],
"#version" => 1.2,
"#baseProfile" => "tiny-ps",
"#id" => "Layer_1",
"#xmlns" => "http://www.w3.org/2000/svg"
];
function push_at_to_associative_array(&$arr)
{
$new_svg = [];
foreach ($arr['svg'] as $key => $svg){
if ( $key == 0){
$new_svg[] = ['title'=>'test'];
}
if ( !array_key_exists('title', $svg) ){
$new_svg[] = $svg;
} else {
# amend title
$new_svg[0]['title'] = $svg['title'];
}
}
$arr['svg'] = $new_svg;
}
push_at_to_associative_array($arr);
print_r($arr);
RESULT
Array
(
[svg] => Array
(
[0] => Array
(
[title] => test
)
[1] => Array
(
[#style] => overflow:visible
[#xlink:href] => test.png
)
[2] => Array
(
[g] =>
[#id] => Layer_2
)
[3] => Array
(
[g] =>
[#id] => Layer_3
)
)
[#version] => 1.2
[#baseProfile] => tiny-ps
[#id] => Layer_1
[#xmlns] => http://www.w3.org/2000/svg
)
An easy solution would be to do something like this:
function fix_array( $array ) {
$svg_title_index = array_key_first(
array_filter(
$array['svg'],
fn($item) => isset($item['title'])
)
);
if (! $svg_title_index) {
array_unshift($array['svg'], ['title' => 'test']);
} elseif($svg_title_index > 0) {
$value = $array['svg'][$svg_title_index];
unset($array['svg'][$svg_title_index]);
array_unshift($array['svg'], $value);
}
return $array;
}
/* CASE 1 - SVG EXISTS BUT IS NOT FIRST */
$array = [
'svg' => [
[
'#style' => 'overflow:visible;',
'##xlink:href' => 'test.png'
],
[
'title' => 'some existing title',
],
],
'#version' => '1.2',
'#baseProfile' => 'tiny-ps',
'#id' => 'Layer_1',
'#xmlns' => 'http://www.w3.org/2000/svg'
];
print_r(fix_array( $array ));
/* CASE 2 - SVG DOES NOT EXIST */
$array = [
'svg' => [
[
'#style' => 'overflow:visible;',
'##xlink:href' => 'test.png'
]
],
'#version' => '1.2',
'#baseProfile' => 'tiny-ps',
'#id' => 'Layer_1',
'#xmlns' => 'http://www.w3.org/2000/svg'
];
print_r(fix_array( $array ));
You can see it in action here
Ok so OP want's to move the array with the title attribute to the start of the array or insert a test value if it doesn't exist.
The below function should achieve this. It might not be the most beautiful or effient but it should put you on the right track.
function order_svg_title( array $input, ?string $default_title ) {
$position = null;
// Find position of existing title (if exists).
foreach( $input[ 'svg' ] as $key => $value ) {
if ( isset( $value[ 'title' ] ) && $key !== 0 ) {
$position = $key;
}
}
// Doesn't already exist, add default title (if not null).
if ( is_null( $position ) ) {
array_unshift( $input[ 'svg' ], array( 'title' => $default_title ) );
}
// Title exists but it's in the wrong position.
else {
$value = $input[ 'svg' ][ $position ];
unset( $input[ 'svg' ][ $position ] );
array_unshift( $input[ 'svg' ], $value );
}
return $input;
}
So for this example usage would be...
$arr = array(
"svg" => array(
array(
"#style" => "overflow:visible",
"#xlink:href" => "test.png"
),
array(
array("g" => "", "#id" => "Layer_2"),
array("g" => "", "#id" => "Layer_3")
)
),
"#version" => 1.2,
"#baseProfile" => "tiny-ps",
"#id" => "Layer_1",
"#xmlns" => "http://www.w3.org/2000/svg"
);
$new_arr = order_svg_title( $arr, 'test' );
Would return:
Array
(
[svg] => Array
(
[0] => Array
(
[title] => test
)
[1] => Array
(
[#style] => overflow:visible
[#xlink:href] => test.png
)
[2] => Array
(
[0] => Array
(
[g] =>
[#id] => Layer_2
)
[1] => Array
(
[g] =>
[#id] => Layer_3
)
)
)
[#version] => 1.2
[#baseProfile] => tiny-ps
[#id] => Layer_1
[#xmlns] => http://www.w3.org/2000/svg
)
Now with an existing title:
$arr = array(
"svg" => array(
array(
"#style" => "overflow:visible",
"#xlink:href" => "test.png"
),
array(
array("g" => "", "#id" => "Layer_2"),
array("g" => "", "#id" => "Layer_3")
),
array(
"title" => "In the wrong position. I should be moved."
)
),
"#version" => 1.2,
"#baseProfile" => "tiny-ps",
"#id" => "Layer_1",
"#xmlns" => "http://www.w3.org/2000/svg"
);
$new_arr = order_svg_title( $arr, 'test' );
Would return:
Array
(
[svg] => Array
(
[0] => Array
(
[title] => In the wrong position. I should be moved.
)
[1] => Array
(
[#style] => overflow:visible
[#xlink:href] => test.png
)
[2] => Array
(
[0] => Array
(
[g] =>
[#id] => Layer_2
)
[1] => Array
(
[g] =>
[#id] => Layer_3
)
)
)
[#version] => 1.2
[#baseProfile] => tiny-ps
[#id] => Layer_1
[#xmlns] => http://www.w3.org/2000/svg
)
I have the data :
Array
(
[0] => Array
(
[id] => 12
[code] => 12345
[name] => Aaron
)
[1] => Array
(
[id] => 5
[code] => 16784
[name] => Bryan
)
[2] => Array
(
[id] => 35
[code] => 32467
[name] => Charlie
)
[3] => Array
(
[id] => 25
[code] => 44513
[name] => Denise
)
[4] => Array
(
[id] => 44
[code] => 15774
[name] => Michael
)
)
In my model i create function :
private function getPosition($field_search, $field_value){ // ID or code
$this->db->select('*');
$this->db->from($this->table);
$this->db->order_by('name','asc');
$result = $this->db->get()->result();
$pos = array_search($field_value, array_column($result, $field_search));
return ($pos !==false ? $pos : -1);
}
if i want to get row index of "32467", i just call
$pos = $this->getPosition('code', '32467');
then i got the row index is "2",
but i wonder, how to get row index if the query is :
$this->db->select('*');
$this->db->from($this->table);
$this->db->where('code', '32467');
$this->db->order_by('name','asc');
$result = $this->db->get()->result();
litte correction in my question,
i mean, if we index in database where code = "32467", we've got index is "2", how to get return if index is "2"
thanks for your help...
<?php
$userdb= Array
(
"0" => Array(
'id' => "12",
'code' => "12345",
'name' => 'Aaron'
),
"1" => Array
(
"id" => "5",
"code" => "16784",
"name" => "Bryan"
),
"2" => Array
(
"id" => "35",
"code" => "32467",
"name" => "Charlie"
),
"3" => Array
(
"id" => "25",
"code" => "44513",
"name" => "Denise"
),
"4" => Array
(
"id" => "44",
"code" => "15774",
"name" => "Michael"
),
);
$key = array_search(32467, array_column($userdb, 'code'));
echo ("The key is: ".$key);
private function getPosition($field_search, $field_value){ // ID or code
$this->db->select('*');
$this->db->from($this->table);
$this->db->order_by('name','asc');
$result = $this->db->get()->result();
$key = array_search($field_value, array_column($result, $field_search));
return $key;
}
?>
Been fighting the whole night. Giving up. I have an adjacent table in mysql:
id, parentid,name,design,path,sort
The depth is maximum four and using mysql query, I print out the results to UL list successfully. From there, items are added, sorted and edited as well as removed. Then when button is clicked, I send the result back to php. The data been sent is JSON and it does get recieved.
json_decode() gives the following sample:
Array ( [0] => Array ( [cls] => [path] => # [id] => 1 [name] =>BLOCKA ) [1] => Array ( [cls] => [path] => # [id] => 2 [name] => BLOCKB [children] => Array ( [0] => Array ( [cls] => [path] => # [id] => 3 [name] => CLASSB1 [children] => Array ( [0] => Array ( [cls] => [path] => # [id] => 7 [name] => CLASSB12 ) ) ) [1] => Array ( [cls] => [path] => # [id] => 4 [name] => CLASSSB13 [children] => Array ( [0] => Array ( [cls] => [path] => # [id] => 5 [name] => CLASSB4 ) [1] => Array ( [cls] => [path] => # [id] => 6 [name] => CLASSB5 ) ) ) ) ) )
Graphically:
BLOCKA
BLOCKB
CLASSB1
CLASSB3
...
I am using jquery.nested
Now my problem is looping through the array, getting id of the parent then add child.
The closest I came with is
function dissect($blocks) {
if (!is_array($blocks)) {
echo $blocks;
return;
}
foreach($blocks as $block) {
dissect($block);
}
}
It does process each element but not in the way I want. Sorry for my broken english...any help would be appreciated.
First iterate through blocks getting the parent, if children exists in parent block, loop through the children.
There might be other better way to implement this, you can try below code:
$blocks = array (
"0" => array (
"cls" => "",
"path" => array(
"id" => 1,
"name" =>"BLOCKA",
)
),
"1" => array (
"cls" => "",
"path" => array(
"id" => 2,
"name" => "BLOCKB" ,
"children" => array (
"0" => array (
"cls" => "",
"path" => array(
"id" => 3,
"name" => "CLASSB1" ,
"children" => array (
"0" => array (
"cls" => "",
"path" => array(
"id" => 7,
"name" => "CLASSB12" ,
),
),
),
),
),
"1" => array (
"cls" => "",
"path" => array(
"id" => 4,
"name" => "CLASSSB13" ,
"children" => array (
"0" => array (
"cls" => "",
"path" => array(
"id" => 5,
"name" => "CLASSB4" ,
),
),
"1" => array (
"cls" => "",
"path" => array(
"id" => 6,
"name" => "CLASSB5",
),
),
),
),
),
),
),
),
) ;
echo "<pre>";
/*loop through blocks*/
foreach ($blocks as $key => $block) {
echo $block['path']['name'].'<br/>'; /* echo parent*/
if (isset($block['path']['children'])) {
loopChildren($block['path']['children']); /* children loop*/
}
}
/*Loop through childrens*/
function loopChildren($childrens, $prefix = '-')
{
foreach ($childrens as $key => $child) {
getChild($child, $prefix);
}
}
/*Get the child and loop sub-children if exist*/
function getChild($child, $prefix='-')
{
echo $prefix. $child['path']['name'].'<br/>'; /*echo child*/
if (isset($child['path']['children'])) {
$prefix .= '-';
loopChildren($child['path']['children'], $prefix); /* sub-children loop*/
}
}
echo "</pre>";
The output:
BLOCKA
BLOCKB
-CLASSB1
--CLASSB12
-CLASSSB13
--CLASSB4
--CLASSB5
thanks for your tip. It guided me to an answer that worked for me. I ended up using two functions. The error was "index out of bound"...here is what I did now...with calls to the mysql table:
$response = json_decode($_POST['s'], true); // decoding received JSON to array
if(is_array($response)){
//start saving now
$order=1;
foreach ($response as $key => $block) {
//0 is for blocks: no parent id needed
$parentid=$this->Blocks_model->insertblock($block['name'],$block['cls'],0,$block['path'],$order);
if (isset($block['children'])) {
$this->childblocks($parentid,$block['children']);
}
$order++;
}
}
private function childblocks($parentid,$e){
$order=1;
foreach ($e as $key => $block) {
$parentid=$this->Blocks_model->insertblock($block['name'],$block['cls'],0,$block['path'],$order);
if (isset($block['children'])) {
$this->childblocks($parentid,$block['children']);
}
$order++;
}
}
I have this array
Array (
[0] => Array
(
[0] => stdClass Object
(
[id] => 226
[user_id] => 1
[name] => Eden Corner Tub by Glass - $2099
)
[1] => stdClass Object
(
[id] => 225
[user_id] => 1
[name] => Blue Quilted Leather Jacket by Minusey - $499
)
[2] => stdClass Object
(
[id] => 222
[user_id] => 1
[name] => Darling New Bathtub by Duravit - $6300
)
)
[1] => Array
(
[0] => stdClass Object
(
[id] => 226
[user_id] => 1
[name] => Eden Corner Tub by Glass - $2099
)
[1] => stdClass Object
(
[id] => 229
[user_id] => 1
[name] => Batman Tumbler Golf Cart - $50000
)
[2] => stdClass Object
(
[id] => 228
[user_id] => 1
[name] => Swirlio Frozen Fruit Dessert Maker - $60
)
) )
I have an array of products that I need to make sure are unique.
Need to make this array unique by id. These array are generated by pushing value.
I'm trying to solve this for more than a week now, but I dont get it to work. I know it should be easy...but anyway - I don't get it :D
Try this:
$array = array(
0 => array(
"name" => "test",
"id" => 4
),
1 => array(
"name" => "test2",
"id" => 152
),
2 => array(
"name" => "test2",
"id" => 152
)
);
$newArray = array();
foreach($array as $value) {
$newArray[$value['id']]['name'] = $value['name'];
$newArray[$value['id']]['id'] = $value['id'];
}
foreach($array as $key=>$inner_array)
{
foreach($array as $key_again=>$array_again)
{
if($key != $key_again)
{
if($inner_array['name'] != $array_again['name'] AND $inner_array['id'] != $array_again['id'])
{
//its okay
}
else
{
unset($array[$key]);
}
}
}
}
Not sure how the arrays are generated, but, if you can change that, you could set the array keys to the IDs directly and check if the id is already set.
Otherwise, you can do the following:
$unique = array();
foreach( $array as $values ) {
if( ! isset( $unique[$values['id']] ) ) {
$unique[$values['id']] = $values;
}
}
This will make your array unique:
$array = array(
0 => array(
"name" => "test",
"id" => 4
),
1 => array(
"name" => "test2",
"id" => 152
),
2 => array(
"name" => "test2",
"id" => 152
) );
$result = array();
$index = array();
foreach($array as $i => $elem) {
if(!isset($index[$elem['id']])) {
$result[$i] = $elem;
$index[$elem['id']] = 1;
}
}
echo var_export($result);
Output:
array (
0 =>
array (
'name' => 'test',
'id' => 4,
),
1 =>
array (
'name' => 'test2',
'id' => 152,
),
)
This will work. It could be considered more clean than a for loop, but I'm not sure about performance.
$array = [
[ "name" => "test", "id" => 4 ],
[ "name" => "test2", "id" => 152 ],
[ "name" => "test2", "id" => 152 ]
];
array_walk($array, function(&$item, $idx) use($array){
$matches = array_slice(array_keys($array, $item), 1);
if (in_array($idx, $matches)) {
$item = false;
}
});
$array = array_filter($array);
Edit Since you updated the data set to work with, you would need to flatten it 1 level:
$array = call_user_func_array('array_merge', $array);
Hi i have the following array and i want to pull the array with productId 100343
Array(
[_id] => MongoId Object
(
[$id] => 5388a02c8ead0ebf048b4569
)
[cartId] => 14ce496ac194d5d8ee8d0cd11bd3ef5a
[products] => Array
(
[100343] => Array
(
[productId] => 100343
[quantity] => 13
[name] => a
)
[100344] => Array
(
[productId] => 100344
[quantity] => 3
[name] => ab
)
[100345] => Array
(
[productId] => 100345
[quantity] => 1
[name] => abc
)
)
)
I've tried like this but it doesn't work
$c->update(
$aQuery,
array(
'$pull' => array('products'=>array('productId'=>'100343'))
)
);
That is not an array. Arrays in MongoDB are different in concept to what PHP calls an array, so the structure you have there is the same as this JSON representation:
{
"products": {
"100343": {
"productId": 100343,
"quantity": 13,
"name": "a"
},
"100344": {
"productId": 100344,
"quantity": 3,
"name": "ab"
},
"100345": {
"productId": 100345,
"quantity": 1,
"name": "abc"
}
}
}
That sort of structure is just a use of sub-documents and is not an array in the sense as used by MongoDB. To remove the entry as you want with this structure you use the $unset operator.
$c->update(
$aQuery,
array(
'$unset' => array('products.productId.100343' => "")
)
);
But really you need to change your data, so to serialize a proper array for MongoDB you need to structure code like this:
$data = array(
"products" => array(
array(
"productId" => 100343,
"quantity" => 13,
"name" => "a"
),
array(
"productId" => 100344,
"quantity" => 3,
"name" => "ab"
),
array(
"productId" => 100345,
"quantity" => 1,
"name" => "abc"
)
)
);
That produces a JSON structure that looks like this:
{
"products":[
{
"productId":100343,
"quantity":13,
"name":"a"
},
{
"productId":100344,
"quantity":3,
"name":"ab"
},
{
"productId":100345,
"quantity":1,
"name":"abc"
}
]
}
That is what MongoDB calls an array, so now you can use the $pull operator correctly:
$c->update(
$aQuery,
array(
'$pull' => array( 'products.productId' => 100343 )
)
);