json array sort by value in php - php

I have JSON arrays of objects. I am trying to sort an array using usort. I want to use value field from field_listing_order. It sorted using value. I am missing something but not able to figure it out. please review the code. Thanks!
stdClass Object
(
[node_title] => abc
[nid] => 2281
[field_api_order_value] => 201
[field_node_entity_type] => node
[_data] => Array
(
[nid] => Array
(
[entity_type] => node
[entity] => stdClass Object
(
[title] => abc
[field_listing_order] => Array
(
[und] => Array
(
[0] => Array
(
[value] => 8
[format] =>
[safe_value] => 8
)
)
)
)
)
)
)
stdClass Object
(
[node_title] => abc
[nid] => 2243
[field_api_order_value] => 204
[field_node_entity_type] => node
[_data] => Array
(
[nid] => Array
(
[entity_type] => node
[entity] => stdClass Object
(
[title] => abc
[field_listing_order] => Array
(
[und] => Array
(
[0] => Array
(
[value] => 3
[format] =>
[safe_value] => 3
)
)
)
)
)
)
) stdClass Object
(
[node_title] => abc
[nid] => 2431
[field_api_order_value] => 242
[field_node_entity_type] => node
[_data] => Array
(
[nid] => Array
(
[entity_type] => node
[entity] => stdClass Object
(
[title] => abc
[field_listing_order] => Array
(
[und] => Array
(
[0] => Array
(
[value] => 1
[format] =>
[safe_value] => 1
)
)
)
)
)
)
)
and So on ...
foreach ($view->result as $result) {
$node = $result->_data['nid']['entity'];
$listing_order = $node->field_listing_order[LANGUAGE_NONE][0];
.....
// code goes here and it works well. sorting issue
}
usort ($node->field_listing_order[LANGUAGE_NONE][0], function($a, $b){
return strcmp($a->value, $b->value);
}); ?>

If you need to sort all the nodes using the field_listing_order value, you need to compare the values along the full path, from the first object to the value:
usort($view->result, function($a, $b) {
$la = $a->_data['nid']['entity']->field_listing_order[LANGUAGE_NONE][0]['value'];
$lb = $b->_data['nid']['entity']->field_listing_order[LANGUAGE_NONE][0]['value'];
return $la - $lb ;
});
In this case $a and $b are two different nodes which could be compared. That why you should compare the field_listing_order's value of these nodes. The answer is working with $la- $lb

Related

how to add array value at specfic index in php codenighter

I have array value like this
Array
(
[0] => Array
(
[channel] => 15
[id] => clsrnMdVKq2omEuQabSCHp83ezAX6w
)
[1] => Array
(
[channel] => 16
[id] => MfSoHUKjD5n90EZbstpiRGY7e8cgh2
)
[2] => Array
(
[channel] => 17
[id] => MfSoHUKjD5n90EZbstpiRGY7e8cgh2
)
)
Now i want to add another array value in specific index .lets say i wants to add this array value at index 1
[1] => Array
(
[channel] => 20
[id] => xxxxxxxxxxxewqeqwexxxxxxxewrewrw
)
Now the result output should be like this
Array
(
[0] => Array
(
[channel] => 15
[id] => clsrnMdVKq2omEuQabSCHp83ezAX6w
)
[1] => Array
(
[channel] => 20
[id] => xxxxxxxxxxxewqeqwexxxxxxxewrewrw
)
[2] => Array
(
[channel] => 16
[id] => MfSoHUKjD5n90EZbstpiRGY7e8cgh2
)
[3] => Array
(
[channel] => 17
[id] => MfSoHUKjD5n90EZbstpiRGY7e8cgh2
)
)
this is my foreach loop to serlize channel and id
foreach ($channel as $key => $ch) {
$user_hash['channel'] = json_encode($ch);
$user_hash['id'] = random_string('alnum', 30);
array_push($user_hash_array, $user_hash);
}
You need to split the array into 2, then insert your new value at the end of the first sub-array, then merge it with the second sub-array. EG: an array which looks like [1,3,4,5] and you want to insert "2" at position 2, then you split at position one to have [1] and [3,4,5]; then you append "2" at the end of first sub-array to form [1,2], then merge this new subarray with the other sub-array([3,4,5]) to form [1,2] + [3,4,5].
For your implementation, try this code:
$array = array() // the original array you want to modify
$insert = array() // the array you want to push into the original one above
$position = 1 // the position at which you want to insert the new item
$newArray = array_slice($array, 0, $position, TRUE) + $insert + array_slice($array, $position, NULL, TRUE);
you can use array_splice array method for add element in array at particular position
<?php
$original_array = array(
array("channel"=>15,"id"=>"sdfdfsf1"),
array("channel"=>16,"id"=>"sdfdfsf2"),
array("channel"=>17,"id"=>"sdfdfsf3")
);
echo "<pre>";print_r($original_array);
$inserted_element =
array(array("channel"=>20,"id"=>"xxxxxxxxxxewqeqwexxxxxxxewrewrw"));
$position=1;
array_splice( $original_array, $position, 0, $inserted_element );
echo "<pre>";print_r($original_array);
?>
Output will be as following
Array
(
[0] => Array
(
[channel] => 15
[id] => sdfdfsf1
)
[1] => Array
(
[channel] => 16
[id] => sdfdfsf2
)
[2] => Array
(
[channel] => 17
[id] => sdfdfsf3
)
)
Array
(
[0] => Array
(
[channel] => 15
[id] => sdfdfsf1
)
[1] => Array
(
[channel] => 20
[id] => xxxxxxxxxxewqeqwexxxxxxxewrewrw
)
[2] => Array
(
[channel] => 16
[id] => sdfdfsf2
)
[3] => Array
(
[channel] => 17
[id] => sdfdfsf3
)
)

