I want to transform this array:
Array
(
[0] => Array
(
[group] => site
[key] => date_format
[value] => %d %B %Y - %H:%M:%S
)
[1] => Array
(
[group] => site
[key] => description
[value] => blah
)
[2] => Array
(
[group] => site
[key] => keywords
[value] =>
)
[3] => Array
(
[group] => pages
[key] => permalink
[value] => <page>(/<subpage>)
)
[4] => Array
(
[group] => system
[key] => plugins
[value] => a:1:{i:0;s:5:"pages";}
)
[5] => Array
(
[group] => site
[key] => title
[value] => some title
)
)
... to this ...
Array
(
[system] => Array
(
[plugins] => a:1:{i:0;s:5:"pages";}
)
[site] => Array
(
[date_format] => %d %B %Y - %H:%M:%S
[description] => blah
[keywords] =>
[title] => some title
)
[pages] => Array
(
[permalink] => <page>(/<subpage>)
)
)
Is there any ways to do this without foreach and any other loop?
No you cannot.
And this micro-optimization would be pointless. Just write a simple foreach loop and stop trying to invent a square wheel.
Related
I use jstree to store a tree structure using JSON. My example structure looks like this:
After using
$treeJSONdecoded = json_decode($treeJSON, true);
my JSON looks like this:
[0] => Array (
[id] => j1_1
[text] => Release1
[icon] => 1
[li_attr] => Array (
[id] => j1_1
)
[a_attr] => Array (
[href] => #
[id] => j1_1_anchor
)
[state] => Array (
[loaded] => 1
[opened] => 1
[selected] => 1
)
[data] => Array ( )
[children] => Array (
[0] => Array (
[id] => j1_3
[text] => List of features
[icon] => 1
[li_attr] => Array (
[id] => j1_3
)
[a_attr] => Array (
[href] => #
[id] => j1_3_anchor
)
[state] => Array (
[loaded] => 1
[opened] => 1
[selected] =>
)
[data] => Array ( )
[children] => Array (
[0] => Array (
[id] => j1_9
[text] => feature1
[icon] => 1
[li_attr] => Array (
[id] => j1_9
)
[a_attr] => Array (
[href] => #
[id] => j1_9_anchor
)
[state] => Array (
[loaded] => 1
[opened] =>
)
[data] => Array ( )
[children] => Array ( )
[type] => default
)
)
[type] => default
)
[1 => Array ( id] => j1_2
[text] => List of documents
[icon] => 1
[li_attr] => Array (
[id] => j1_2
)
[a_attr] => Array (
[href] => #
[id] => j1_2_anchor
)
[state] => Array (
[loaded] => 1
[opened] => 1
[selected] =>
)
[data] => Array ( )
[children] => Array (
[0] => Array (
[id] => j1_5
[text] => document1
[icon] => 1
[li_attr] => Array (
[id] => j1_5
)
[a_attr] => Array (
[href] => #
[id] => j1_5_anchor
)
[state] => Array (
[loaded] => 1
[opened] =>
)
[data] => Array ( )
[children] => Array ( )
[type] => default
)
)
[type] => default )
)
[type] => default
)
How do I iterate through the whole JSON to get 'text' values and have an array like:
{"Release1", "List of features", ... , "document1"}
assuming that I don't know how many levels there are. I tried something like
foreach($treeJSONdecoded as $val){
echo $val['text'];
}
just to see what I can fetch but it doesn't seem to work.
Thanks #Scuzzy, this got me what I wanted:
$jsonIterator = new RecursiveIteratorIterator(
new RecursiveArrayIterator(json_decode($treeJSON, TRUE)),
RecursiveIteratorIterator::SELF_FIRST);
$newArray = [];
foreach ($jsonIterator as $key => $val) {
if(!is_array($val) && $key == 'text') {
array_push($newArray, $val);
}
}
This is the array which i get from post
Array
(
[data] => Array
(
[Invoice] => Array
(
[itemNo] => Array
(
[0] => rtgrg
[1] => 4t4t
[2] => ththt
[3] => thth
)
[itemName] => Array
(
[0] => rtgrt
[1] => 4t5t5
[2] => hthtyh
[3] => gnghnn
)
[itemDiscription] => Array
(
[0] => 5tr5t
[1] => t45t4
[2] => tyhthtyh
[3] => gnghnh
)
[price] => Array
(
[0] => 2
[1] => 10
[2] => 9
[3] => 12
)
[itemQuantity] => Array
(
[0] => 2
[1] => 12
[2] => 9
[3] => 9
)
[itemDiscount] => Array
(
[0] => 11
[1] => 0.14
[2] => 0.13
[3] => 0.1
)
[itemTotal] => Array
(
[0] => 333333
[1] => 34535
[2] => 55555555555555
[3] => 666666666
)
[itemStartDate] => Array
(
[0] =>
[1] =>
[2] =>
[3] => 2016-06-17
)
[itemEndDate] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
)
[itemCustomcol] => Array
(
[0] => 4t4t4
[1] => t5trgr
[2] => htht
[3] => gngh
)
[itemCustomcol2] => Array
(
[0] => t4t4t
[1] => rtgtr
[2] => thth
[3] => gng
)
[itemttax2] => Array
(
[0] => tax1
)
[itemttax3] => Array
(
[0] => tax1
)
[itemttax4] => Array
(
[0] => tax1
)
[itemtCsTax] => Array
(
[0] => gngn
)
[itemtCsTaxPer] => Array
(
[0] => 0.1
)
[itemtDiscount] => Array
(
[0] => 0.18
)
[itemtInvReceived] => Array
(
[0] => gngn
)
[itemtInvToReturn] => Array
(
[0] => gngng
)
[itemInvDue] => Array
(
[0] => nghnghng
)
[itemInvComment] => Array
(
[0] => hngnhg
)
)
)
There is a separate table for dynamic input like itemName, itemDiscription to itemCustomcol2 and another table for itemttax2 to itemInvComment .
I tried foreach loop for multiple element array but this doent work for me as it returns a string. one of them is shown below
$capture_field_vals ="";
foreach($_POST["data"]["Invoice"]["itemNo"] as $key => $text_field){
$capture_field_vals .= $text_field .", ";
}
echo $capture_field_vals;
What i want is collect all element[0] and then insert it in a row mysql and then another element another row and so on.
Is there any reason you can't use the following format?
Array
(
[data] => Array
(
[Invoice] => Array
(
[0] => Array
(
[itemNo] => number
[itemName] => name
[itemDescription] => desc
[price] => 0.00
),
[1] => Array
(
[itemNo] => number
[itemName] => name
[itemDescription] => desc
[price] => 0.00
),...
with this method you can loop through retrieving all details of each item in the invoice?
If I have misunderstood your problem, I apologise
I get this array in response from webservice, so How do I read it in a foreach cycle? or some easy way to read it. The [group] they are more than 12 [id]
there is.
Array
( [response] => Array (
[single] => Array ( [parameters] => Array ( [0] => Array ( [name] => msgCode [value] => 0101 )
[1] => Array ( [name] => msgDesc [value] => OK )
[2] => Array ( [name] => status [value] => 1)
[3] => Array ( [name] => message [value] => Normal) ) )
[group] => Array ( [id] => N4BD767 [parameters] => Array ( [0] => Array ( [name] => idFee [value] => 000 ) echo
[1] => Array ( [name] => typeFee [value] => Cuota)
[2] => Array ( [name] => entryDate [value] => 2014-12-17T14:06:47-03:00 )
[3] => Array ( [name] => expirationDate [value] => 2015-12-05T00:00:00-03:00)
[4] => Array ( [name] => amountOrigin [value] => 221980)
[5] => Array ( [name] => surcharges [value] => 1856)
[6] => Array ( [name] => entity [value] => ONLINE)
[7] => Array ( [name] => feeStatus [value] => inicial )
[8] => Array ( [name] => tranNumber [value] => 27) ) ) ) )
Okay guys, this is what I did and it work fine. With no foreach.
$output = array();
array_walk_recursive($result, function($item,$key) use (&$output){
array_push($output,$key,$item);
});
echo var_dump($output);
I know this is considered a somewhat basic PHP Array question but i'm running on 35 hours no sleep and I really just need to finish this up as quickly as posibble so I can get to sleep...sorry just being honest!
In PHP I have this variable $design_values
If I print_r($design_values); this ARRAY it spits out what is show below. It is "Desing" database records.
In this case there is 2 Design records which make up the first 2 Array keyys the 0 and 1
In the application there can be any number of Designs from 0 up to any number.
Now under the 2 Design Records are 24 more Array keys for each of the 2 Design Arrays.
These 24 Array keys are numbered 0 to 23.
Now under each of the 24 Array keys, is 2 keys. One named name and the other named value.
I need to take the Array $design_values and create a new Array of that array. The new Array should be formatted much better amd easy to work with.
So the name and value keys should make up a key => value. The New array should look more like this....
The reason the current Array is a complete nightmare is because that is the Format I get it in from an existing library which returns this Data from an API call.
If someone can help me to manipulate this Array into the desired Array I will be grateful! I have been messing with it for 2 hours with no luck.
Desired New Array Format :
Array
(
[0] => Array
(
['assigned_user_name'] => 'Jason Administrator',
['modified_by_name'] => 'Jason Administrator',
['created_by_name'] => 'Jason Administrator',
['id'] => '4c5c3c08-2b14-9f9c-6cee-542c56cac7b1',
['date_entered'] => '2014-10-01 19:29:32',
....continued for all 24 record items
),
[1] => Array
(
['assigned_user_name'] => 'Jason Administrator',
['modified_by_name'] => 'Jason Administrator',
['created_by_name'] => 'Jason Administrator',
['id'] => '4c5c3c08-2b14-9f9c-6cee-542c56cac7b1',
['date_entered'] => '2014-10-01 19:29:32',
....continued for all 24 record items
)
)
Current Array Format :
Array
(
[0] => Array
(
[0] => Array
(
[name] => assigned_user_name
[value] => Jason Administrator
)
[1] => Array
(
[name] => modified_by_name
[value] => Jason Administrator
)
[2] => Array
(
[name] => created_by_name
[value] => Jason Administrator
)
[3] => Array
(
[name] => id
[value] => 4c5c3c08-2b14-9f9c-6cee-542c56cac7b1
)
[4] => Array
(
[name] => name
[value] => test
)
[5] => Array
(
[name] => date_entered
[value] => 2014-10-01 19:29:32
)
[6] => Array
(
[name] => date_modified
[value] => 2014-10-01 19:29:32
)
[7] => Array
(
[name] => modified_user_id
[value] => 1
)
[8] => Array
(
[name] => created_by
[value] => 1
)
[9] => Array
(
[name] => description
[value] =>
)
[10] => Array
(
[name] => deleted
[value] => 0
)
[11] => Array
(
[name] => assigned_user_id
[value] => 1
)
[12] => Array
(
[name] => chann_channelqms_id_c
[value] =>
)
[13] => Array
(
[name] => channelqms
[value] =>
)
[14] => Array
(
[name] => design_name
[value] =>
)
[15] => Array
(
[name] => design_number
[value] =>
)
[16] => Array
(
[name] => overall_height
[value] =>
)
[17] => Array
(
[name] => overall_width
[value] =>
)
[18] => Array
(
[name] => show_to_customer
[value] => 1
)
[19] => Array
(
[name] => uploadfile
[value] => 2014-09-29_21-57-50.png
)
[20] => Array
(
[name] => nam_channelletterqms_nam_channelletterqms_designs_name
[value] => Test
)
[21] => Array
(
[name] => price_c
[value] =>
)
[22] => Array
(
[name] => shipping_c
[value] =>
)
[23] => Array
(
[name] => totalprice_c
[value] =>
)
)
[1] => Array
(
[0] => Array
(
[name] => assigned_user_name
[value] => Jason Administrator
)
[1] => Array
(
[name] => modified_by_name
[value] => Jason Administrator
)
[2] => Array
(
[name] => created_by_name
[value] => Jason Administrator
)
[3] => Array
(
[name] => id
[value] => 86f21f44-4b21-1826-3592-542c59e4be66
)
[4] => Array
(
[name] => name
[value] => fdtgrfdhg
)
[5] => Array
(
[name] => date_entered
[value] => 2014-10-01 19:41:54
)
[6] => Array
(
[name] => date_modified
[value] => 2014-10-19 19:30:45
)
[7] => Array
(
[name] => modified_user_id
[value] => 1
)
[8] => Array
(
[name] => created_by
[value] => 1
)
[9] => Array
(
[name] => description
[value] =>
)
[10] => Array
(
[name] => deleted
[value] => 0
)
[11] => Array
(
[name] => assigned_user_id
[value] => 1
)
[12] => Array
(
[name] => chann_channelqms_id_c
[value] =>
)
[13] => Array
(
[name] => channelqms
[value] =>
)
[14] => Array
(
[name] => design_name
[value] => design name
)
[15] => Array
(
[name] => design_number
[value] => 313
)
[16] => Array
(
[name] => overall_height
[value] => 22
)
[17] => Array
(
[name] => overall_width
[value] => 22
)
[18] => Array
(
[name] => show_to_customer
[value] => 1
)
[19] => Array
(
[name] => uploadfile
[value] => 2014-09-29_21-57-50.png
)
[20] => Array
(
[name] => nam_channelletterqms_nam_channelletterqms_designs_name
[value] => Test
)
[21] => Array
(
[name] => price_c
[value] =>
)
[22] => Array
(
[name] => shipping_c
[value] =>
)
[23] => Array
(
[name] => totalprice_c
[value] =>
)
)
)
If you can't change it at the source then here is one way (PHP >= 5.5.0 needed for array_column):
foreach($design_values as $key => $values) {
$result[$key] = array_combine(
array_column($values, 'name'),
array_column($values, 'value'));
}
Or possibly, and easier:
foreach($design_values as $key => $values) {
$result[$key] = array_column($values, 'value', 'name');
}
Our use the PHP Implementation of array_column
You should probably create the array you want when making the first array, but if you don't have control over that and just want to conver then something like this should work:
$newArray = array();
foreach($oldArray as $row){
$tmp = array();
foreach($row as $values){
$tmp[$values['name']] = $values['value'];
}
$newArray[] = $tmp;
}
print_r($newArray);
I have a multi dimensional array of items which all have a date. The array follows the format: $relatedItems[$year][$month][$day][]. Is there a way of sorting this so that it is in date order. I've tried krsort() bu i think that will just sort the year key e.g. [2014]. Sorry if this is vague I'm new to php.
Array
(
[2014] => Array
(
[01] => Array
(
[13] => Array
(
[0] => Array
(
[type] => news-blog
[title] => Jungle News
[date] => 13th January 2014
[url] => /sport/jungle-ultra/news/jungle-news/
[description] => Description goes here
)
)
[23] => Array
(
[0] => Array
(
[type] => gallery
[title] => New Gallery
[url] => /sport/jungle-ultra/galleries/new-gallery/
[images] => Array
(
[0] => Array
(
[title] => 10.jpg
[src] => /files/cache/978f98c61f63757334fbeec79fabe482_f65.jpg
)
[1] => Array
(
[title] => 9.jpg
[src] => /files/cache/1526727d35b28930b49c69b065a229de_f64.jpg
)
[2] => Array
(
[title] => 8.jpg
[src] => /files/cache/d1b67fefed5e7b7644ad9ec1fe8559ae_f63.jpg
)
[3] => Array
(
[title] => 7.jpg
[src] => /files/cache/80160e395233a15d1b1df4df67ec565d_f62.jpg
)
[4] => Array
(
[title] => 6.jpg
[src] => /files/cache/48575e2d53c487f99633a1105e7a322b_f61.jpg
)
[5] => Array
(
[title] => 4.jpg
[src] => /files/cache/9fe01089fc9656562fb7f082499439fc_f60.jpg
)
)
)
)
[27] => Array
(
[0] => Array
(
[type] => video
[title] => Test
[url] => /sport/jungle-ultra/video/test/
[thumb] => /files/5313/8996/9327/speaker-header-small.jpg
)
)
[24] => Array
(
[0] => Array
(
[type] => video
[title] => Rich reaches half way point
[url] => /sport/jungle-ultra/video/rich-reaches-half-way-point/
[thumb] => /files/7013/9039/4602/video-cover-test.jpg
)
)
[21] => Array
(
[0] => Array
(
[type] => audio
[title] => Day 1 - Update from Alexander Island
[url] => /sport/jungle-ultra/audio/day-1-update-alexander-island/
[thumb] => /files/7013/9039/4602/video-cover-test.jpg
)
)
)
)
)
You are correct in using ksort but you need to use a recursive function. Try using this:
function sort_by_date($dates){
if(is_array($dates)){ // If it is an array
ksort($dates); // Sort the array keys
foreach($dates as $date){ // Loop through the keys
sort_by_date($date); // And call this recursive function on each of the child arrays
}
}
}
Usage:
$array = array(...); // Your date array
sort_by_date($array); // New function for sorting