I am currently getting the same $val results but the $key is working fine. What am i doing wrong?
foreach($awards['award_title'] as $key) {
foreach($_POST['award_title_new'] as $val)
$award_title_trans[$key] = $val;
}
update_option('award_title', $award_title_trans );
$awards array:
["award_title"]=>
array(2) {
[0]=>
string(7) "testnew"
[1]=>
string(5) "newti"
}
and the $POST is an input array name='award_title_new[]'
What i am expecting is to save the $awards value as a key and the $POST as the value:
array(2) {
["testnew"]=>
string(3) "345"
["newti"]=>
string(3) "345"
}
This did the trick!
array_combine($awards['award_title'], $_POST['award_title_new']);
array(2) {
["testnew"]=>
string(3) "hello"
["newti"]=>
string(3) "bye"
}
Related
What is the shortest way to change multiple values in array key that has a specific value in it?
For example, I have this array:
array(2) {
[0]=>
array(5) {
["state"]=>
string(6) "active"
["payer_mail"]=>
string(12) "mail#none.com"
["start"]=>
string(12) "06/05/2015"
["end"]=>
string(8) "08/07/2017"
["price"]=>
string(8) "45.00"
["keystring"]=>
string(8) "493457025"
}
[1]=>
array(6) {
["place"]=>
string(2) "47"
["state"]=>
string(8) "canceled"
["payer_mail"]=>
string(12) "mail#none.com"
["start"]=>
string(9) "20/8/2014"
["end"]=>
string(10) "20/10/2017"
["price"]=>
string(5) "95.00"
["keystring"]=>
string(8) "34879205"
}
}
And I want to change the "state" value of the key that has "34879205" value for his "keystring" sub-key.
You can use foreach():-
foreach($array as &$value) {
if ($value['keystring'] == '34879205'){
$value['state'] = "";//change value here
}
}
Output:- https://eval.in/838789
Another way i found is:
$key = array_search('34879205', array_column($array, 'keystring'));
$array[$key]['state'] = 'newvalue';
You can use array_map function
$result = array_map(function ($element) {
if ($element['keystring'] === '34879205') {
$element['state'] = 'new_state';
}
return $element;
}, $array);
I have array $result
array(2) {
["Smiley TV"]=>
array(2) {
["Speed"]=>
array(2) {
[0]=>
string(4) "9510"
[1]=>
string(5) "33775"
}
["Turbo"]=>
array(2) {
[0]=>
string(4) "2427"
[1]=>
string(5) "19696"
}
}
["Victory Media"]=>
array(1) {
["Speed"]=>
array(2) {
[0]=>
string(4) "4144"
[1]=>
string(5) "80445"
}
}
}
How with foreach i can get values. For example in the "Smyley TV" how i can result "Speed".
Also please write how i can get all data one by one. Thanks
You can fetch this way
foreach ($arrays as $array) {
echo $array['smyley TV'];
}
I have this array:
array(5) {
[0]=>
array(4) {
["productCode"]=>
string(4) "X001"
["productUPC"]=>
string(3) "261"
["productTextSeq"]=>
string(1) "1"
["productTxtVal"]=>
string(5) "Text1"
}
[1]=>
array(4) {
["productCode"]=>
string(4) "X001"
["productUPC"]=>
string(3) "261"
["productTextSeq"]=>
string(1) "2"
["productTxtVal"]=>
string(5) "Text2"
}
[2]=>
array(4) {
["productCode"]=>
string(4) "X001"
["productUPC"]=>
string(3) "261"
["productTextSeq"]=>
string(1) "3"
["productTxtVal"]=>
string(5) "Text3"
}
[3]=>
array(4) {
["productCode"]=>
string(4) "X002"
["productUPC"]=>
string(3) "262"
["productTextSeq"]=>
string(1) "1"
["productTxtVal"]=>
string(5) "Text1"
}
[4]=>
array(4) {
["productCode"]=>
string(4) "X002"
["productUPC"]=>
string(3) "262"
["productTextSeq"]=>
string(1) "2"
["productTxtVal"]=>
string(5) "Text2"
}
}
With the above input, I want the output array to look like this:
array(2) {
[0]=>
array(3) {
["productCode"]=>
string(4) "X001"
["productUPC"]=>
string(3) "261"
["productTxtVal"]=>
string(17) "Text1 Text2 Text3"
}
[1]=>
array(3) {
["productCode"]=>
string(4) "X002"
["productUPC"]=>
string(3) "262"
["productTxtVal"]=>
string(11) "Text1 Text2"
}
}
The resulting array does not need the productTextSeq key, just the combined values of productTextVal, when the productCode is the same. I've searched SO for examples of this but it seems every example I've found are based on multiple input arrays. I know I can brute force this with nested foreach functions but would love a more elegant solution.
I ended up just doing it the brute force method, here is my solution if anyone's interested:
$productData = array();
$sortedData = array();
$comments = '';
$saveKey = '';
$appendComment = false;
$idx = 0;
foreach ($data as $key=>$value) {
foreach ($value as $k=>$v) {
if ($k == 'productCode') {
if ($v == $saveKey) {
$appendComment = true;
} else {
$appendComment = false;
$saveKey = $v;
if ($idx !== 0) { // Don't write to array on first iteration!
$productData['productTxtVal'] = $comments;
$sortedData[] = $productData;
}
}
}
if ($k == 'productTxtVal') {
if ($appendComment == true) {
$comments .= ' ' . trim($v);
} else {
$comments = trim($v);
}
}
}
$productData = $value;
$idx++;
}
Not "elegant" but it works. I also have a check after this logic in case only one productCode is in the original array, as it won't be written to the $sortedData array since the key never changes.
The following code assumes you control the contents of the original data array (due to risk of injection using extract() function) and that no 2 items with the same productCode have the same productTextSeq.
$products = [];
foreach ($data as $item) {
// extract contents of item array into variables
extract($item);
if (!isset($products[$productCode])) {
// create product array with code, upc, text as array
$products[$productCode] = compact('productCode', 'productUPC') + ['productTxtVal' => []];
}
// add text value to array with sequence as index
$products[$productCode]['productTxtVal'][$productTextSeq] = $productTxtVal;
}
$products = array_values( // ignore array keys
array_map(function($product) {
ksort($product['productTxtVal']); // sort text as array by index/ sequence
$product['productTxtVal'] = implode(' ', $product['productTxtVal']); // implode into string
return $product;
}, $products)
);
You can run the code here: https://repl.it/BWQL
I'm trying to add another column of data to each row in a foreach loop. It's purpose is to remember the element of data importeded from XML processed to an multidimensional array. It's stuck as a scalar though the var_dumps looks fine.
<?php
$KEY = 0;
foreach ($eventsArray as $keyMe){
$thisKey['KEY'][0] = strval($KEY);
$keyedArray = array_merge($keyMe, $thisKey);
$KEY++;
}
// Prep for multisort
foreach ($keyedArray as $key => $value){
$date[$key] = $value['DATE'];
$title[$key] = $value['TITLE'];
$link[$key] = $value['LINK'];
$slide[$key] = $value['SLIDE'];
$location[$key] = $value['LOCATION'];
$time[$key]= $value['TIME'];
$KEY[$key] = $value['KEY']; // Warning: Cannot use a scalar value as an array
}
/* var_dump(
array(7) {
["DATE"]=> array(1) { [0]=> string(10) "2012-12-18" }
["TITLE"]=> array(1) { [0]=> string(20) "Event Title" }
["LINK"]=> array(1) { [0]=> string(38) "aLinkLocation.htm" }
["SLIDE"]=> array(1) { [0]=> string(2) "16" }
["LOCATION"]=> array(1) { [0]=> string(8) "Location of Event" }
["TIME"]=> array(1) { [0]=> string(3) "8am" }
["KEY"]=> array(1) { [0]=> string(2) "23" }
}
*/
array(2) {
["names"]=> array(4) {
[0]=> string(4) "Edit"
[1]=> string(6) "Delete"
[2]=> string(8) "Activate"
[3]=> string(10) "Deactivate"
}
["action"]=> array(4) {
[0]=> string(4) "ajax"
[1]=> string(4) "abc"
[2]=> string(4) "def"
[3]=> string(4) "xyz"
}
}
How do i loop through this in a single foreach loop?
Assuming both arrays are of the same size and have the same keys:
foreach($array['names'] as $k => $name) {
$action = $array['actions'][$k];
// do whatever you want to do with $name and $action
}
$newArr = array();
foreach($data['names'] as $i => $val) {
$newArr[$val] = $data['actions'][$i];
}
Or if you want a one liner at that
$newArr = array_combine($data['names'], $data['action']);
I guess the best way is a recursive function which can move through even three dimensions and more
function MoveThroughArray($arr)
{
foreach($arr as $value)
{
if(is_array($value))
MoveThroughArray($value);
else
// Do Something
}
}