How to remove parent array index from array

I want to remove parent array index in array.
Following is my array.
Array
(
[0] => Array
(
[0] => Array
(
[id] => 296
[username] => David0123
[profile_slug] => david-love
)
)
[1] => Array
(
[0] => Array
(
[id] => 297
[username] => Anne_wils
[profile_slug] => anne-chase
)
)
[2] => Array
(
[0] => Array
(
[id] => 300
[username] => malina001
[profile_slug] => malina-reid
)
)
)
And I want like this way..
Array(
[0] => Array
(
[id] => 296
[username] => David0123
[profile_slug] => david-love
)
[1] => Array
(
[id] => 297
[username] => Anne_wils
[profile_slug] => anne-chase
)
[2] => Array
(
[id] => 300
[username] => malina001
[profile_slug] => malina-reid
)
)
I used following script for it but not work.
$myMainArray = json_decode(json_encode($allEscorts),true);
$i=0;
foreach( array_values($myMainArray) as $k=> $val){
echo $val[$i]['id'];
$i++;
}
I want to display data each element but first i have to remove parent array indexes.
You can use array_map to pull values up one level
$myMainArray = json_decode(json_encode($allEscorts),true);
$myMainArray = array_map(function($el) {
return $el[0];
}, $myMainArray);
You should check if the first array could be generate as you wish.
If not you can use array_map to get the first index from the inner-array.
for example:
$result = array_map(function($item){
return $item[0]; // always return the first array-index
}, $first_array);

Multidimensional array in pdf report using codeigniter

Below is the code snippet for printing values in a PDF report using codeigniter.
foreach ($data as $key => $getData)
{
$html1 = $this->load->view('pdf/pod2', $getData, true);
//echo $html;exit;
$this->load->library('pdf');
$pdf = $this->pdf->load();
$pdf->setAutoTopMargin = 'stretch';
$pdf->setAutoBottomMargin = 'stretch';
//$pdf->SetFooter($_SERVER['HTTP_HOST'].'|{PAGENO}|'.date(DATE_RFC822));
$pdf->WriteHTML($html1);
}
If i do echo '<pre>';print_r($data);exit;
I get the following output
Array
(
[316] => Array
(
[CNote] => stdClass Object
(
[Consignment_Details_id] => 316
)
[Invoice_Nos] => Array
(
[0] => stdClass Object
(
[invoice] => 161052
)
[1] => stdClass Object
(
[invoice] => 161053
)
)
[Invoice_Copies] => Array
(
)
[CNote_data] => Array
(
)
[no_packages] => 0
[driver] =>
)
[317] => Array
(
[CNote] => stdClass Object
(
[Consignment_Details_id] => 317
)
[Invoice_Nos] => Array
(
[0] => stdClass Object
(
[invoice] => 161066
)
)
[Invoice_Copies] => Array
(
)
[CNote_data] => Array
(
)
[no_packages] => 0
[driver] =>
)
[318] => Array
(
[CNote] => stdClass Object
(
[Consignment_Details_id] => 318
)
[Invoice_Nos] => Array
(
[0] => stdClass Object
(
[invoice] => 161067
)
)
[Invoice_Copies] => Array
(
)
[CNote_data] => Array
(
)
[no_packages] => 0
[driver] =>
)
)
Now the problem is in the pdf report only the last array values is being displayed
i.e 318
Actual scenario what i need is o display three array values but only last value is getting displayed. How to solve this, any help appreciated.

