I have an associative array in PHP. i want to change position of an array index and its value.
Array
(
[savedRows] => 1
[errors] => Array
(
[0] => System has Skipped this row, because you have enter not valid value "" for field "description" in sheat "Electronics Laptops" row number "4"
)
[success] => successfully saved.
)
to like this
Array
(
[savedRows] => 1
[success] => successfully saved.
[errors] => Array
(
[0] => System has Skipped this row, because you have enter not valid value "" for field "description" in sheat "Electronics Laptops" row number "4"
)
)
i want to change ["errors"] index position from second to last and [success] index position at second when ever this array build. This is a dynamic array not a static it builds when a function call on function return i am getting this array.
You can use array functions, but by far the easiest way to change it would be:
$newRow = ['savedRows' => $oldRow['savedRows'],
'success' => $oldRow['success'],
'errors' => $oldRow['errors']];
But it is an associative array, not a numeric array, so order should not be that important.
You need not make this too complicated or use any fancy functions. Just follow a few simple steps.
Store the errors sub array in another variable $errorField.
Unset the array index "errors"
Append this $errorField to a new key "errors".
$errorField = $array['errors'];
unset($array['errors']);
$array['errors'] = $errorField;
Why does the order in the array matter?
If you really need that visually, you should initialize your array before you use it and overwrite the values when you fill it:
$arr = [
'savedRows' => 0,
'success' => '',
'errors' => [],
]
// the rest of your code
Note that the order should not matter, if it does, you have another problem that you should fix.
Related
I've the following type of array:
Array (
[2017-01-01] => Array (
[booking_nb] => 0
)
[2017-01-02] => Array (
[booking_nb] => 0
);
How can I get the value of booking_nb if the date is equal to 2017-01-02 ?
Do I need to loop into the array ?
Thanks.
Assuming 2017-01-02 is an array key, you can do the following:
$array['2017-01-02']['booking_nb']; // will return the value 0
However, I recommend that if you are only storing the booking_nb value inside of each sub-array (i.e. no other elements inside the sub-arrays), you simply store them like so:
array(
'2017-01-01' => 0,
'2017-01-02' => 0,
)
This way, you can select with the following:
$array['2017-01-01']; // gives 0
The simplicity gained from this method also has the downside of the inability to store additional data, so use according to your needs.
Please help.
I've come across an input field like this and it is confusing me:
<input type="text" name="filter[][isranged][]">
I know it has to do with an array, but does this even make sense leaving the first and third bracket sets empty?
The brackets deal with creating keys for the values and what I was thinking is that this "filter" is an array that has another array in it (with a key called isranged) which has another array inside it. Am I correct? The brackets are confusing me.
The input is used to store a date like this: 09/03/2014
The [] is used to dynamically create the next element 0, 1, etc. Given two inputs named like that you will get the following $_POST array:
Array
(
[filter] => Array
(
[0] => Array
(
[isranged] => Array
(
[0] => 'Value of first input'
)
)
[1] => Array
(
[isranged] => Array
(
[0] => 'Value of second input'
)
)
)
)
[filter] gets a new numeric index for each input but the [isranged] array will always only contain 1 element [0] since they are part of different filter[x] arrays.
If you were to break that structure down into logical form it would be
filter = [
{ isranged: [ Many Values ] }
]
So basically filter is an array of an object that has a property of "isranged" that is an array itself.
So I am working with wordpress and using the following code I get the following. I know this must be a basic question but I want to know what it means:
[1012] => Array ( [0] => 1 )
[1013] => Array ( [0] => 0 )
[1014] => Array ( [0] => 0 )
[1018] => Array ( [0] => 0 )
PHP:
<?php
$all_meta_for_user = get_user_meta(get_current_user_id());
print_r( $all_meta_for_user );
?>
This data is user's metadata https://codex.wordpress.org/Function_Reference/get_user_meta
As you say, this is your metadata. Two things here: first of all, I'll store all that data together inside a single key and I'll remove the uneeded single-element array:
[my_plugin] => Array( [1012] => 1, [1013] => 0 ...)
That lowers the possibility of your data mixing with other's plugin data, having conflicts, etc. Also, as second array is probably not needed, it will make access to it a little simpler.
When you have that, it's only a matter of accessing the value like this:
if ($all_meta_for_user['my_plugin'][$id] == 1) show_the_post()
where $id is the post ID.
To use a single key, I'll do something like this (untested):
$posts_meta_for_user = get_user_meta(get_current_user_id(), 'my_plugin', true);
if (is_array($posts_meta_for_user)) {
$posts_meta_for_user[$new_id] = $new_value;
update_user_meta(get_current_user_id(), 'my_plugin', $posts_meta_for_user);
} else {
$posts_meta_for_user = array($new_id => $new_value);
add_user_meta(get_current_user_id(), 'my_plugin', $awesome_level);
}
Notice that we get only the meta value named 'my_plugin' and test it already had a value by checking that is an array. If it is, we update it, and if it's not, we create a new one. $new_id is the post ID you want to store and $new_value the value.
Just a quick one i need to get a value from an array the array is made like this
$resultOfAdd[“CaseAndMatterResult”][“ResultInfo”][“ReturnCode”];
and it gives an output of this
Array (
[AddCaseAndMatterResult] => Array (
[ResultInfo] => Array (
[ReturnCode] => OK
[Errors] =>
[Warnings] =>
)
[CaseID] => 4880062
[MatterID] => 4950481
[LeadID] => 0
[CustomerID] => 0
)
)
All i want to do is put the part "MatterID" into a variable. how would I achieve this.
i have tried
$matterID = array($resultOfAdd["MatterID"]);
and this does not work
Regards
This is a multi-dimensional, associative array. Think of it like floors of a building. The key MatterID does not live in the first dimension (floor), rather on the second, in the AddCaseAndMatterResult sub-array.
$matterID = $resultOfAdd['AddCaseAndMatterResult']['MatterID']
Successive dimensions of an array are specified with successive square-brackets, each naming the key to look in (this is true of most languages).
$matterID = $yourArray['AddCaseAndMatterResult']['MatterID'];
Use this way:
$matterID = $resultOfAdd['AddCaseAndMatterResult']['MatterID'];
I'm a bit struggling with the associative arrays in associative arrays. Point is that I always have to drill deeper in an array and I just don't get this right.
$array['sections']['items'][] = array (
'ident' => $item->attributes()->ident,
'type' => $questionType,
'title' => $item->attributes()->title,
'objective' => (string) $item->objectives->material->mattext,
'question' => (string) $item->presentation->material->mattext,
'possibilities' => array (
// is this even neccesary to tell an empty array will come here??
//(string) $item->presentation->response_lid->render_choice->flow_label->response_label->attributes()->ident => (string) $item->presentation->response_lid->render_choice->flow_label->response_label->material->mattext
)
);
foreach ($item->presentation->response_lid->render_choice->children() as $flow_label) {
$array['sections']['items']['possibilities'][] = array (
(string) $flow_label->response_label->attributes()->ident => (string) $flow_label->response_label->material->mattext
);
}
So 'possibilities' => array() contains an array and if I put a value in it like the comment illustrates I get what I need. But an array contains multiple values so I am trying to put multiple values on the position $array['sections']['items']['possibilities'][]
But this outputs that the values are stores on a different level.
...
[items] => Array
(
[0] => Array
(
[ident] => SimpleXMLElement Object
(
[0] => QTIEDIT:SCQ:1000015312
)
[type] => SCQ
...
[possibilities] => Array
(
)
)
[possibilities] => Array
(
[0] => Array
(
[1000015317] => 500 bytes
)
[1] => Array
...
What am trying to accomplish is with my foreach code above is the first [possibilities] => Array is containing the information of the second. And of course that the second will disappear.
Your $array['sections']['items'] is an array of items, so you need to specify which item to add the possibilities to:
$array['sections']['items'][$i]['possibilities'][]
Where $i is a counter in your loop.
Right now you are appending the Arrays to [items]. But you want to append them to a child element of [items]:
You do:
$array['sections']['items']['possibilities'][] = ...
But it should be something like:
$array['sections']['items'][0]['possibilities'][] = ...
$array['sections']['items'] is an array of items, and as per the way you populate the possibilities key, each item will have it's own possibilities. So, to access the possibilities of the item that is being looped over, you need to specify which one from $array['sections']['items'] by passing the index as explained in the first answer.
OR
To make things simpler, you can try
Save the item array (RHS of the first =) to a separate variable instead of defining and appending to the main array at the same time.
Set the possibilities of that variable.
Append that variable to the main $array['sections']['items']
I have:
array[{IsChecked: true, SEC: 0, STP: 0},
{IsChecked: ture ,SEC: 0, STP: 1},
{IsChecked: false, SEC: 1 ,STP: 0}]
How to get each SEC where IsCheked value is true?