Sort multidimensional array recursive by specific key

I'm trying to sort this array recursively by its label:
Array
(
[0] => Array
(
[id] => 6
[label] => Bontakt
[children] => Array
(
)
)
[1] => Array
(
[id] => 7
[label] => Ampressum
[children] => Array
(
[0] => Array
(
[id] => 5
[children] => Array
(
)
[label] => Bome
)
[1] => Array
(
[id] => 8
[children] => Array
(
)
[label] => Aome
)
[2] => Array
(
[id] => 10
[children] => Array
(
)
[label] => Come
)
)
)
[2] => Array
(
[id] => 9
[label] => Contakt
[children] => Array
(
)
)
[3] => Array
(
[id] => 11
[label] => Dead
[children] => Array
(
)
)
)
I've read several Questions, and I feel to be pretty close, but I can't figure out what's not working:
function sortByAlpha($a, $b)
{
return strcmp(strtolower($a['label']), strtolower($b['label'])) > 0;
}
function alphaSort(&$a)
{
foreach ($a as $oneJsonSite)
{
if (count($oneJsonSite["children"]) > 0) alphaSort($oneJsonSite["children"]);
}
usort($a, 'sortByAlpha');
}
alphaSort($jsonSites);
Current output is like this:
Ampressum
Bome
Aome
Come
Bontakt
Contakt
Dead
The children elements are not sorted...
Check this out:
In order to be able to directly modify array elements within the loop precede $value with &. In that case the value will be assigned by reference.
(Picked from here: http://php.net/manual/en/control-structures.foreach.php)
You should try it with this one:
function alphaSort(&$a)
{
foreach ($a as &$oneJsonSite)
{
if (count($oneJsonSite["children"]) > 0) alphaSort($oneJsonSite["children"]);
}
usort($a, 'sortByAlpha');
}

php move nodes to parent array

How could I move all nodes of the 'fields' array into its parent array '113', whilst unsetting 'fields' ?
[a] => Array
(
[113] => Array
(
[title] => asdfasdfas
[alias] => asdfasdfas
[fields] => Array
(
[jr_streetaddress] => Array
(
[type] => text
[label] => Street Address
[data] => asdfasdffsd
)
[jr_towncity] => Array
(
[type] => text
[label] => Town / City
[data] => Nottingham
)
)
)
)
Assuming your top level array ($something['a']) is the variable $a:
foreach($a as $key => $values){
if(isset($values['fields']))
{
$a[$key] = array_merge($a[$key], (array) $values['fields']);
unset($a[$key]['fields']);
}
}
Alternatively, if you dont want to hit every array element in $a you can just remove the loop and substitute $values with $a[113] and $key with 113.
Also note the casting for the fields element to an array, jsut in case it isnt one with (array) $values['fields']
If you can make this array like this:
[a] => Array
(
[113] => Array
(
[title] => asdfasdfas
[alias] => asdfasdfas
[jr_streetaddress] => Array
(
[type] => text
[label] => Street Address
[data] => asdfasdffsd
)
[jr_towncity] => Array
(
[type] => text
[label] => Town / City
[data] => Nottingham
)
)
)
try to use this code:
$array['a'][113]['jr_streetaddress'] = $array['a'][113]['fields']['jr_streetaddress'];
$array['a'][113]['jr_towncity'] = $array['a'][113]['fields']['jr_towncity'];
unset($array['a'][113]['fields']);

Categories